Named Messages: Don't allocate a byte array whenever you send a message
メンテナーはふだん 3 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 38/100
- issue の種類
- 機能追加
- 明瞭さ
- おおむね明確
- 活発さ
- 停滞
調査の方向性
まず Runtime/Hashing/XXHash.cs と Runtime/Messaging/CustomMessageManager.cs を読み、特に SendNamedMessage の呼び出し経路と、報告された行に記載されているアロケーションを確認します。プロファイラーを使って名前付きメッセージのアロケーションを再現し、その後、設定されたハッシュサイズが変わっても 32 ビットと 64 ビットの両方のハッシュ動作を維持しながら、それを回避する方法を判断します。名前付き送信で名前のバイト配列がアロケートされなくなり、既存の動作が利用可能なテストで引き続きカバーされていれば完了です。
索引モデルが issue の本文から書いたものです。
説明
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/[email protected]/Runtime/Hashing/XXHash.cs:218
Unity.Netcode.Runtime.dll!Unity.Netcode::CustomMessagingManager.SendNamedMessage() ./Library/PackageCache/[email protected]/Runtime/Messaging/CustomMessageManager.cs:296
Unity.Netcode.Runtime.dll!Unity.Netcode::CustomMessagingManager.SendNamedMessageToAll() ./Library/PackageCache/[email protected]/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.
- 主要言語
- C#
- スター
- 2.3k
- フォーク
- 465
- 平均マージ
- 3日 12時間
- マージ済み PR(30日)
- 39
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
Unity-Technologies/com.unity.netcode.gameobjects のほかの issue
-
TimeSyncMessage is sent reliably, so a late one resets the client clock backwards and stalls its NetworkTransforms対応中かも @reginaldalfret が 8 日前に担当しました。 オープン
難易度 4/5 3〜5日 初心者へのやさしさ 52/100
Unity-Technologies/com.unity.netcode.gameobjects#4249 ·
メンテナーはふだん 3 日以内に返信
-
NetworkTransform: half-float base update dropped as out of order when UseUnreliableDeltas is on, so later deltas decode ~2 m off対応中かも @NoelStephensUnity が 8 日前に担当しました。 オープンport:3.x-completed priority:medium stat:imported type:bug
難易度 4/5 3〜5日 初心者へのやさしさ 57/100
Unity-Technologies/com.unity.netcode.gameobjects#4248 ·
メンテナーはふだん 3 日以内に返信
-
Tracking type:feature-2.x
難易度 5/5 1週間以上 初心者へのやさしさ 35/100
Unity-Technologies/com.unity.netcode.gameobjects#3870 · コメント 5 件 ·
メンテナーはふだん 3 日以内に返信
-
Tracking type:feature
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
Unity-Technologies/com.unity.netcode.gameobjects#3830 · コメント 7 件 ·
メンテナーはふだん 3 日以内に返信
-
priority:medium stat:imported type:feature type:feature-2.x
難易度 4/5 3〜5日 初心者へのやさしさ 42/100
Unity-Technologies/com.unity.netcode.gameobjects#3706 · コメント 5 件 ·
メンテナーはふだん 3 日以内に返信
Unity-Technologies/com.unity.netcode.gameobjects の issue をすべて見る
似ている issue
-
hand-to-AI
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
CactuseSecurity/firewall-orchestrator#5803 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 3 日以内に返信
-
bug python
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
ilia-sokolov/OfficeAgent.NET#89 ·
メンテナーはふだん 13 日以内に返信