Associating instances

So, to implement the Focusmanager I found out, that there is another small technology needed. Basically, I need some kind of an association system to link arbitrary typed instances to the scenegraph nodes. While rendering the scenegraph, this could have multiple uses:

  • OpenGL or other scenegraph renderer need some kind of optimisation attachments, based on the scenegraph node-level. I don’t like the idea of creating inheritance hierarchies for the scene nodes to specialise them. I want them to be allocated and attached separately, and I want to attach instances based on classes not derived from the same baseclass.
  • Additionally, the Focusmanager may attach information on a per node granularity, for example to store the relevant information to interpolate quaternions.

The requirements for such a thing are:

  • Ownership: Attachments are created, bound and destroyed individually. Attachments shall be destroyed when the node is destroyed.
  • Accessibility: Attachments must be able to be queried from the node.
  • Performance: There should be no addtional overhead in allocating and binding an attachment. Querying an attachment of a specific type shall be as fast as possible.

Interesting, before writing this blog entry I was pretty sure that these requirements are some kind of a new thought, but now I see a direct similarity to the COM (common object model) and clones.

Nevertheless, the first implementation suggestion was to implement a std::list of boost::any in the node to support the attachments (thanks to Alex for pointing this out). There are two drawbacks of this solution:

  1. Every element of std::list requires an additional memory allocation.
  1. boost::any compares types by using RTTI information of C++. It compares the types by calling the == operator of the type_info record, which is implemented as a simple string compare on the gcc and cl compilers (the two I am using at the moment to compile the framework with). This comparison is pretty slow, compared with a simple pointer comparison.

So first, I will replace the std::list with the elists I have created (ooops, sorry the documentation and specification of the elists is not yet online, I am working on the last review, but the reference documentation is available from libsmart.com).

Then I need a kind of a type information for each instance needed to be attached. I don’t think I’d like to use RTTI, and so a simple per instance static entry and a templated wrapper will make the type comparisons a simple pointer compare (resulting in a ten times faster queries).

The performance characteristics of the queries are linear. I don’t think that there will be more than 5 attachments per node at any time.

This is the next step to do for my scenegraph evolution, I hopefully have some time the next days to implement it and write a paper about it.

Happy scenegraph dreams.