Efficiency of immutable vs mutable

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Nov 3 13:08:48 PST 2015


On Tuesday, November 03, 2015 18:44:06 Andrew via Digitalmars-d-learn wrote:
> This:
>
> On Tuesday, 3 November 2015 at 04:08:09 UTC, TheFlyingFiddle
> wrote:
> > __gshared char[4] lookup = ['a', 't', 'g', 'c];
>
> Has the same efficiency gain as immutable, so it looks like a
> thread-local vs global difference and the extra cost is going
> through the thread-local lookup.

Just FYI. Be _very_ careful with __gshared, since it essentially breaks the
type system. It's really only intended to be used with extern(C)
declarations so that you can access variables from C libraries. The compiler
still treats a __gshared variables as thread-local, because __gshared does
not affect the type. If you want a variable to not be in TLS, you really
should be using shared rather than __gshared. D's type system is designed so
that variables not marked with shared or immutable are thread-local, and
trying to get around that is just asking for trouble (e.g. the compiler can
and will make assumptions based on the fact that a non-shared variable is in
TLS per the type system).

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list