Skip to content

perf: Improved performance for large number of NetworkObjects#1951

Closed
starchitectus wants to merge 1 commit intoUnity-Technologies:developfrom
Space-Base-Place:develop
Closed

perf: Improved performance for large number of NetworkObjects#1951
starchitectus wants to merge 1 commit intoUnity-Technologies:developfrom
Space-Base-Place:develop

Conversation

@starchitectus
Copy link
Copy Markdown
Contributor

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.

@starchitectus starchitectus requested a review from a team as a code owner May 9, 2022 22:30
@unity-cla-assistant
Copy link
Copy Markdown

unity-cla-assistant commented May 9, 2022

CLA assistant check
All committers have signed the CLA.

@PyrateAkananto
Copy link
Copy Markdown

In com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviourUpdater.cs there is a tiny local variable name inconsistency:

foreach (var dirtyObj in m_DirtyNetworkObjects)
vs
foreach (var sobj in m_DirtyNetworkObjects)
(old local variable name)

@0xFA11
Copy link
Copy Markdown
Contributor

0xFA11 commented May 24, 2022

even though this sounds and looks like a bold change, it actually makes quite a lot of sense to me.
perhaps, we might consider this as an optimization task/goal some time @Unity-Technologies/multiplayer-sdk

Copy link
Copy Markdown

@davidvogelunity davidvogelunity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Copy Markdown

@davidvogelunity davidvogelunity Jun 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just skipping the update for the host client since it will already be applied locally.

davidvogelunity added a commit to davidvogelunity/com.unity.netcode.gameobjects that referenced this pull request Jun 7, 2022
@JesseOlmer JesseOlmer added stat:awaiting-triage Status - Awaiting triage from the Netcode team. stat:import Status - Issue is going to be saved internally labels Aug 9, 2022
@jeffreyrainy
Copy link
Copy Markdown
Contributor

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?

@starchitectus
Copy link
Copy Markdown
Contributor Author

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.

@michalChrobot michalChrobot removed the stat:awaiting-triage Status - Awaiting triage from the Netcode team. label Jan 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stat:import Status - Issue is going to be saved internally

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants