[webkit-dev] String::operator+= considered harmful
Maciej Stachowiak
mjs at apple.com
Tue Sep 4 16:53:40 PDT 2012
operator+ is now efficient (potentially more so than using a StringBuilder if you can do it all in one statement). operator+= still sucks, and I don't think we came up with an obvious way to get good performance with the same syntax. One possibility: we could add operator+=(String&) to StringBuilder rather than String. Then you could use the sugary operator syntax with the efficient class.
Regards,
Maciej
On 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
More information about the webkit-dev
mailing list