Genbox / VelcroPhysics

High performance 2D collision detection system with realistic physics responses.
MIT License
662 stars 114 forks source link

Avoid List<T> O(n) operations #18

Open Genbox opened 7 years ago

Genbox commented 7 years ago

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 are O(n) operations, which slows down the engine considerably when working with many fixtures on a body.

Genbox commented 7 years ago

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 commented 7 years ago

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