You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
*** Generated by Gemini Pro 2.5 for your convenience based on the raw benchmark data.
*Note: Benchmarks run on Windows 11, 12th Gen Intel Core i5-12600K, .NET 9.0.4, with SwitchMediator v1.13.0.
These benchmarks compare SwitchMediator (a source-generated mediator) against the popular MediatR library across various scenarios. The goal is to quantify the performance benefits offered by SwitchMediator's compile-time generation approach.
Key Observations & Conclusions
1. Startup Performance (_Startup methods)
Observation: SwitchMediator's startup is staggeringly faster than MediatR's, and the performance gap widens exponentially as the number of handlers (N) increases.
N=25:~1.6 µs (Switch) vs ~162 µs (MediatR) -> ~102x faster
N=100:~4.0 µs (Switch) vs ~1.3 ms (MediatR) -> ~335x faster
N=600:~19.9 µs (Switch) vs ~33.5 ms (MediatR) -> ~1688x faster
Conclusion: This is a primary advantage of source generation. SwitchMediator completely avoids MediatR's runtime assembly scanning, resulting in near-instantaneous startup that scales incredibly well. This is crucial for:
Applications with a large number of handlers.
Serverless functions or scenarios requiring fast cold starts.
AOT (Ahead-of-Time) compilation scenarios where reflection is problematic.
Faster developer feedback loops during testing and debugging.
Allocation: Startup allocations are also orders of magnitude lower for SwitchMediator (~147 KB vs ~17 MB at N=600 -> ~117x less), reducing memory pressure during application initialization.
2. Request/Response Dispatch (_Send methods - No Pipeline)
Observation: SwitchMediator's Send operation is consistently faster across different handler counts (N).
Performance is stable regardless of N.
Speedup is consistently ~2.4x - 3.0x faster than MediatR (e.g., ~20 ns vs ~56 ns at N=600).
Conclusion: The source-generated dispatch mechanism provides a significant, constant-time speed advantage for core request/response handling compared to MediatR's approach.
Allocation: SwitchMediator allocates 3x less memory per Send operation (96 B vs 288 B). This reduction is vital for high-throughput applications, minimizing GC pressure and improving overall efficiency. The 96 B likely represents near-optimal allocation (Task, request/response objects).
3. Notification Dispatch (_Publish methods)
Observation: SwitchMediator's Publish is significantly faster and scales well.
Consistently ~4.8x - 5.1x faster than MediatR (e.g., ~20 ns vs ~96 ns at N=600).
Conclusion: SwitchMediator uses a highly efficient, source-generated mechanism for identifying and invoking notification handlers.
Allocation: This is a major highlight: SwitchMediator achieves ZERO memory allocation for the Publish operation itself (the notification object was pre-allocated for the test). MediatR allocates 592 B per publish. This makes SwitchMediator extremely compelling for event-driven systems or scenarios with frequent notifications, as it completely avoids GC impact from the dispatch process.
Observation: SwitchMediator remains faster when executing requests through open generic pipeline behaviors, with the advantage holding as the number of behaviors (B) increases.
B=1:~541 ns vs ~788 ns -> ~1.46x faster
B=5:~1.56 µs vs ~1.95 µs -> ~1.25x faster
Conclusion: The source-generated pipeline handling in SwitchMediator introduces less overhead per step compared to MediatR's pipeline resolution and execution.
Allocation: SwitchMediator consistently allocates less memory during pipeline execution (~1.6x - 2.0x less than MediatR - e.g., 312 B vs 640 B for B=1). This reinforces the lower overhead per pipeline step and contributes to better performance in complex request flows.
Overall Conclusion
The benchmark results clearly demonstrate that SwitchMediator's source-generation strategy delivers substantial performance improvements and significant memory allocation reductions compared to MediatR across all core operations:
Startup: Orders of magnitude faster (up to ~1688x), with excellent scaling and ~117x less allocation.
Send: ~3x faster with 3x less allocation.
Publish: ~5x faster with zero allocation.
Pipelines: Faster execution (~1.25-1.45x) with lower allocation per step (~1.6-2.0x less).
SwitchMediator is positioned as the high-performance, low-allocation choice for implementing the mediator pattern in modern .NET applications, particularly beneficial in scenarios demanding fast startup times, low latency, high throughput, and minimal GC pressure.