r/dotnet • • 23h ago

I built a Roslyn analyzer for .NET that enforces architecture rules and flags allocations the compiler hides

0 Upvotes

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.


r/dotnet • • 20h ago

Benchmarking C# 15 [Union] Result, TryOut, and throw/catch (.NET 11 NativeAOT, with & without runtime-async) vs Rust

3 Upvotes

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)

VariantWall [s]ns/opPeak RAM [MiB]InstructionsIPCCache MissBranch MissesRelative
Rust Result0.496 ± 0.0331.651.77.23B3.1983.2K (33.5%)335.1K1.00x
C# TryOut0.525 ± 0.0181.753.58.73B3.6793.9K (35.3%)4.1M1.06x
C# Result ([Union])1.497 ± 0.0154.993.512.39B1.77111.0K (29.6%)4.2M3.02x
C# Throw27.905 ± 0.14693.0254.4414.15B3.2147.3M (11.8%)44.5M56.21x
Rust Panic (catch_unwind)40.665 ± 0.214135.551.8657.66B3.4616.5M (8.5%)48.6M81.91x

Async (80M operations, 10% failures)

VariantWall [s]ns/opPeak RAM [MiB]InstructionsIPCCache MissBranch MissesRelative
Rust AsyncResult0.475 ± 0.0025.941.83.05B1.3734.4K (28.4%)388.3K1.00x
C# AsyncResult (runtime-async)0.512 ± 0.0036.403.56.27B2.7083.7K (35.3%)4.9M1.08x
C# AsyncResult (no runtime-async)2.111 ± 0.02226.393.517.08B1.75435.9K (35.5%)7.8M4.44x
C# AsyncThrow (runtime-async)8.643 ± 0.066108.0451.8131.34B3.3014.4M (12.5%)12.8M18.21x
C# AsyncThrow (no runtime-async)22.745 ± 0.032284.3152.4325.94B3.0969.7M (6.3%)89.7M47.84x

https://github.com/VladislavPrimakov/benchmarks#7-throw-vs-result-benchmark-c-nativeaot-vs-rust


r/dotnet • • 22h ago

Introducing DocumentExtractionClient (preview): Extract documents in .NET

Thumbnail devblogs.microsoft.com
82 Upvotes