Static constructors in structs.

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Oct 30 19:31:20 PDT 2015


On Friday, 30 October 2015 at 21:29:22 UTC, BBasile wrote:
> __gshared is mostly usefull on fields (eg public uint a)

That's only true if it is at the module level or static. Ordinary 
struct members are whatever the container is and class members 
are on the heap unless you do something fancy.

module test;

int tls; // this is in tls
shared(int) s; // this is not TLS
__gshared int s; // also not TLS, same as shared(), but doesn't 
change the type

struct Foo {
    int var; // will be TLS only is the instance is TLS
    static int tls; // put in TLS
    shared(int) s; // not TLS
    __gshared int s; // also not TLS
}

class Foo {
    int var; // not TLS unless you do some weird allocation scheme
    static int tls; // TLS
    // you get the idea now
}

> because it prevents a data to be put on the TLS, which in 
> certain case reduces the perfs up to 30%. The byte code using a 
> global variable that's not __gshared can be incredibly slower !

right, TLS has some cost but it has benefits too. Best 
performance is often gotten by not using globals much at all - 
try to get a local copy on the stack to work with. Then you will 
more likely be in the CPU cache and can see enormous speedups.


More information about the Digitalmars-d-learn mailing list