Skip to content

Commit 03bbfd2

Browse files
chore: optimize bitflags accessors in ParentSync (#3814)
* chore: optimize bitflags accessors in ParentSync * Update changelog
1 parent 1a16947 commit 03bbfd2

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
4040

4141
### Changed
4242

43+
- Improve performance of `ParentSyncMessage`. (#3814)
4344
- Improve performance of `DestroyObjectMessage`. (#3801)
4445
- Improve performance of `CreateObjectMessage`. (#3800)
4546
- First pass of CoreCLR engine API changes. (#3799)

com.unity.netcode.gameobjects/Runtime/Messaging/Messages/ParentSyncMessage.cs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,23 @@ internal struct ParentSyncMessage : INetworkMessage
1010

1111
public ulong NetworkObjectId;
1212

13-
private byte m_BitField;
13+
private const byte k_WorldPositionStays = 0x01;
14+
private const byte k_IsLatestParentSet = 0x02;
15+
private const byte k_RemoveParent = 0x04;
16+
private const byte k_AuthorityApplied = 0x08;
1417

15-
public bool WorldPositionStays
16-
{
17-
get => ByteUtility.GetBit(m_BitField, 0);
18-
set => ByteUtility.SetBit(ref m_BitField, 0, value);
19-
}
18+
public bool WorldPositionStays;
2019

2120
//If(Metadata.IsReparented)
22-
public bool IsLatestParentSet
23-
{
24-
get => ByteUtility.GetBit(m_BitField, 1);
25-
set => ByteUtility.SetBit(ref m_BitField, 1, value);
26-
}
21+
public bool IsLatestParentSet;
2722

2823
//If(IsLatestParentSet)
2924
public ulong? LatestParent;
3025

3126
// Is set when the parent should be removed (similar to IsReparented functionality but only for removing the parent)
32-
public bool RemoveParent
33-
{
34-
get => ByteUtility.GetBit(m_BitField, 2);
35-
set => ByteUtility.SetBit(ref m_BitField, 2, value);
36-
}
27+
public bool RemoveParent;
3728

38-
public bool AuthorityApplied
39-
{
40-
get => ByteUtility.GetBit(m_BitField, 3);
41-
set => ByteUtility.SetBit(ref m_BitField, 3, value);
42-
}
29+
public bool AuthorityApplied;
4330

4431
// These additional properties are used to synchronize clients with the current position,
4532
// rotation, and scale after parenting/de-parenting (world/local space relative). This
@@ -51,8 +38,16 @@ public bool AuthorityApplied
5138

5239
public void Serialize(FastBufferWriter writer, int targetVersion)
5340
{
41+
42+
byte bitset = 0x00;
43+
if (WorldPositionStays) { bitset |= k_WorldPositionStays; }
44+
if (IsLatestParentSet) { bitset |= k_IsLatestParentSet; }
45+
if (RemoveParent) { bitset |= k_RemoveParent; }
46+
if (AuthorityApplied) { bitset |= k_AuthorityApplied; }
47+
5448
BytePacker.WriteValueBitPacked(writer, NetworkObjectId);
55-
writer.WriteValueSafe(m_BitField);
49+
writer.WriteByteSafe(bitset);
50+
5651
if (!RemoveParent)
5752
{
5853
if (IsLatestParentSet)
@@ -72,7 +67,12 @@ public bool Deserialize(FastBufferReader reader, ref NetworkContext context, int
7267
var networkManager = (NetworkManager)context.SystemOwner;
7368

7469
ByteUnpacker.ReadValueBitPacked(reader, out NetworkObjectId);
75-
reader.ReadValueSafe(out m_BitField);
70+
reader.ReadByteSafe(out byte bitset);
71+
WorldPositionStays = (bitset & k_WorldPositionStays) != 0;
72+
IsLatestParentSet = (bitset & k_IsLatestParentSet) != 0;
73+
RemoveParent = (bitset & k_RemoveParent) != 0;
74+
AuthorityApplied = (bitset & k_AuthorityApplied) != 0;
75+
7676
if (!RemoveParent)
7777
{
7878
if (IsLatestParentSet)

0 commit comments

Comments
 (0)