perf: skip redundant work in the scene update and draw loops - #52
Merged
Merged
Conversation
Three per-frame costs in the scene loop that only pay off when something actually changed: - update compacted the object vector with erase_if every frame. Scan for a dead object first and only compact when one exists; the scan is a read-only pass that short-circuits, the compaction moves elements. - draw re-sorted every object by z-index every frame. Sort only when the vector is no longer in z-order. This also stops the unstable sort from permuting equal z-index objects between frames. - Pending objects were appended one push_back at a time. Reserve once and insert the range. Also read the clock once per frame in the managed scene loops instead of sampling it twice for the delta and again for the FPS counter.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Split out of #47.
Four per-frame costs in the scene loop that only pay off when something actually changed:
updateranerase_ifover the whole vector every frame. Scan for a dead object first and only compact when one exists — the scan is a read-only pass that short-circuits on the first hit, the compaction moves elements and reallocates.drawre-sorted every object every frame. Sort only when the vector is no longer in z-order. As a side benefit this stops the unstableranges::sortfrom permuting equal-z-index objects between frames, so draw order among ties is now stable.push_backat a time. Reserve once and insert the range.high_resolution_clocktwice for the frame delta and again for the FPS counter. One read per frame now.The original PR drove the first two off
_needs_sort/_has_dead_objectsmember flags. Both are dropped here:_has_dead_objectswas cleared before the update loop and re-set during it, so an object killed outsideupdate(duringdraw, or by external code between frames) wasn't noticed until the following update — one frame late. Because the draw loop only checksactive, notalive, that dead object got drawn for an extra frame. Theany_ofscan has the same cost class as the flag path and is exactly the original semantics._needs_sortwas redundant: it was||'d with theis_sortedcheck, which already catches every case that would set it. An insertion either breaks sortedness —is_sortedsees it — or doesn't, in which case no sort was needed.