Very effective performance fix
Hi,
I was poking around some in the guts of tikilib, and we're doing a lot of expensive substr() calls in the parse_pp_np() function. I profiled it, and we're spending almost 50% of the time in this function.
Below is a simple fix I implemented on my site, it reduces the use of substr() quite dramatically. I haven't tried to optimize any of the other code, just trying to avoid those substr(), so my "fix" most certainly isn't the best possible. But it gives an idea what can be done. If there's interest, I'd be more than happy to rewrite this function and submit a patch.
On my site, this reduces the effective load time of a typical page (tiki-view_blog) from 0.75s to 0.45s. This is simply because I reduce the time spent in substr() from about 40% to 3% (of total CPU time used).
Thanks,
-- Leif
In lib/tikilib.php, parse_pp_np() I simply added a test on the next character before doing substr(), i.e.
// Find all sections delimited by ... $new_data = ''; $nopa = ''; $state = true; $skip = false; $dlength=strlen($data); for ($i = 0; $i < $dlength; $i++) { if ($data[$i] == '~') { $tag5 = substr($data, $i, 5); $tag4 = substr($tag5, 0, 4); $tag1 = substr($tag4, 0, 1); // Beginning of a noparse section found if ($state && $tag4 == '') { $i += 3; $state = false; $skip = true; } // Termination of a noparse section found if (!$state && ($tag5 == '')) { $state = true; $i += 4; $skip = true; $key = md5($this->genPass()); $new_data .= $key; $aux["key"] = $key; $aux["data"] = $nopa; $noparsed[] = $aux; $nopa = ''; } } else { $tag1 = $data[$i]; } . . .