1 (edited by orlandu63 2007-11-13 03:57)

Topic: Optimizations

In function generate_navlinks(), count($extra_links[1]) is being redundantly executed with each loop iteration.
In function check_bans(), count($cur_ban_ips) is being redundantly executed with each loop iteration.

You can actually remove the entire for(; ; ) loop in check_bans() with this code:

//after line 363 in rev 1084
$cur_ban_ips = array_map(create_function('$ip', 'return "$ip.";'), $cur_ban_ips); //I don't even think you need this part, along with line 328.
if(in_array($user_ip, $cur_ban_ips)) {
//lines 371-379
}

Re: Optimizations

That code won't work, just take a look at how check_bans works wink
And I'm confused, do you have any benchmarks to back up the idea that storing the count and using the stored value actually reduces page load time?

3 (edited by orlandu63 2007-11-13 12:51)

Re: Optimizations

Yes. Using this code,

$array = array_fill(0, 500000, null);
$time = microtime(true);
for($i = 0; $i <= count($array); ++$i);
echo '<strong>', microtime(true)-$time, '</strong><br/>';

$time = microtime(true);
$count = count($array);
for($i = 0; $i <= $count; ++$i);
echo '<em>', microtime(true)-$time, '</em>';

It took 35ms to iterate through the first loop.
It took just under 10ms to iterate through the second loop.

Re: Optimizations

Fair enough wink

Re: Optimizations

Yes, that is a good optimization, but making a nameless function would cause more overhead.

Re: Optimizations

Which we're not going to be doing wink

Re: Optimizations

Looks like it was added in, orlandu63.

http://dev.punbb.org/changeset/1090

8 (edited by oneless 2007-11-16 04:01)

Re: Optimizations

Nice find orlandu63 smile

So how would one go about implementing this optimization into 1.2?.. smartys said the above code wouldn`t work.. or is this optimization only applicable to 1.3..

Re: Optimizations

anyone??

10 (edited by orlandu63 2007-11-17 04:06)

Re: Optimizations

In functions.php, replace for ($i = 0; $i < count($extra_links[1]); ++$i) with for ($i = 0, $num_links = count($extra_links); $i < $num_links; ++$i). (Around line 315)
In functions.php, replace for ($i = 0; $i < count($cur_ban_ips); ++$i) with for ($i = 0, $num_ips = count($cur_ban_ips); $i < $num_ips; ++$i) (Around line 213)

Re: Optimizations

Thank you so much orlandu63.

Re: Optimizations

oneless wrote:

Thank you so much orlandu63.

Woops -- mistake on my part:

Replace:

for ($i = 0, $num_links = count($extra_links); $i < $num_links; ++$i)

With:

for ($i = 0, $num_links = count($extra_links[1]); $i < $num_links; ++$i)

Editing code without syntax highlighting confuses me tongue

Re: Optimizations

What's so odd about that? Looks good IMO.

Re: Optimizations

What does the "[1]" do because the origional code appears to work fine.. I have it on a live forum now too.

Re: Optimizations

As much as we appreciate tips on how to optimize PunBB, I would like to point out that this particular optimization probably won't be measurable considering we're talking about only a few iterations. In other words, I wouldn't bother porting this to 1.2. Still, as we say in Sweden, many streams make a river smile

"Programming is like sex: one mistake and you have to support it for the rest of your life."