I am in a situation where I need to share data across many instances of a polymorphic object tree, but then again, I need the shared data to be "per-tree" so using static class members in the base class is no really an option.
I don't want to "overburden" each instance with extra member pointers to the shared data, so my current approach (considering I use trees) is to have the shared data as members of the tree root node, and each access to shared data goes through a chain of indirections depending on the depth of the particular node through which "tree global" data is accessed.
Since there are scenarios where shared data will be very frequently (millions per second... at least that's what is intended) accessed, I wonder if there is some design pattern that can help me avoid the indirections to reach to the root node while still not introducing extra bloat to the footprint of objects.
While it is possible to "cache" the root node pointer as a local for say a tight loop that accesses shared data, there are many scenarios where functionality will cascade along tree nodes and even switch trees in the process, so caching the root node pointer is applicable only in a narrow context.
- note the mention of static members does not limit the scope of the implementation to C++, I added the C tag as well because at this point I am open to any ideas.