2 ms·
> That's why D has an `immutable` qualifier, giving the optimizer to do optimizations assuming it does not change. Which optimizations are enabled by immutable
by matheusmoreira 7y ago
> That's why D has an `immutable` qualifier, giving the optimizer to do optimizations assuming it does not change.
Which optimizations are enabled by immutable data? I can think of constant folding. Is the data statically allocated in a read-only page?
> For a famous example, Fortran assumes two arrays never overlap. In C/C++ they can. This is the source of a persistent gap in performance between Fortran and C/C++.
What sort of optimizations does this assumption enable? Does it allow the compiler to freely reorder or parallelize the code?
C assumes that pointers to different types are never equal. This is incompatible with common systems programming concepts such as type punning. Even something simple like reinterpreting some data structure as an array of uint8_t can make the optimizer introduce bugs into the code. Notably, the Linux kernel is compiled with strict aliasing disabled:
https://lkml.org/lkml/2003/2/26/158 https://lkml.org/lkml/2003/2/26/158
https://lkml.org/lkml/2009/1/12/369 https://lkml.org/lkml/2009/1/12/369
- MauranKilom 7y ago> What sort of optimizations does this assumption enable? Does it allow the compiler to freely reorder or parallelize the code? Consider this code: void foo(int* data, const int& count) { for (int i = 0; i < count; ++i) data[i]++; } The compiler has to account for the possibility of `count` being inside of `data`. It has to reload `count` from memory at every iteration. This also prevents e.g. auto-vecorization: Compare https://godbolt.org/z/sNRKv4 https://godbolt.org/z/sNRKv4 vs https://godbolt.org/z/iJq5yn https://godbolt.org/z/iJq5yn. Similar story if you manipulate data with multiple arrays in play: Unless the compiler knows (via `restrict`, type-based alias analysis or just seeing the definition) that two objects are non-overlapping, it has to correctly handle the worst case. See also this related discussion: https://news.ycombinator.com/item?id=20800076 https://news.ycombinator.com/item?id=20800076
- Silhouette 7y agoWhich optimizations are enabled by immutable data? If nothing else, deep immutability means you can pass around a reference to data instead of the data itself but still treat it as having value semantics. This can be a huge saving with large data structures, sharing data across threads, etc. C++ programs are full of functions taking const references to things instead of the things themselves because C++ programmers have to do this optimisation (or some equivalent) manually to get the same performance boost, because the language doesn't prevent all possible cases of aliasing that would make automatically applying that transformation safe.
- WalterBright 7y ago> Which optimizations are enabled by immutable data? int test(immutable(int)* p, int* q) { foo(*p); *q = 4; foo(*p); // don't have to reload *p }