The problem with the Container class is that it is a linked list. While it is not subject to fragmentation, like Array, it is extremely fast for linear operations, and it gracefully handles the removal of items while it's being iterated, it absolutely blows when doing lookups. A lookup in a linked list is an O(n), or linear, operation which is bad, bad, bad on large collections. Particles are an example of a very large, very active collection which needs a lot of lookups.
I remembered something from my earlier days of doing game programming with the Torque engine. They use a red-black tree for their memory management. It is a nicely balanced tree with O(log n) lookups. I read something, earlier today, that said looking up an item in a red-black tree with 4 billion entries is at most 32 operations. So, I found a nice implementation of a red-black tree in Java and did a port to JavaScript. Unfortunately, it doesn't implement the remove() operation... I've found a decent example of the operation, but I've got to translate it as well.
What I'm hoping to accomplish is that a new subclass of Container will use a backing red-black tree for speedy lookups. I think this will offer the best of both worlds when trying to keep things relatively fast. It will mean more data overhead, but then again I don't care as much about a small footprint -- only that the engine perform well.
No comments:
Post a Comment