4 ms·
Surely this doesn't work without const since that would be breaking the expectation of unique objects. I suppose a sufficiently thorough optimizer can check if
by kevin_thibedeau 4y ago
Surely this doesn't work without const since that would be breaking the expectation of unique objects. I suppose a sufficiently thorough optimizer can check if an array is modified or escapes via a pointer to disable the compaction.
- nneonneo 4y agoIf an object is initialized with a constant, the constant itself can be located in read-only memory and copied to the object at runtime. The constant can then be deduplicated.
- brrrrrm 4y agoconst pretty much does nothing to allow optimization in C/C++ :(
- jcelerier 4y agoIt definitely does for globals
- tsimionescu 4y agoI think in general it does something for symbols visible outside the compilation unit (globals, public fields). Otherwise, the compiler usually has enough information to decide if something is actually a constant regardless of the const modifier.
- heinrich5991 4y ago`const` only does something for (global, static, local) variable declarations. The rest is just guidance to for the programmer. It doesn't do anything on pointers, and I think it doesn't do anything on members, but not entirely sure on the last one.
- chrisseaton 4y ago> expectation of unique objects Is that expectation granted by the language specification? Identity is often a problem with optimisations, but only if the specification guarantees it in the first place.
- leni536 4y agoIn C++ string literals are const. In C they are non-const, but modifyng them is undefined behavior regardless.