bevy
bevy copied to clipboard
Avoid unnecessary copying with BufferVec::swap
Objective
prepare_skinned_meshes is repeatedly copying values from ExtractedJoints when both end up identical to each other and cleared every frame.
Solution
- Add spans for measuring the time spent writing out buffers to the GPU.
- Add
BufferVec::swapto directly replace the backingVecwith a pre-prepared one. Avoid the copy.
Performance
On many_foxes ,this halves the time spent in prepare_skinned_meshes:

Almost all of the time spent in the system is now spent writing the buffer out.

Changelog
Added: BufferVec::swap.
I think my main concern with this would be people expecting the extracted joints to be populated and their system being scheduled for after the vec was swapped and their thing doesn’t work. It’sa bit of a hidden gotcha. Is there some way to document it on the extracted joints type perhaps?
I think my main concern with this would be people expecting the extracted joints to be populated and their system being scheduled for after the vec was swapped and their thing doesn’t work. It’sa bit of a hidden gotcha. Is there some way to document it on the extracted joints type perhaps?
An alternative would be just to have a clear/append option that utilizes a faster batch memcpy of the entire thing. Or just write directly from the ExtractedJoints instead of copying it into the BufferVec first. It'd be much faster than pushing one by one. With that said, I'm not sure what kind of use case requires reading the extracted joints in prepare that isn't just skinned meshes.
Closing in favor of #6833 for the target use case.