Assigning global and static associative arrays

Jonathan M Davis jmdavisProg at gmx.com
Sat Sep 1 12:49:38 PDT 2012


On Saturday, September 01, 2012 11:10:17 Jonathan M Davis wrote:
> On Saturday, September 01, 2012 16:14:29 ixid wrote:
> > Those still have different addresses when made immutable too, is
> > this something that could be optimized for all immutable enums to
> > behave like strings or are there reasons to keep them unique at
> > each instance?
> 
> The compiler has to got to extra work to make string literals use the same
> memory like that. While it _could_ do the same with other array literals of
> basic types, it's not worth it, because they're just not used enough,
> whereas string literals get used all over the place.

The other thing to remember is that for the whole sharing address thing to 
work, the elements in the array literal must be immutable, and string literals 
are the only ones which are that way by default. Other array literals are only 
going to have immutable elements if they're used to directly initialize an 
array with immutable elements. So, it's probably relatively rare for other 
array literals to even be used in a way, and unless they're used that way 
_and_ the same literal is used in multiple places, then you can't make them 
share the same memory. So, there's not much point in trying to make them share 
memory. String literals, on the other hand, are immutable and frequently 
duplicated, so there's some value in sharing them. But even with them, it 
wouldn't surprise me at all if they only share memory within a single module, 
since separate compilation could screw with their ability to share. I don't 
know though.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list