Remove redundant texture copies in TextureCopyNode #871
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.
For each AssetEvent::Modified event, TextureCopyNode will do 1 copy from each Texture into a buffer in the render_context. Every time a system calls Assets.get_mut to retrieve a Texture from a Handle it will trigger the AssetEvent::Modified event. If you have multiple systems which need to retrieve the same texture, this causes TextureCopyNode to spend time copying the data for the same texture multiple times, once for each call to get_mut that happened for that Texture (as well as the time spent copying, it also raises the peak memory). A Gist which contains sample code which uses the same Texture from multiple systems: https://gist.github.com/rod-salazar/f45608644f042de036c09bf2565aba69
This change makes it so that we only do the copy once per texture handle.
The assumption this change makes is that we only need to synchronize between the a particular Texture's data and the render_context once since this TextureCopyNode happens after the calls to get_mut.
I ran into this scenario in a different way though. When I was writing some code which attempted to call texture.get_mut in a loop, where sometimes I would ask for the same texture multiple times. In this case, my loop caused hundreds of copies for what I thought was just a harmless lookup in the assets using get_mut.