Skip to content

Commit 9f7a7a8

Browse files
EmandMharayuu9
andauthored
chore: Improve performance of NetworkList set operation (#3587)
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]>
1 parent 5dc5435 commit 9f7a7a8

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Additional documentation and release notes are available at [Multiplayer Documen
1919

2020
### Changed
2121

22+
- 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. (#3587)
23+
2224

2325
## [2.5.0] - 2025-08-01
2426

com.unity.netcode.gameobjects/Runtime/NetworkVariable/Collections/NetworkList.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,13 @@ public T this[int index]
617617
}
618618

619619
var previousValue = m_List[index];
620+
621+
// Only trigger an event if the value has changed
622+
if (NetworkVariableSerialization<T>.AreEqual(ref previousValue, ref value))
623+
{
624+
return;
625+
}
626+
620627
m_List[index] = value;
621628

622629
var listEvent = new NetworkListEvent<T>()

0 commit comments

Comments
 (0)