Reputation: 11
My understanding is that the C++ compiler is free to place program-wide variables wherever it wants, as long as they do not appear together in a struct.
//in .h
struct Singleton {
static int hotvar1;
static int hotvar2;
};
//in .cpp
//is placed at location x
int Singleton::hotvar1 = 0;
//is next to hotvar2, but placed at a random location
int Singleton::hotvar2 = 0;
versus
struct MyStruct {
//is placed at memory address x
int hotvar1;
//is placed at memory address x+4, therefore probably in the same cacheline
int hotvar2;
};
My program uses a lot of singletons, which contain many high-frequency read, low-frequency write variables. I have read in another thread, that many compilers use some sort of hashing algorithm for the placement of these program-wide variables in .bss or .data section, which is more than sub-optimal for cache locality.
Is there any method for grouping these variables together without losing data encapsulation by just putting all of these static vars into a single global struct?
Idea:
struct HotVars {
int Singleton1::hotvar1 = 0;
int Singleton2::hotvar2 = 0;
int Singleton1::apiHandle = 0;
};
I know that this is some kind of micro optimisation, but my program is severely back-end bound so improving cache locality is definitely a good idea.
Upvotes: 1
Views: 108