string manipulation performance

Jonathan M Davis jmdavisProg at gmx.com
Sun Jun 12 18:22:12 PDT 2011


On 2011-06-12 18:02, Lloyd Dupont wrote:
> But... string being immutable I don't see the point of allocating some
> space for one..
> Am I missing something?

Just because it's immutable doesn't mean that it doesn't need to exist at 
runtime. All immutable means is that you can't change it. It could have been 
created entirely at runtime with values decided entirely at runtime. An 
immutable variable takes up just as much space as a mutable one. The 
differences are that you can never change it and that it's implicitly shared.

Now, manifest constants don't exist per se. e.g.

enum str = "hello world";

str doesn't exist at runtime, and you can't take its address. But that's 
because every place that you use str, it gets replaced with its value. So, 
every place that it's used takes up just as much space as if you'd put the 
value there directly.

immutable variables, however, very much exist just as much as mutable ones.

- Jonathan M davis


More information about the Digitalmars-d-learn mailing list