Reduce garbage allocation in SyncVar Deserialize when syncvar has no hook#4111
Open
liamcary wants to merge 1 commit intoMirrorNetworking:masterfrom
Open
Reduce garbage allocation in SyncVar Deserialize when syncvar has no hook#4111liamcary wants to merge 1 commit intoMirrorNetworking:masterfrom
liamcary wants to merge 1 commit intoMirrorNetworking:masterfrom
Conversation
Reduces 32+ bytes of garbage allocation per deserialized syncvar when syncvar has no hook.
Contributor
Author
Collaborator
|
interesting find, gonna check this soon |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.

While profiling my project I found that deserializing sync vars causes small allocations on the client each time the value changes. In my case I had a double and an int that was changing frequently, and each time they changed there was a 40 bytes and 32 byte allocation in GeneratedSyncVarDeserialize. There's no explicit memory allocation in GeneratedSyncVarDeserialize, but removing the Action<T, T> OnChanged parameter fixes the allocation, even though the NetworkBehaviour's DeserializeSyncVar method passes in null. I assume its a boxing operation on the generic delegate when checking if its null.
To fix the allocation, I implemented a NoHook alternative for each of the 4 GeneratedSyncVarDeserialize methods, then updated the NetworkBehaviourProcessor to call the NoHook method for SyncVar's that don't have a hook. Its not the cleanest implementation, and ideally the delegate could be passed in without any allocation, to reduce memory allocations when deserializing syncvars that DO have a hook.
This is a before and after screenshot of the profiler with Deep Profile enabled: