Allow monomorphization for reducers and more - #5610
Draft
lisandroct wants to merge 2 commits into
Draft
Conversation
lisandroct
marked this pull request as draft
July 28, 2026 19:38
lisandroct
force-pushed
the
lisandro/allow-monomorphization-for-reducers-and-more
branch
from
August 13, 2026 15:05
d6c0f77 to
dc5c78d
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description of Changes
This PR updates the C# bindings codegen/runtime so generated module exports can dispatch directly to statically-known generic reducer, procedure, HTTP handler, view, and anonymous-view entrypoints when building with Native AOT-LLVM.
The runtime now keeps generic static caches for generated dispatchers and exposes generic call paths that avoid relying only on indexed interface collections. The generated FFI forwarding methods switch on the host-provided function/view id and call the matching generic runtime entrypoint, while preserving the existing id-based fallback path.
For NativeAOT, the previous exported entrypoints always flowed through non-generic runtime methods such as
Module.__call_reducer__(id, ...), which then indexed into anIReducerlist and invoked through the interface. That shape hides the concrete generated reducer/handler/view type from the AOT compiler at the call site.The generated exports now switch on the function id in generated code and call generic runtime methods such as
Module.__call_reducer__<SomeReducer>(...). Because the concrete generated type is present as a generic type argument, NativeAOT can compile a specialized instantiation for that reducer/handler/view. In practice, this gives the AOT compiler a monomorphic call path: it can see the exact dispatcher type, use the static generic cache for that type, and avoid part of the dynamic interface/list dispatch shape that was previously required.All of this gives extra optimization opportunities in AOT output because the compiler no longer has to treat every reducer invocation as the same opaque interface call.
Generated code snapshots were updated for the new sealed dispatcher classes and AOT forwarding shape.
API and ABI breaking changes
No public API or module ABI breaking changes. The change is internal to C# generated code and runtime dispatch behavior.
Expected complexity level and risk
3
The main risk is that generated dispatch ids must stay aligned with registration order for reducers, procedures, HTTP handlers, named views, and anonymous views. The implementation preserves fallback id-based dispatch, but it touches the C# module FFI path used by NativeAOT/.NET 10 builds, so reviewers should pay close attention to ordering and parity across all generated dispatcher categories.
Testing
masterBenchmark Results
Benchmarks were run through the existing harness using C#
stdb_module/csharpNativeAOT/.NET 10 module builds.Each row below reports the median across three runs per branch. The range column is the min/max spread across those three runs, so it gives a rough estimate of run-to-run noise.
Overall, the PR appears to reduce spread in most of the reducer-heavy special benchmarks. The clearest improvements are:
print_bulk lines=100: about 5.5% fasterprint_bulk lines=1000: about 2.6% fasterinsert u32/u64/str btree: about 6.7% fasterprint_bulk lines=1: about 3.5% faster, but master had very high spread on this benchmarkMost game, filter, and iterate workloads are effectively flat. These are dominated more by database/query work than by reducer dispatch overhead.
The main negative result in the three-run aggregate was
insert u32/u64/u64 unique, which showed +7.7%. I investigated it separately with five focused runs of only that benchmark:The focused rerun does not confirm a meaningful regression. The best estimate is roughly flat, possibly +1-2.5%, while master’s own focused run spread was about 6.1%.