Re: Use "ropes" for string concatenation
| From: | Nikita Popov | Date: | Tue, 24 Mar 2015 09:11:18 +0000 |
| Subject: | Re: Use "ropes" for string concatenation | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-85438@lists.php.net to get a copy of this message | ||
On Mon, Mar 23, 2015 at 11:41 PM, Dmitry Stogov <dmitry@zend.com> wrote:
> Hi,
>
> Recently, Xinchen and me worked on optimization that eliminates useless
> reallocations and copying during string concatenation (ZEND_ADD_STRING and
> family + ZEND_CONCAT).
>
> The idea comes from ropes, but adopted especially for our needs.
> Rope is popular data structure in languages with immutable strings.
> http://en.wikipedia.org/wiki/Rope_%28data_structure%29
>
> We don't try to use ropes everywhere in the engine (at least it's too
> later for 7.0), only for concatenation.
>
> The first branch uses ropes only instead of ZEND_ADD_STRING and family.
> This must be safe. The only problem is possible memory leaks on exception
> (but we already have this problem anyway). The simplest way to understand
> the patch - read code for new opcodes in zend_vm_def.h.
>
> https://github.com/php/php-src/pull/1194/files
>
> The second branch in addition uses ropes for series of ZEND_CONCAT. It
> disables calls to do_operation(ZEND_CONCAT) handler of custom internal
> classes.
>
> https://github.com/php/php-src/pull/1195/files
>
> Both make slight speed improvement (first +0.3%, second +0.6% on wordpress
> home page).
>
> We don't currently use ability to override CONCAT behavior in internal
> classes, and I'm not sure if it may be useful at all. (For example Lua
> doesn't provide concat meta-method). May be remove it?
>
Overloading concat operator will be useful in the future for the
implementation of the UString class, for example.
Nikita