On Monday, 12 August 2019 at 22:48:43 UTC, Bert wrote:
I have a recursive class structure(think of a graph or tree) and I need to keep a global state for it,

If I'm understanding the problem correctly, it seems like you have a choice to make: either "bloat" the child nodes by the size of a pointer/reference to the shared tree state (4 or 8 bytes), or forego that by traversing the tree (from child to root node) every time you want to access the state.

But if you don't want traversal every time, you're going to have to cache some kind of pointer (or Tree ID in the case of a central dictionary). I suppose if you got down to the nitty-gritty maybe you could use an 8- or 16-bit value for the key ID? (Disclosure: I know absolutely zero about how D lays out classes and structs in memory, this might not save any memory at all)

It would help if you could provide some rough metrics:

- How deep you expect each recursive structure to be (from root to deepest leaf node)

- How many total nodes you expect to have across all trees (such that adding a single pointer to each instance would be memory-prohibitive)

- How often each node will have to access the shared state (such that traversal-every-time would be too slow)

That might help in figuring out the best approach.

Reply via email to