Remove spurious memory allocations in TextureCacheBase::SerializeTexture and DeserializeTexture #9641
+48
−18
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.
I'm doing a project with Dolphin which wants to save state incredibly frequently (~every frame). Reallocating
texture_datainTextureCacheBase::SerializeTexturetakes up a non-negligible amount of the serialization time in my environment. For my own project I might be able to implement a check to see if textures have changed in the last frame and avoid re-serializing them, but this is general enough to save everybody some fractions of a second when they save state - maybe TASers would appreciate it?This PR fixes the issue by adding a method to
PointerWrapwhich sets aside some space for the caller to read/write/verify, then using it to set aside one large chunk of space all at once in the texture cache. This should not change the save-state format.Additionally, the new method is used when deserializing textures as well to avoid a single memory allocation/copy/deallocation, but that's of much less concern and could be removed pretty easily.
I'm not a huge fan of the method I went with here - all of the paths that hit
DoExternalneed to handle themodeof thePointerWrap, but at present there's no enforcement of that. I've personally verified that only read and write operations take place to the pointers, but that's no guard against future changes so it's pretty reasonable to sayDoExternalis going too far/breaking abstraction. If so, let me know: I may be able to change this to refactorSerializeTextureto do one allocation instead of worst-case layers * levels copies/reallocations.If save state performance is important, I've got a few other lower-impact changes which I could also try to contribute if desired.