Title: Named Messages: Don't allocate a byte array whenever you send a message · Issue #2779 · Unity-Technologies/com.unity.netcode.gameobjects · GitHub
Open Graph Title: Named Messages: Don't allocate a byte array whenever you send a message · Issue #2779 · Unity-Technologies/com.unity.netcode.gameobjects
X Title: Named Messages: Don't allocate a byte array whenever you send a message · Issue #2779 · Unity-Technologies/com.unity.netcode.gameobjects
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 s...
Open Graph 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 standa...
X 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 standa...
Opengraph URL: https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/issues/2779
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Named Messages: Don't allocate a byte array whenever you send a message","articleBody":"**Is your feature request related to a problem? Please describe.**\r\nNamed 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.\r\n\r\n**Describe the solution you'd like**\r\nI see two possible solutions:\r\n* 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).\r\n* 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.\r\n\r\n**Describe alternatives you've considered**\r\n* I can use unnamed messages and just re-implement the logic from named messages, using the solution described above.\r\n\r\n**Additional context**\r\n\r\nExample GC Alloc from the profiler:\r\n```\r\nSize: 41\r\n\r\nCall Stack:\r\nmscorlib.dll!System.Text::Encoding.GetBytes()\r\nUnity.Netcode.Runtime.dll!Unity.Netcode::XXHash.Hash32()\t./Library/PackageCache/com.unity.netcode.gameobjects@1.5.2/Runtime/Hashing/XXHash.cs:218\r\nUnity.Netcode.Runtime.dll!Unity.Netcode::CustomMessagingManager.SendNamedMessage()\t./Library/PackageCache/com.unity.netcode.gameobjects@1.5.2/Runtime/Messaging/CustomMessageManager.cs:296\r\nUnity.Netcode.Runtime.dll!Unity.Netcode::CustomMessagingManager.SendNamedMessageToAll()\t./Library/PackageCache/com.unity.netcode.gameobjects@1.5.2/Runtime/Messaging/CustomMessageManager.cs:236\r\n```\r\n\r\nI 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).\r\n\r\nThanks 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.","author":{"url":"https://github.com/Valerionn","@type":"Person","name":"Valerionn"},"datePublished":"2023-11-28T05:02:47Z","url":"https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/issues/2779"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:a9bbac6e-d649-ae82-b72f-785080bafb3e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BD4A:5537E:2675C18:3301772:6B0057EC |
| html-safe-nonce | ad97d88774c4c9eefb28e449db9e24a746cd8554ab9eacfe0e2e20427a9b6607 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRDRBOjU1MzdFOjI2NzVDMTg6MzMwMTc3Mjo2QjAwNTdFQyIsInZpc2l0b3JfaWQiOiI1MTgyMTg2MjI0NDc5MzM0MjAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 641f9264ee344db4c303e9f4761cda2035aab43ec7531d72a6e97be5d6506be1 |
| hovercard-subject-tag | issue:2013650055 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/Unity-Technologies/com.unity.netcode.gameobjects/2779/issue_layout |
| twitter:image | https://opengraph.githubassets.com/99cbac00d18e41af4d0e27fc9992418da8b3e48c9edc5d237d61a5b0055d4586/Unity-Technologies/com.unity.netcode.gameobjects/issues/2779 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/99cbac00d18e41af4d0e27fc9992418da8b3e48c9edc5d237d61a5b0055d4586/Unity-Technologies/com.unity.netcode.gameobjects/issues/2779 |
| og:image:alt | 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 standa... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Valerionn |
| hostname | github.com |
| expected-hostname | github.com |
| None | f420f4996e6b1ff1ccd6d7683f8134f4b5f0f8a50124c210dbfe7490e5644abb |
| turbo-cache-control | no-preview |
| go-import | github.com/Unity-Technologies/com.unity.netcode.gameobjects git https://github.com/Unity-Technologies/com.unity.netcode.gameobjects.git |
| octolytics-dimension-user_id | 426196 |
| octolytics-dimension-user_login | Unity-Technologies |
| octolytics-dimension-repository_id | 116385203 |
| octolytics-dimension-repository_nwo | Unity-Technologies/com.unity.netcode.gameobjects |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 116385203 |
| octolytics-dimension-repository_network_root_nwo | Unity-Technologies/com.unity.netcode.gameobjects |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 26e00df917aba0f128135dc073ea0c5e33a70d40 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width