Reduce allocations in NetGetData hot paths#271
Reduce allocations in NetGetData hot paths#271Xekep wants to merge 1 commit intoPryaxis:general-develfrom
Conversation
sgkoishi
left a comment
There was a problem hiding this comment.
Please use real profiling data rather than crafted benchmarks that don't reflect the actual use case. Most if not all of these PRs are examples of premature optimization.
| return; | ||
| } | ||
| if (!Enum.IsDefined(typeof(PacketTypes), (int)e.PacketId)) | ||
| if ((uint)e.PacketId >= knownPacketIds.Length || !knownPacketIds.Get(e.PacketId)) |
There was a problem hiding this comment.
Enum.IsDefined itself takes <2ns and replacing it with BitArray won't make a difference (especially when this snippet is executed <100 times per second)
| SHA512 shaM = new SHA512Managed(); | ||
| var result = shaM.ComputeHash(uuid); | ||
| Netplay.Clients[buffer.whoAmI].ClientUUID = result.Aggregate("", (s, b) => s + b.ToString("X2")); | ||
| Netplay.Clients[buffer.whoAmI].ClientUUID = Convert.ToHexString(SHA512.HashData(uuidBytes)); |
There was a problem hiding this comment.
This is the only line worth merging that actually improves (in terms of readability). That said, although the current version is an anti-pattern, the performance 'improvement' 0.331M ops/s -> 1.805M ops/s is still irrelevant: we'll never even get close to 0.00001M ops/s.
|
@sgkoishi I'm thinking there's a non-zero chance he's optimising from real-world attacks on his server he witnessed - with running a large server of his own and all |
This PR reduces allocations in a few common
NetGetDatapaths without changing the public API.Changes:
Enum.IsDefinedinNetHooks.OnReceiveDatawith a cached packet lookupGetDataEventArgsallocation when there are noNetGetDatahandlersClientUUIDWhy:
NetGetDatais on a very hot path. The current code does extra allocations and repeated enum checks even when the packet is simple or when no plugin handlesNetGetData.Measurements:
ClientUUID: 0.331M ops/s -> 1.805M ops/s (5.45xfaster)ClientUUIDallocations: 35.137 GB -> 1.319 GB over 3,000,000 iterationsNetGetDatahandlers: 23.248M ops/s -> 77.314M ops/s (3.33xfaster)Validation:
dotnet build TSAPI.sln -c Releasedotnet test TSAPI.sln -c Release --no-build --filter FullyQualifiedName!~Benchmarks