std::string responsible for half the allocations in chrome

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Sat Dec 6 08:30:24 PST 2014


On Sat, Dec 06, 2014 at 05:10:09PM +0100, Joseph Rushton Wakeling via Digitalmars-d wrote:
> On 05/12/14 23:03, deadalnix via Digitalmars-d wrote:
> >http://www.reddit.com/r/programming/comments/2ocmvb/stdstring_is_responsible_for_almost_half_of_all/
> >
> >Looks like someone need immutable(char)[] .
> 
> Someone asked me the other day, and I realized I didn't have a ready
> answer as I'd never particularly considered it: why is it
> important/beneficial that the string type be immutable(char)[] ?

Immutable, because then you can freely use slices as substrings without
worrying that the substring you hand to function X might get modified by
unrelated function Y while function X is not quite done with processing
it yet.

D arrays in general, because .length eliminates an entire class of
performance killers, namely strlen(). :-P Plus, the GC allows you to
append to strings without worrying that other references to the original
string will also unwittingly get lengthened (unlike in C, where
appending to a char* will cause the lengthened string to be visible via
other copies of that char* too -- the solution is usually to call
strdup() everywhere, which is another performance killer).


T

-- 
A linguistics professor was lecturing to his class one day. "In
English," he said, "A double negative forms a positive. In some
languages, though, such as Russian, a double negative is still a
negative. However, there is no language wherein a double positive can
form a negative." A voice from the back of the room piped up, "Yeah,
yeah."


More information about the Digitalmars-d mailing list