#48044 [Opn->Bgs]: Simple optimization to speed up many web apps
| From: | scottmac@php.net | Date: | Wed, 22 Apr 2009 13:42:28 +0000 |
| Subject: | #48044 [Opn->Bgs]: Simple optimization to speed up many web apps | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-136129@lists.php.net to get a copy of this message | ||
ID: 48044
Updated by: scottmac@php.net
Reported By: rasmus at mindplay dot dk
-Status: Open
+Status: Bogus
Bug Type: Feature/Change Request
Operating System: linux/windows
PHP Version: 5.2.9
New Comment:
Strings in C are based on char*, when you append to a string a memory
segment that's big enough to accommodate the result needs to be found.
Previous Comments:
------------------------------------------------------------------------
[2009-04-22 13:20:25] rasmus at mindplay dot dk
Hmm, it seems I may be wrong about this.
I did some benchmarking, and .= is definitely considerably faster.
I still suspect there may be some memory overhead though? From the
following line of code:
$output .= " $value\n";
I received the following error message:
Allowed memory size of 33554432 bytes exhausted (tried to allocate
253344 bytes)
$value in this case is relatively short - $output is about 250KB of
HTML. Why would appending to $output mean another allocation of a
further 250KB of memory?
------------------------------------------------------------------------
[2009-04-22 12:51:04] rasmus at mindplay dot dk
Description:
------------
I suspect the .= operator could be greatly optimized, using a very
simple optimization.
Reproduce code:
---------------
$output .= "<div>".$string."</div>";
The answer to that probably is, PHP creates a copy of $output, and
appends to that, e.g.:
Expected result:
----------------
Just a simple string-concatenation. But many applications build HTML by
appending to an output string in this manner - if you have 250KB of HTML
in your $output variable already, every time you append to it, PHP needs
to reallocate memory and copy 250KB of string data.
Actual result:
--------------
It works, but it's extremely inefficient (almost x 2 memory usage, plus
huge overhead from copying the string every time)
Since the .= operator was used, you could instead append directly to
the string on the left side of the operator, after composing the
resulting string on the right side of the operator.
It seems what actually happens right now is this:
$output = $output . "<div>".$string."</div>";
The difference is that the whole string, on the right side of the =
operator, is composed, and then assigned.
For the .= operator, this is unnecessary.
From my recent analysis of bottlenecks in Drupal (a poorly architected
piece of software, I know, but popular nonetheless), it seems that a
significant part of Drupal's slowness comes from the fact that
everything it does involves appending to huge strings - often the entire
output.
I suspect that this optimization could potentially increase the speed
of Drupal (and probably many other applications) dramatically.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=48044&edit=1