At Google I/O and in the forums, it has been clear that if you need
optimal performance, you must avoid memory allocation inside tight
loops.  I'm curious about the best way to handle this in my use-case,
which I think must be fairly common.  I need to access at most n
objects of type MyObject, I need to iterate over them during every
screen refresh,  and I need to remove them and put them back in the
object pool randomly.

The object pool seems pretty straight-forward, just use a
Vector<MyObject>(n), add n copies of MyObject at bootup, and add/
remove from the end to avoid any memcpy or allocation.

The iterating is a little more difficult.  I can use for(MyObject m :
myObjects), but this will allocate memory to create the iterator.  I
could create a static final Iterator at bootup, but there is no good
way to get back to the start of the List.  For now, I'm using another
Vector for active MyObjects, and a for loop with an int to get() the
objects and avoid allocating an iterator.  get() runs in constant time
for a Vector, so this works well.

However, when an object is done, I need to remove it and send it back
to the pool.  If I use Vector.remove(i), it has to memcpy everything
after i.  I could use an Iterator on a LinkedList, which can remove()
in constant time, but I don't think there is a good way to get the
Iterator back to the start of the list without allocating memory.  I
could use an int to loop through a LinkedList, but LinkedList.get(n)
runs in O(n) time.

Any tips on the optimal way to solve this?  Worst-case, I assume the
memcpy called during Vector.remove() is a better bet over allocating
an Iterator inside my app loop, correct?

Thanks!
Brad
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to