miniBB ®

miniBB

®
Support Forums
  
 · Start · Sign in · Register · Search · Statistics · File Bank · Manual ·
Master Class miniBB Support Forums / Master Class /  
 

(micro-optimization) - changing in_array to isset in the $userInfo condition from bb_func_vthread.php file

 
Author tom322
Active Member
#1 · Posted: 16 Jun 2019 03:02Reply Quote
For those interested in micro-optimization.. as far as I know, isset is always faster than in_array because isset checks only one value from array, and in_array goes through all values. So I checked the code in bb_func_vthread.php (which the majority of forum visitors land most frequently) and there is this line of code in the "/* User info in posts */" section:

if(!in_array($cols[0],$userInfo)) $userInfo[]=$cols[0];
I thought to replace it to make use of the isset() function and associative array, so I changed it to:

if(!isset($userInfo[$cols[0]])) $userInfo+=array($userInfo=>$cols[0]);
From what I initially observed after the change, nothing was broken and the speed was a little faster (from my testing using the browser Network).

My question to Paul or others - is this a correct custom change; wouldn't it break anything from other areas of the script/addons; is it safe to use? Thank you.

Author tom322
Active Member
#2 · Posted: 16 Jun 2019 03:24Reply Quote
Ok, nevermind, it breaks something (the $getClForums function is not getting proper user_id values), so it's not worthy optimization I guess, sorry :

Author Paul
Lead Lead Developer
#3 · Posted: 16 Jun 2019 12:31Reply Quote
tom322:
isset is always faster than in_array because isset checks only one value from array, and in_array goes through all values
It is theoretically true, but if we speak about the $userInfo array which is generated in the bb_func_vthread.php file, then it generates only as much values as are set in the $viewmaxreplys setting - i.e. the amount of messages per page which obviously rather won't be set up for more than 50. THIS amount actually doesn't affect anything in terms of `in_array` vs. `isset`. If there would be about 50,000 or even 5,000 values then you'd note a difference :)

That's why this optimization wouldn't make a lot of sense... specially because for `getClForums`, $userInfo be substituted as a plain array of values (user IDs). You solution actually "sums up"... umm, not sure what exactly :)

tom322:
nothing was broken and the speed was a little faster
I suppose an impression of changed speed is a placebo effect, or whatever else happened at that moment (for example, less users visited the site in a moment that's why you were having it). I suppose, this function in terms of the value it holds, is hard-to-catch at the speed measuring.

Author tom322
Active Member
#4 · Posted: 16 Jun 2019 19:09Reply Quote
Ok, thank you - that makes sense. You're right that it was not a real test of speed ::

Master Class miniBB Support Forums / Master Class /
 (micro-optimization) - changing in_array to isset in the $userInfo condition from bb_func_vthread.php file
 Share Topic's Link

Your Reply Click this icon to move up to the quoted message

BB code for the Bold StyleBB code for the Italic StyleBB Code for the PictureBB code for the URLUpload a File

  ?
Post as a Guest, leaving the Password field blank. You could also enter a Guest name, if it's not taken by a member yet. Sign-in and post at once, or just sign-in, bypassing the message's text.


Before posting, make sure your message is compliant with forum rules; otherwise it could be locked or removed with no explanation.

 

 
miniBB Support Forums Powered by Forum Software miniBB ® Home  Features  Requirements  Demo  Download  Showcase  Gallery of Arts
Compiler  Premium Extensions  Premium Support  License  Contact Us
Check out the File and Picture Attachments add-on: extend your miniBB-forums, attaching images and files.