[webkit-dev] String::operator+= considered harmful

Dirk Schulze dschulze at adobe.com
Tue Sep 4 16:44:41 PDT 2012


Yes, looks like the efforts didn't went further. Anyway, is there no possibility to improve operator+= further? It is very likely that even future code will land with this operator instead of StringBuilder. I think it is better to try to change the operator (if possible) instead of people.

Greetings,
Dirk

On Sep 4, 2012, at 4:41 PM, Adam Barth <abarth at webkit.org> wrote:

> Ah, you're think of operator+, which is now quite efficient.  This
> thread is about operator+=, which is sadly slower than molasses.
> 
> Adam
> 
> 
> On Tue, Sep 4, 2012 at 4:38 PM, Dirk Schulze <dschulze at adobe.com> wrote:
>> With a short search in the logs I found optimizations for  at least operator+, but didn't search further:
>> 
>> http://trac.webkit.org/changeset/86330
>> https://bugs.webkit.org/show_bug.cgi?id=58420
>> 
>> Greetings,
>> Dirk
>> 
>> On Sep 4, 2012, at 4:31 PM, Adam Barth <abarth at webkit.org> wrote:
>> 
>>> Do you have a proposal for how that would work and/or a link to the
>>> previous discussion?
>>> 
>>> Adam
>>> 
>>> 
>>> On Tue, Sep 4, 2012 at 4:27 PM, Dirk Schulze <dschulze at adobe.com> wrote:
>>>> I thought we had efforts to make String::operator+= use StringBuilder somehow? I can remember that we had a discussion on webkit-dev and definitely on bugzilla about improving String::operator+= instead of replacing it with StringBuilder.
>>>> 
>>>> Greetings,
>>>> Dirk
>>>> 
>>>> On Sep 4, 2012, at 4:22 PM, Adam Barth <abarth at webkit.org> wrote:
>>>> 
>>>>> As part of the work to deploy efficient string patterns [1] throughout
>>>>> WebKit, Benjamin and I noticed a bunch of very inefficient code that
>>>>> uses operator+= with Strings:
>>>>> 
>>>>> String foo;
>>>>> for (...)
>>>>> foo += [...];
>>>>> return foo;
>>>>> 
>>>>> This pattern is particularly inefficient because += mallocs an
>>>>> entirely new buffer for the result and copies the the string into the
>>>>> new buffer.  That means that this pattern makes O(n) calls to malloc
>>>>> and does O(n^2) work.  A more efficient pattern is to use
>>>>> StringBuilder:
>>>>> 
>>>>> StringBuilder foo;
>>>>> for (...)
>>>>> foo.append([...]);
>>>>> return foo.toString();
>>>>> 
>>>>> I'm in the process of going through WebCore and removing all callers
>>>>> of WTF::String::operator+=.  Once that's complete, my plan is to
>>>>> remove WTF::String::operator+= from WTFString.h.  Hopefully that will
>>>>> nudge contributors and reviewers towards more efficient string
>>>>> patterns.
>>>>> 
>>>>> Removing operator+= will likely require changes to a number of
>>>>> port-specific files.  I'll do my best to remove these, but I might
>>>>> need some help from maintainers of individual ports.  If you're
>>>>> interested in helping out, please let me know.
>>>>> 
>>>>> Many thanks,
>>>>> Adam
>>>>> 
>>>>> [1] http://trac.webkit.org/wiki/EfficientStrings
>>>>> _______________________________________________
>>>>> webkit-dev mailing list
>>>>> webkit-dev at lists.webkit.org
>>>>> http://lists.webkit.org/mailman/listinfo/webkit-dev
>>>> 
>>> _______________________________________________
>>> webkit-dev mailing list
>>> webkit-dev at lists.webkit.org
>>> http://lists.webkit.org/mailman/listinfo/webkit-dev
>> 
> _______________________________________________
> webkit-dev mailing list
> webkit-dev at lists.webkit.org
> http://lists.webkit.org/mailman/listinfo/webkit-dev



More information about the webkit-dev mailing list