Cysharp / MagicOnion

Unified Realtime/API framework for .NET platform and Unity.
MIT License
3.8k stars 423 forks source link

Reduce allocation on StreamingHubClient broadcast events #787

Closed mayuki closed 3 months ago

mayuki commented 3 months ago

Before

BenchmarkDotNet v0.13.12, Windows 11 (10.0.22631.3737/23H2/2023Update/SunValley3)
13th Gen Intel Core i7-13700KF, 1 CPU, 24 logical and 16 physical cores
.NET SDK 8.0.300
  [Host]   : .NET 8.0.5 (8.0.524.21615), X64 RyuJIT AVX2
  ShortRun : .NET 8.0.5 (8.0.524.21615), X64 RyuJIT AVX2

Job=ShortRun  IterationCount=3  LaunchCount=1
WarmupCount=3

| Method                                     | Mean     | Error     | StdDev   | Rank | Gen0   | Gen1   | Gen2   | Allocated |
|------------------------------------------- |---------:|----------:|---------:|-----:|-------:|-------:|-------:|----------:|
| Parameter_Zero                             | 281.8 ns |  50.35 ns |  2.76 ns |    1 |      - |      - |      - |       4 B |
| Parameter_Many                             | 354.9 ns |  40.72 ns |  2.23 ns |    2 | 0.0029 | 0.0014 |      - |      47 B |
| Parameter_Zero_With_SynchronizationContext | 397.5 ns | 710.39 ns | 38.94 ns |    3 | 0.0057 | 0.0019 |      - |      97 B |
| Parameter_Many_With_SynchronizationContext | 420.9 ns | 251.29 ns | 13.77 ns |    4 | 0.0110 | 0.0048 | 0.0014 |     156 B |

After


BenchmarkDotNet v0.13.12, Windows 11 (10.0.22631.3737/23H2/2023Update/SunValley3)
13th Gen Intel Core i7-13700KF, 1 CPU, 24 logical and 16 physical cores
.NET SDK 8.0.300
  [Host]   : .NET 8.0.5 (8.0.524.21615), X64 RyuJIT AVX2
  ShortRun : .NET 8.0.5 (8.0.524.21615), X64 RyuJIT AVX2

Job=ShortRun  IterationCount=3  LaunchCount=1
WarmupCount=3

| Method                                     | Mean     | Error     | StdDev   | Rank | Gen0   | Gen1   | Allocated |
|------------------------------------------- |---------:|----------:|---------:|-----:|-------:|-------:|----------:|
| Parameter_Zero                             | 256.8 ns |  57.96 ns |  3.18 ns |    1 |      - |      - |       2 B |
| Parameter_Many                             | 339.3 ns | 202.94 ns | 11.12 ns |    2 | 0.0029 | 0.0019 |      50 B |
| Parameter_Zero_With_SynchronizationContext | 372.1 ns | 209.20 ns | 11.47 ns |    3 | 0.0029 | 0.0010 |      49 B |
| Parameter_Many_With_SynchronizationContext | 372.8 ns | 192.16 ns | 10.53 ns |    3 | 0.0057 | 0.0010 |      91 B |