std::string responsible for half the allocations in chrome
H. S. Teoh via Digitalmars-d
digitalmars-d at puremagic.com
Sun Dec 7 15:28:43 PST 2014
On Sun, Dec 07, 2014 at 06:08:51PM +0000, Sean Kelly via Digitalmars-d wrote:
> On Saturday, 6 December 2014 at 16:32:30 UTC, H. S. Teoh via Digitalmars-d
> wrote:
> >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.
>
> At the same time, immutable means that if you do need to do any string
> manipulation, you need to copy the string first. I think whether
> immutable means more or less allocations than mutable/const is
> actually more dependent on application design than anything, and some
> applications can't afford the copying that using immutable requires.
True, but at least if mutation is expected you could use char[] instead,
which still allows cheap slicing and appending without invalidating
other references to the data. In C, people pass char* everywhere without
any indication of whether mutation will be expected or not (const char*
is sadly not consistently used outside the stdlib... and in some places
even in the stdlib). This means that whenever you're unsure, you have to
strdup() yet again.
D's string isn't 100% ideal though... there *are* some places in Phobos
that traffick in string even where const(char)[] or char[] might make
more sense. One side-effect of this is certain functions being overly
eager in heap allocation because they take string but have to mutate the
input, so they have to dup/idup to make the change and then return it --
again as string. The next function that has to do something else with
that return value will again have to allocate all over again.
T
--
Живёшь только однажды.
More information about the Digitalmars-d
mailing list