Unity-Technologies / Unity-Technologies/com.unity.netcode.gameobjects
Named Messages: Don't allocate a byte array whenever you send a message
還沒有人認領這個 Issue。
- 主要語言
- C#
- 星號
- 2.3k
- 分支
- 461
- 平均合併
- 3 天 16 小時
- 30 天內合併 PR
- 20
描述
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 existingbyte[]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.StringToHashorShader.PropertyToID. However, the HashSize can be 4 bytes or 8 bytes (depending onNetworkManager.NetworkConfig.RpcHashSize) and can even change during the runtime (at least,CustomMessageManagersupports 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 theGetHashfunction 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.
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
先閱讀 Runtime/Hashing/XXHash.cs 和 Runtime/Messaging/CustomMessageManager.cs,尤其是 SendNamedMessage 的呼叫路徑,以及報告行中指出的配置。使用 profiler 重現具名訊息的配置,接著判斷如何在設定的雜湊大小變更時,同時保留 32 位元和 64 位元雜湊行為並避免這項配置。完成的標準是:具名傳送不再配置名稱位元組陣列,且現有行為仍由可用的測試涵蓋。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- csharp, unity
- 領域
- networking, performance
- Issue 類型
- 功能
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 38/100