Static/Bipartite SAP
Wednesday, September 14th, 2011Another idea for optimizing the SAP when we have a small amount of dynamic objects moving amongst a large amount of static objects:
Put the dynamics in their own vanilla SAP. Get the dynamic-vs-dynamic overlaps from there, nothing new.
Put the dynamics & statics in a special SAP (“Static SAP”, or “Bipartite SAP”) modified to only report dynamic-vs-static overlaps:
- The statics are stored in the SAP as usual (same box/endpoint structures as before)
- The dynamics however are stored in a separate array, using a new box structure.
- When the dynamic objects are inserted in the SAP, their endpoints are not inserted into the same array as for statics. They are stored directly in the new box structure, i.e. in the (dynamic) box class itself. They link to the static arrays of endpoints (their virtual position within those arrays, if they would have been inserted there as usual)
- When running “updateObject” on those dynamic objects, we move their endpoints virtually instead of for real. That is, we still look for the new sorted positions within the arrays of endpoints, but we don’t actually shift the buffers anymore. The update is a const function for the main SAP data-structure, the only thing that gets modified is the data within the dynamic box we’re updating. In other words we don’t have real “swaps” anymore. No more memmoves, thus reduced number of LHS, etc.
The drawback is that we update dynamic objects twice, once in the “static SAP”, once in the normal SAP.
Didn’t try it, don’t know about the perf, but might be interesting.