php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #48044 Simple optimization to speed up many web apps
Submitted: 2009-04-22 12:51 UTC Modified: 2009-04-22 22:28 UTC
From: rasmus at mindplay dot dk Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 5.2.9 OS: linux/windows
Private report: No CVE-ID: None
 [2009-04-22 12:51 UTC] 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.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-04-22 13:20 UTC] 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 13:42 UTC] scottmac@php.net
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.
 [2009-04-22 22:28 UTC] rasmus at mindplay dot dk
Good point. What was I thinking.

Thanks.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 04 12:01:31 2024 UTC