r/dotnet • u/god_of_tits_an_wine • 21h ago
r/dotnet • u/VladislavPrimakov • 20h ago
Benchmarking C# 15 [Union] Result, TryOut, and throw/catch (.NET 11 NativeAOT, with & without runtime-async) vs Rust
I benchmarked error-handling patterns in .NET 11 NativeAOT (C# 15 [Union] Result<int, ErrorCode>, classic TryOut, and throw/catch) against Rust (Result<i32, DomainError> and panic::catch_unwind), and also tested the impact of .NET 11's <Features>runtime-async=on</Features> on async paths. Tooling: hyperfine v2 (with hardware perf counters), .NET 11.0.100 NativeAOT (IlcInstructionSet=native, OptimizationPreference=Speed), Rust 1.100.0 (opt-level=3, lto=true, codegen-units=1, target-cpu=native, panic="unwind")
Sync (300M operations, 10% failures)
| Variant | Wall [s] | ns/op | Peak RAM [MiB] | Instructions | IPC | Cache Miss | Branch Misses | Relative |
|---|---|---|---|---|---|---|---|---|
Rust Result | 0.496 ± 0.033 | 1.65 | 1.7 | 7.23B | 3.19 | 83.2K (33.5%) | 335.1K | 1.00x |
C# TryOut | 0.525 ± 0.018 | 1.75 | 3.5 | 8.73B | 3.67 | 93.9K (35.3%) | 4.1M | 1.06x |
C# Result ([Union]) | 1.497 ± 0.015 | 4.99 | 3.5 | 12.39B | 1.77 | 111.0K (29.6%) | 4.2M | 3.02x |
C# Throw | 27.905 ± 0.146 | 93.02 | 54.4 | 414.15B | 3.21 | 47.3M (11.8%) | 44.5M | 56.21x |
Rust Panic (catch_unwind) | 40.665 ± 0.214 | 135.55 | 1.8 | 657.66B | 3.46 | 16.5M (8.5%) | 48.6M | 81.91x |
Async (80M operations, 10% failures)
| Variant | Wall [s] | ns/op | Peak RAM [MiB] | Instructions | IPC | Cache Miss | Branch Misses | Relative |
|---|---|---|---|---|---|---|---|---|
Rust AsyncResult | 0.475 ± 0.002 | 5.94 | 1.8 | 3.05B | 1.37 | 34.4K (28.4%) | 388.3K | 1.00x |
C# AsyncResult (runtime-async) | 0.512 ± 0.003 | 6.40 | 3.5 | 6.27B | 2.70 | 83.7K (35.3%) | 4.9M | 1.08x |
C# AsyncResult (no runtime-async) | 2.111 ± 0.022 | 26.39 | 3.5 | 17.08B | 1.75 | 435.9K (35.5%) | 7.8M | 4.44x |
C# AsyncThrow (runtime-async) | 8.643 ± 0.066 | 108.04 | 51.8 | 131.34B | 3.30 | 14.4M (12.5%) | 12.8M | 18.21x |
C# AsyncThrow (no runtime-async) | 22.745 ± 0.032 | 284.31 | 52.4 | 325.94B | 3.09 | 69.7M (6.3%) | 89.7M | 47.84x |
https://github.com/VladislavPrimakov/benchmarks#7-throw-vs-result-benchmark-c-nativeaot-vs-rust
r/dotnet • u/kralmoriaty • 22h ago
I built a Roslyn analyzer for .NET that enforces architecture rules and flags allocations the compiler hides
I have been maintaining Clean Architecture codebases where the dependency
rules exist only in a wiki, and they rot anyway. So I built RungerHeimdall:
a Roslyn analyzer that enforces the rules at compile time.
What it does
Architecture (8 rules): layers declared in architecture.json, dependency
direction enforced, forbidden types and namespaces, namespace-must-match-folder,
repositories/services must be interfaces, framework allow-list, no static
mutable state, endpoints cannot construct infrastructure types.
Performance (20 rules): 12 deterministic allocation rules and 8 context-aware
ones. Examples: empty params array, ToList() before foreach, string concat in a
loop, struct compared through EqualityComparer<T>.Default (boxes), foreach over
non-generic IEnumerable of value types, Task.Run inside async, async void,
blocking on Task, repeated enumeration, ValueTask awaited twice,
Task.FromResult in a loop, Array.Resize in a loop.
Adoption: heimdall-baseline.json freezes existing violations so you only see
new ones. Temporary exemptions require a reason and can expire.
Hot paths: [HotPath]/[ColdPath], plus framework entry points (controllers,
hosted services, SignalR hubs, Minimal API handlers, Azure Functions,
MassTransit consumers), loops and iterators.
Honesty section: two of my original rules (empty params, Enum.HasFlag)
measured 0 B on .NET 9 because the JIT now removes the allocation. I
downgraded both to info and published the numbers. The benchmark table is at
https://github.com/rungerheimdall/rungerheimdall/blob/main/docs/benchmarks.md
Quick try:
dotnet add package RungerHeimdall.Analyzers --version 1.1.1
heimdall init
dotnet build
Rule reference (filterable): https://rungerheimdall.github.io/rungerheimdall/rules.html
Repo: https://github.com/rungerheimdall/rungerheimdall
Package: https://www.nuget.org/packages/RungerHeimdall.Analyzers
MIT, no telemetry. I am the author. Feedback on false positives would be the
most useful thing right now — that is what kills analyzers.