perf: eliminate string concat during remote method calls (#908)

* perf: eliminate string concat during remote method calls

* perf: params causes an array allocation

* refactor: simpler method hash calculation

* Update NetworkBehaviour.cs
This commit is contained in:
Paul Pacheco 2019-06-17 03:00:51 -05:00 committed by vis2k
parent 1c18743788
commit 70a532b5db

View File

@ -79,6 +79,18 @@ protected void InitSyncObject(SyncObject syncObject)
}
#region Commands
private static int GetMethodHash(Type invokeClass, string methodName)
{
// (invokeClass + ":" + cmdName).GetStableHashCode() would cause allocations.
// so hash1 + hash2 is better.
unchecked
{
int hash = invokeClass.FullName.GetStableHashCode();
return hash * 503 + methodName.GetStableHashCode();
}
}
[EditorBrowsable(EditorBrowsableState.Never)]
protected void SendCommandInternal(Type invokeClass, string cmdName, NetworkWriter writer, int channelId)
{
@ -108,7 +120,7 @@ protected void SendCommandInternal(Type invokeClass, string cmdName, NetworkWrit
{
netId = netId,
componentIndex = ComponentIndex,
functionHash = (invokeClass + ":" + cmdName).GetStableHashCode(), // type+func so Inventory.RpcUse != Equipment.RpcUse
functionHash = GetMethodHash(invokeClass, cmdName), // type+func so Inventory.RpcUse != Equipment.RpcUse
payload = writer.ToArray()
};
@ -144,7 +156,7 @@ protected void SendRPCInternal(Type invokeClass, string rpcName, NetworkWriter w
{
netId = netId,
componentIndex = ComponentIndex,
functionHash = (invokeClass + ":" + rpcName).GetStableHashCode(), // type+func so Inventory.RpcUse != Equipment.RpcUse
functionHash = GetMethodHash(invokeClass, rpcName), // type+func so Inventory.RpcUse != Equipment.RpcUse
payload = writer.ToArray()
};
@ -183,7 +195,7 @@ protected void SendTargetRPCInternal(NetworkConnection conn, Type invokeClass, s
{
netId = netId,
componentIndex = ComponentIndex,
functionHash = (invokeClass + ":" + rpcName).GetStableHashCode(), // type+func so Inventory.RpcUse != Equipment.RpcUse
functionHash = GetMethodHash(invokeClass, rpcName), // type+func so Inventory.RpcUse != Equipment.RpcUse
payload = writer.ToArray()
};
@ -212,7 +224,7 @@ protected void SendEventInternal(Type invokeClass, string eventName, NetworkWrit
{
netId = netId,
componentIndex = ComponentIndex,
functionHash = (invokeClass + ":" + eventName).GetStableHashCode(), // type+func so Inventory.RpcUse != Equipment.RpcUse
functionHash = GetMethodHash(invokeClass, eventName), // type+func so Inventory.RpcUse != Equipment.RpcUse
payload = writer.ToArray()
};
@ -242,7 +254,7 @@ protected class Invoker
[EditorBrowsable(EditorBrowsableState.Never)]
protected static void RegisterDelegate(Type invokeClass, string cmdName, MirrorInvokeType invokerType, CmdDelegate func)
{
int cmdHash = (invokeClass + ":" + cmdName).GetStableHashCode(); // type+func so Inventory.RpcUse != Equipment.RpcUse
int cmdHash = GetMethodHash(invokeClass, cmdName); // type+func so Inventory.RpcUse != Equipment.RpcUse
if (cmdHandlerDelegates.ContainsKey(cmdHash))
{