[webkit-dev] String::operator+= considered harmful
mjs at apple.com
Tue Sep 4 16:56:54 PDT 2012
On Sep 4, 2012, at 4:44 PM, Dirk Schulze <dschulze at adobe.com> wrote:
> 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.
If you think of it in terms of String::operator+=, it's hard to make it efficient to repeatedly apply it. The problem is that it returns a String. To make it chainable, you have two basic options:
- Immediately collapse down to the current String representation (causing the O(N^2) issue.
- Convert String itself to be able to track a pending append operation without doing it, in the style of a "rope", but this costs memory and potentially makes all Strings slower to access.
Both of these options are worse performance-wise than using StringBuilder. Maybe giving StringBuilder an operator+= and removing String's operator+= is enough of a carrot to point people in the right direction.
More information about the webkit-dev