bevy icon indicating copy to clipboard operation
bevy copied to clipboard

Avoid unnecessary copying with BufferVec::swap

Open james7132 opened this issue 3 years ago • 2 comments

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::swap to directly replace the backing Vec with a pre-prepared one. Avoid the copy.

Performance

On many_foxes ,this halves the time spent in prepare_skinned_meshes:

image

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

image


Changelog

Added: BufferVec::swap.

james7132 avatar Jun 02 '22 16:06 james7132

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?

superdump avatar Jun 18 '22 02:06 superdump

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.

james7132 avatar Jun 24 '22 22:06 james7132

Closing in favor of #6833 for the target use case.

james7132 avatar Dec 03 '22 06:12 james7132