You've got a fixed set of possible element types, so how about?
- Start by preallocating all of the elements you will ever need and storing them in an array: access the type you need using an offset into the array based on the enum. That will reduce the total number of allocations you ever need to do and give you o(1) access time to find anything you ever need.
- Back the above up by adding an initialise method to these elements, that you use in place of new in SetHUDElement
- To give you a fast way of iterating over only active elements, use either a LinkedList or write your own intrusive linked list that embeds into your elements.
- If you're going for the LinkedList approach handle your adding and removal using LinkedListNodes. You can make your RemoveHUDElement fast too, by making the LinkedListNode accessible from the HUD element: a simple null check will tell you if the item is active or not: once again o(1), getting rid of the iteration over active elements. Similar thinking applies if you roll your own list solution.
How about that?
↧