Rendering: Pre-allocate light vectors in scene culling#114329
Rendering: Pre-allocate light vectors in scene culling#114329kellemar wants to merge 2 commits intogodotengine:masterfrom
Conversation
Reserve capacity for directional_lights and lights_with_shadow vectors before iterating over scenario lights. This avoids potential reallocations during the rendering loop when pushing light instances.
clayjohn
left a comment
There was a problem hiding this comment.
I would also move the arrays to be member arrays of the class so they can be reused across frames and we can avoid making new allocations each frame.
In general we discourage making performance PRs without any profiling numbers (https://contributing.godotengine.org/en/latest/engine/guidelines/optimization.html). However, in this case it is acceptable since this also aligns with our goal of having 0 allocations per frame.
That's why I suggest taking this a step further and actually eliminating the per-frame allocations.
|
Just pushed an update for this. Moved them to class members for re-use across the frames. |
blueskythlikesclouds
left a comment
There was a problem hiding this comment.
This PR has no behavioral difference compared to before. CoW Vector's clear method frees the internal capacity. LocalVector should be used instead.
While looking through the rendering code, I noticed that
directional_lightsandlights_with_shadowvectors in_render_sceneare built up withpush_back()without reserving capacity first. Since we already know the upper bound fromscenario->directional_lights.size(), we can avoid the reallocations.It's a small thing, but this runs every frame during scene rendering so it adds up.
Changes
reserve()fordirectional_lightsbefore the loopreserve()forlights_with_shadowinside the directional lights blockBoth use
scenario->directional_lights.size()as the capacity hint, which is the maximum number of elements either vector can have.