mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 11:00:32 +00:00
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:
parent
1c18743788
commit
70a532b5db
@ -79,6 +79,18 @@ protected void InitSyncObject(SyncObject syncObject)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#region Commands
|
#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)]
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||||
protected void SendCommandInternal(Type invokeClass, string cmdName, NetworkWriter writer, int channelId)
|
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,
|
netId = netId,
|
||||||
componentIndex = ComponentIndex,
|
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()
|
payload = writer.ToArray()
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -144,7 +156,7 @@ protected void SendRPCInternal(Type invokeClass, string rpcName, NetworkWriter w
|
|||||||
{
|
{
|
||||||
netId = netId,
|
netId = netId,
|
||||||
componentIndex = ComponentIndex,
|
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()
|
payload = writer.ToArray()
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -183,7 +195,7 @@ protected void SendTargetRPCInternal(NetworkConnection conn, Type invokeClass, s
|
|||||||
{
|
{
|
||||||
netId = netId,
|
netId = netId,
|
||||||
componentIndex = ComponentIndex,
|
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()
|
payload = writer.ToArray()
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -212,7 +224,7 @@ protected void SendEventInternal(Type invokeClass, string eventName, NetworkWrit
|
|||||||
{
|
{
|
||||||
netId = netId,
|
netId = netId,
|
||||||
componentIndex = ComponentIndex,
|
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()
|
payload = writer.ToArray()
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -242,7 +254,7 @@ protected class Invoker
|
|||||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||||
protected static void RegisterDelegate(Type invokeClass, string cmdName, MirrorInvokeType invokerType, CmdDelegate func)
|
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))
|
if (cmdHandlerDelegates.ContainsKey(cmdHash))
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user