-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Minimize small allocations by dropping the tick Vecs from Resources #11226
Minimize small allocations by dropping the tick Vecs from Resources #11226
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change makes sense to me, the unsafe
is straightforward, and the docs are good. Curious to see if this measurably changes performance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, this could be surprisingly impactful.
Co-authored-by: Joseph <21144246+JoJoJet@users.noreply.github.com>
I'll run a full trace on these later to see if there's any tangible impact on rendering, but I ran a quick sanity check on the assembly output, and the removal of a bunch of allocation related machinery in resource insertion seems promising: james7132/bevy_asm_tests@main...11226-minimize-resource-allocations#diff-f4d513f091332848b18110a4e97729c2d725fef55a135820bc0a0e8cc56b88a7 |
Objective
Column
unconditionally requires three separate allocations: one for the data, and two for the tick Vecs. The tick Vecs aren't really needed for Resources, so we're allocating a bunch of one-element Vecs, and it costs two extra dereferences when fetching/inserting/removing resources.Solution
Drop one level lower in
ResourceData
and directly store aBlobVec
and twoUnsafeCell<Tick>
s. This should significantly shrinkResourceData
(exchanging 6 usizes for 2 u32s), removes the need to dereference two separate ticks when inserting/removing/fetching resources, and can significantly decrease the number of small allocations the ECS makes by default.This tentatively might have a non-insignificant impact on the CPU cost for rendering since we're constantly fetching resources in draw functions, depending on how aggressively inlined the functions are.
This requires reimplementing some of the unsafe functions that
Column
wraps, but it also allows us to delete a few Column APIs that were only used for Resources, so the total amount of unsafe we're maintaining shouldn't change significantly.