perf: Insertion-sort the sweep broadphase and swap-remove its active list - #3983
perf: Insertion-sort the sweep broadphase and swap-remove its active list#3983spydon wants to merge 1 commit into
Conversation
eacf83f to
ef353a5
Compare
| // Between two ticks the hitboxes only move a little, so [items] is | ||
| // always nearly sorted: an insertion sort runs in close to linear time | ||
| // here, where a general-purpose sort would pay its full O(n log n) on | ||
| // every tick. This also avoids allocating a comparator closure per tick. |
There was a problem hiding this comment.
I'd much rather we extracted it to a commons sort util, I know we'd still get the tear-off, but the main benefit is the sort algorithm change anyway? maybe we can use the prefer-inline pragma? or how about creating our own simple data structure class instead of List that wraps the sort and the remove, the comparator can be set at list creator level, no per-tick tear-off
There was a problem hiding this comment.
honestly compared to the actual sorting operation (even in the linear case) the comparator tear-off (once per tick) does not feel significant
There was a problem hiding this comment.
actually I think the tearoff trick from your other PRs will pair really nicely hear, you don't need the closure at all!
like int compareAabbs(aabb1, aabb2) => ...
and whateverSort(list, compareAabbs)
Description
Sweep.updatere-sorted its items with a fullList.sortand a closure comparator every tick, although the list is nearly sorted between ticks (hitboxes only move a little per frame); an insertion sort makes that near-linear.Sweep.queryalso pruned its active list with a searchingList.remove; since active-list order does not matter, that is now an O(1) swap-remove.Extracted from #3960 so the data-structure change there stands alone (as requested in this comment). Stacked on #3982.
Checklist
docsand added dartdoc comments with///.examplesordocs.Breaking Change?
Related Issues
Relates to #3957