jeudi 1 mars 2018

Most efficient way to get a tree object's root node

I am interested in some design pattern that will give me very fast access from any tree node to the tree root with as little CPU and memory overhead as possible.

Now, to iterate the obvious solutions, and explain why they are inapplicable:

  • go down the tree until the root is found - which is what I am currently doing, but as the tree can get quite deep and root access is pretty frequent, that is not an ideal solution, even if mitigating the overhead by caching and reusing the root as much as possible

  • store the root node pointer in every node - very fast, but adds a significant memory overhead, as the tree is quite big

Now, if there was only one tree, I could use a singleton, a static class member or any similar approach, however the application has more than one tree, each representing an open project.

Before resorting to searching the root, my design involved only one single active project at a time, so I could presume that if any user activity is occurring, it would be targeting the active project, which was a very efficient way to get the particular object's root item.

But now I have a multiple windows application, where each window can have a different active project, and also inter-project communication that can trigger activity inside a project that is not currently active in any editor window. So it is no longer possible to use the "active project pointer" approach.

It seems that I have exhausted all options, but there might be some fancy design pattern which could give me a good compromise?

Aucun commentaire:

Enregistrer un commentaire