perf: Improved performance for large number of NetworkObjects#1951
perf: Improved performance for large number of NetworkObjects#1951starchitectus wants to merge 1 commit intoUnity-Technologies:developfrom Space-Base-Place:develop
Conversation
|
In
|
|
even though this sounds and looks like a bold change, it actually makes quite a lot of sense to me. |
davidvogelunity
left a comment
There was a problem hiding this comment.
I don't work on network stuff normally, but I stumbled on this perf problem as well and came up with an independent solution. Your code is definitely more performant.
| public virtual void SetDirty(bool isDirty) | ||
| { | ||
| m_IsDirty = isDirty; | ||
| m_NetworkBehaviour.NetworkManager.MarkNetworkObjectDirty(m_NetworkBehaviour.NetworkObject); |
There was a problem hiding this comment.
In testing on my project, it's possible for this to be null, likely this gets called before Initialize(). Probably worth checking for dirty in Initialize() too.
While there aren't any cases with false now, it might be worth checking too.
| { | ||
| if (sobj.IsNetworkVisibleTo(client.ClientId)) | ||
| var client = networkManager.ConnectedClientsList[i]; | ||
| if (networkManager.IsHost && client.ClientId == networkManager.LocalClientId) |
There was a problem hiding this comment.
Is this safe? The perf benefit is negligible and there's stuff happening in PreNetworkVariableWrite(), but I haven't dug really deep into what it's doing.
There was a problem hiding this comment.
This is just skipping the update for the host client since it will already be applied locally.
- Full credit to Unity-Technologies#1951
|
Nice PR! Trying to have it work with our test setup required a bit of tweaks. I've opened: #2116 which integrates this plus other changes so that it doesn't break our tests. OK if I close this and we migrate the discussion over there? |
Thanks Jeff! Great to see this gaining traction. Ok to close this one. |
Presently every NetworkObject is checked for each client every tick to see if it has dirty NetworkVariables that require syncing. This causes poor performance with large numbers of NetworkObjects as they all are checked multiple times per frame regardless of whether or not they have changed.
This PR changes the behaviour of NetworkVariables such that they register themselves with the NetworkManager for updating when they are marked dirty so there are no redundant checks.