Unity-Technologies / Unity-Technologies/com.unity.netcode.gameobjects

Named Messages: Don't allocate a byte array whenever you send a message

Open
#2,779 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

type:feature
Dominant language
C#
Stars
2.3k
Forks
461
Avg merge
3d 16h
Merged PRs (30d)
20

Description

Is your feature request related to a problem? Please describe.
Named Messages are a great way to send custom messages that are not tied to a specific game object and/or are more dynamic than standard RPC calls. However, the only way to send these messages (via CustomMessagingManager.SendNamedMessage) is by providing a string which gets then hashed using XXHash.Hash32 or XXHash.Hash64 which then calls Encoding.UTF8.GetBytes(text). This function always allocates a new byte array, containing the string. As far as I can see, there is no other possible way to send a named message.

Describe the solution you'd like
I see two possible solutions:

  • Don't use Encoding.UTF8.GetBytes(text) and instead use one of the overloads where you can specify an existing byte[] or Span. This would allow the use of a pre-alloced buffer (e.g. from a temp alloced NativeArray or an array pool).
  • Add a function overload where I can provide the hash myself (and provide a way to obtain this hash). This would work similar to e.g. Animator.StringToHash or Shader.PropertyToID. However, the HashSize can be 4 bytes or 8 bytes (depending on NetworkManager.NetworkConfig.RpcHashSize) and can even change during the runtime (at least, CustomMessageManager supports this case). Therefore it might be a bit confusing to the user which hash (with which data type) they have to use in this case - unless the GetHash function returns a struct which contains both hashes...which might be a bit weird as well.

Describe alternatives you've considered

  • I can use unnamed messages and just re-implement the logic from named messages, using the solution described above.

Additional context

Example GC Alloc from the profiler:

Size: 41

Call Stack:
mscorlib.dll!System.Text::Encoding.GetBytes()
Unity.Netcode.Runtime.dll!Unity.Netcode::XXHash.Hash32()	./Library/PackageCache/com.unity.netcode.gameobjects@1.5.2/Runtime/Hashing/XXHash.cs:218
Unity.Netcode.Runtime.dll!Unity.Netcode::CustomMessagingManager.SendNamedMessage()	./Library/PackageCache/com.unity.netcode.gameobjects@1.5.2/Runtime/Messaging/CustomMessageManager.cs:296
Unity.Netcode.Runtime.dll!Unity.Netcode::CustomMessagingManager.SendNamedMessageToAll()	./Library/PackageCache/com.unity.netcode.gameobjects@1.5.2/Runtime/Messaging/CustomMessageManager.cs:236

I wouldn't say this is a huge problem (though it depends on how many messages are being sent and how long their name is), and unnamed messages are an easy workaround. However, most of this package seems to focus on allocating as little as possible, and I think that fixing this problem here shouldn't be too hard (otherwise just ignore this issue).

Thanks for your work on this amazing package, I really enjoy using it! I can also provide a PR with a fix mentioned above if you would like me to, I just didn't want to submit something unwanted.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading Runtime/Hashing/XXHash.cs and Runtime/Messaging/CustomMessageManager.cs, especially the SendNamedMessage call path and the allocation noted at the reported lines. Reproduce the named-message allocation with the profiler, then determine how to avoid it while preserving both 32-bit and 64-bit hash behavior when the configured hash size changes. Done means named sends no longer allocate the name byte array and existing behavior remains covered by the available tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, unity
Domain
networking, performance
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.