www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Efficiency of immutable vs mutable

reply Andrew <atrotman ebay.com> writes:
I've written a short D program that involves many lookups into a 
static array. When I make the array immutable the program runs 
faster.  This must mean that immutable is more than a restriction 
on access, it must affect the compiler output. But why and how?

Thanks
Andrew
Nov 02 2015
next sibling parent reply TheFlyingFiddle <borin.lukas gmail.com> writes:
On Tuesday, 3 November 2015 at 03:16:07 UTC, Andrew wrote:
 I've written a short D program that involves many lookups into 
 a static array. When I make the array immutable the program 
 runs faster.  This must mean that immutable is more than a 
 restriction on access, it must affect the compiler output. But 
 why and how?

 Thanks
 Andrew
I'm going to speculate a bit here since you did not post any code. Say you have this code: static char[4] lookup = ['a', 't', 'g', 'c'] This lookup table will be in thread local storage (tls). TLS is a way to have global variables that are not shared between threads that is every thread has it's own copy of the variable. TLS variables are not as fast to access as true global variables however since the accessing code has to do some additional lookup based on the thread to gain access to the correct copy. If you change the above code to this: static immutable char[4] lookup = ['a', 't', 'g', 'c']; Then you get an immutable array. Since this array cannot change and is the same on all threads there is no reason to have separate storage for each thread. Thus the above code will create a true global variable that is shared between the threads. So what you are likely seeing is that a variable changed from being tls to being a shared global. Global accessing is faster then TLS accessing so this is probably what made your code run faster. If you want a shared global that is not immutable you can do this. __gshared char[4] lookup = ['a', 't', 'g', 'c]; Be warned though these kinds of globals are not thread safe so mutation and accessing should be synchronized if your using more then one thread.
Nov 02 2015
parent reply Andrew <atrotman ebay.com> writes:
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. Thanks
Nov 03 2015
parent Jonathan M Davis via Digitalmars-d-learn writes:
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
Nov 03 2015
prev sibling parent Jonathan M Davis via Digitalmars-d-learn writes:
On Tuesday, November 03, 2015 03:16:06 Andrew via Digitalmars-d-learn wrote:
 I've written a short D program that involves many lookups into a
 static array. When I make the array immutable the program runs
 faster.  This must mean that immutable is more than a restriction
 on access, it must affect the compiler output. But why and how?
If a variable is immutable, then the compiler knows that it will never change, and at least some of the time, the compiler is able to use that information to better optimize the code. But exactly how much the compiler is able to optimize based on immutable is going to be very dependent on the code in question and on how the compiler's optimizer works. - Jonathan M Davis
Nov 02 2015