Assigning global and static associative arrays

Ali Çehreli acehreli at yahoo.com
Sun Sep 2 19:34:32 PDT 2012


On 09/02/2012 12:24 PM, Philippe Sigaud wrote:
 > On Sun, Sep 2, 2012 at 8:50 PM, Ali Çehreli<acehreli at yahoo.com>  wrote:
 >> On 09/02/2012 06:45 AM, monarch_dodra wrote:
 >>
 >>> Is there *any* scenario where one would choose the enum over the
 >>> static immutable...?
 >
 > Due to Jonathan advice, I converted a part of my code (enum =>  static
 > this). At runtime, I got a 40% decrease in runtime, a *very* good
 > news. But then CTFE does not work anymore: some functions tell me the
 > static AA I use is not initialized (or whatever).
 > I tried to use if(__ctfe) but then I lose most of the speedup.
 >
 > All in all, for my Pegged parser generator project, I ended up
 > generating code specifically for compile-time (for CT parsing) and
 > some other function for runtime. So yes, enums are useful for CT
 > computation sometimes.

Thank you. Then I am sticking with the guidelines under the "How to use" 
section here:

   http://ddili.org/ders/d.en/const_and_immutable.html

There is some warning there against making arrays and associative arrays 
enums:

<quote>
enum constants bring a hidden cost when they are used for arrays or 
associative arrays [...] For that reason, it may make more sense to 
define arrays and associative arrays as immutable variables if they are 
going to be used more than once in the program.
</quote>

<quote>Consider the hidden cost of enum arrays and enum associative 
arrays. Define them as immutable variables if the arrays are large and 
they are used more than once in the program.</quote>

Ali



More information about the Digitalmars-d-learn mailing list