Unity-Technologies / Unity-Technologies/com.unity.netcode.gameobjects
Named Messages: Don't allocate a byte array whenever you send a message
Personne n'a encore pris cette issue.
- Langage dominant
- C#
- Étoiles
- 2.3k
- Forks
- 461
- Merge moyen
- 3 j 16 h
- PR mergées (30 j)
- 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 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.
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par lire Runtime/Hashing/XXHash.cs et Runtime/Messaging/CustomMessageManager.cs, en particulier le chemin d’appel de SendNamedMessage et l’allocation indiquée aux lignes signalées. Reproduisez l’allocation des messages nommés avec le profiler, puis déterminez comment l’éviter tout en préservant le comportement de hachage sur 32 bits et sur 64 bits lorsque la taille de hachage configurée change. Le travail est terminé lorsque les envois nommés n’allouent plus le tableau d’octets du nom et que le comportement existant reste couvert par les tests disponibles.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- csharp, unity
- Domaine
- networking, performance
- Type d'issue
- Fonctionnalité
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 38/100