3 ms·
For primitive types, a modern compiler's dependency analyzer can get better stack locality than anything you can do. Therefore I like to put all primitives at
by Daniel_Newby 17y ago
For primitive types, a modern compiler's dependency analyzer can get better stack locality than anything you can do. Therefore I like to put all primitives at the top so they are easy to find.
Mutable composite types that use the heap should be scoped broadly, to reuse their heap buffer without touching the memory allocator. Example: C++ std::string.
Immutable heap composites could be scoped narrowly to reuse heap as soon as possible. On the other hand that reduces locality of reference for the deallocator code, and probably spams the branch predictor. If the immutable is small and deeply nested, it should probably be broadly scoped because branch prediction is made of solid gold (unless the language is garbage collected, making deallocation branchless).
Hmm... That got complicated. Stick them all at the top unless you are optimizing a nested loop.