Skip to content

perf: Optimize NetworkList indexer setter performance #3585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

harayuu9
Copy link

@harayuu9 harayuu9 commented Aug 6, 2025

This PR adds an equality check to the NetworkList<T> indexer setter, aligning its behavior with NetworkVariable<T>.Value. Since NetworkList<T> already constrains T to IEquatable<T>, this change is both safe and broadly applicable. It avoids redundant assignments when the new value is equal to the current value, which improves runtime efficiency and avoids unnecessary event dispatch and network synchronization.

Changelog

com.unity.netcode.gameobjects

  • Changed: Optimized NetworkList<T> indexer setter to skip operations when the new value equals the existing value, improving performance by avoiding unnecessary list events and network synchronization.

Testing and Documentation

  • No documentation changes or additions were necessary.

Backport

Not applicable.

…nged

Added equality check in NetworkList indexer setter to avoid unnecessary operations when the new value equals the existing value. This improves performance by preventing redundant list events and network synchronization.
@harayuu9 harayuu9 requested a review from a team as a code owner August 6, 2025 07:56
Copy link
Collaborator

@EmandM EmandM left a comment

Choose a reason for hiding this comment

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

Thanks again for the submission! This seems like a fantastic and needed optimization. I'll pull this over internally.

In case you didn't know, it's now possible to use Collections inside of NetworkVariables. So you can use NetworkVariable<CollectionType> and NGO will use network optimized deltas for the network variable updates (docs).

We're super interested if you have any feedback on the usage of NetworkList<T> vs NetworkVariable<List<T>>.

@harayuu9
Copy link
Author

harayuu9 commented Aug 6, 2025

@EmandM
I’ve looked into that, thank you. In my specific use case, I’m working with fairly large 2D maps—256x256 or even larger—so the data size is significant. Additionally, updates occur quite frequently, roughly once every 5 frames. Because of this, performance took a substantial hit when using NetworkVariable due to the cost of diffing such large structures.

Another important factor is that NetworkListEvent provides a much more convenient and flexible event-driven model. It functions similarly to ObservableCollection in that it allows me to handle changes in a fine-grained way. In my scenario, I can efficiently create, delete, or modify objects only on the specific tiles that have changed.

@EmandM
Copy link
Collaborator

EmandM commented Aug 6, 2025

Cheers for the feedback! It's great to have performance improvements that are based on real-life use-cases. We'll land this fix over in #3587

@EmandM EmandM closed this Aug 6, 2025
EmandM added a commit that referenced this pull request Aug 7, 2025
continues: #3585 

This PR adds an equality check to the `NetworkList<T>` indexer setter,
aligning its behavior with `NetworkVariable<T>.Value`. Since
`NetworkList<T>` already constrains `T` to `IEquatable<T>`, this change
is both safe and broadly applicable. It avoids redundant assignments
when the new value is equal to the current value, which improves runtime
efficiency and avoids unnecessary event dispatch and network
synchronization.

## Changelog

- Changed: Optimized `NetworkList<T>` indexer setter to skip operations
when the new value equals the existing value, improving performance by
avoiding unnecessary list events and network synchronization.

## Testing & QA

This is a very small optimization in an area of code that is deeply
covered by unit and integration tests. No manual testing is required.

## Documentation

As there are no API or behaviour changes, no documentation is required

## Backport

This is a new optimization and so doesn't need to be backported

---------

Co-authored-by: Yuto Harada <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants