vrld / HC

General purpose collision detection library for the use with LÖVE.
http://hc.readthedocs.org/
404 stars 48 forks source link

Performance and memory improvements in GJK #51

Closed pablomayobre closed 6 years ago

pablomayobre commented 6 years ago

GJK allocates a simplex table every time it's called, and closest_edge allocates many tables in the EPA loop.

This resulted in high memory consumption as seen by @pfirsich. Trying to fix this issue we both worked on a way to make the simplex and edge tables static.

Additionally the table.insert was slowing down the iteration so it was changed for a single loop that inserts both values.

Feel free to squash the commits if needed

pfirsich commented 6 years ago

As requested by @Positive07 I will attach some data collected on the influence on memory consumption of the changes in the pull request.

The data is display in a tool of mine that collects (among other things) memory consumption using collectgarbage("count") at some key-spots marked manually in my code.

The points "get collisions" (top, middle) and "get level collisions" (right) capture the memory consumption changes from the call to HC:collisions() (these two calls are for different HC-instances) (averaged over a few seconds worth of frames):

normal static_simplex

The memory consumption increase is noticably smaller.

vrld commented 6 years ago

Looks good. Very good, indeed. Thanks for the in-depth analysis and the fix!