VelcroPhysics icon indicating copy to clipboard operation
VelcroPhysics copied to clipboard

Avoid List<T> O(n) operations

Open Genbox opened this issue 8 years ago • 2 comments

To simplify the code, Velcro Physics uses simple lists instead of the linked lists Box2D uses. However, operations such as Contains() and Remove() on List<T> are O(n) operations, which slows down the engine considerably when working with many fixtures on a body.

Genbox avatar Apr 12 '17 17:04 Genbox

I've created a generic doubly linked list that is circular to avoid branches in code. The enumerator simply iterates the list using the count to void infinite circulation.

It is 2x as slow when iterating, but much faster when removing nodes. We allocate 2x the memory for the linked list as well since it needs a container to hold the references.

image

Genbox avatar Jun 04 '17 15:06 Genbox

Note that I tested with a version that is non-generic and got identical results.

Genbox avatar Jun 04 '17 16:06 Genbox