Repository navigation
Preserve runtime expression ordering for pruned spill syntax - #665
jogibear9988 wants to merge 2 commits into
Conversation
Runtime expression invocation must follow System’s structural stack-spill ordering even when FEC prunes a dead try or goto branch. Inspect argument syntax without reducing opaque extension nodes, and defer the implicit Compile call where required. Tests compare result, exception and side-effect order for valid, null and invalid receivers, including dead conditional branches.
|
The current implementation needs reworking before merge. It adds a recursive ExpressionVisitor after normal collection, so selected argument subtrees are walked again, and it introduces another depth-dependent stack risk. FEC already uses loops to avoid recursion along many expression chains. The underlying ordering bug is real: System considers Try/Goto syntax even in branches FEC prunes, which can change whether argument side effects happen before a null/invalid runtime expression throws during its implicit Compile call. The regression tests should stay. I am replacing the visitor with structural spill tracking during existing collection, plus an iterative syntax-only scan of discarded branches. Live argument subtrees should not be walked twice; dead syntax must not register bindings, emit exception regions, or reduce extension nodes. I will test both expression APIs and report fresh compilation/runtime timings and allocations against the current patch and master. The previous +128 B result includes spill setup as well as inspection, so removing the visitor alone does not guarantee the entire cost disappears. |
Record structural spill information during normal argument collection and inspect only discarded conditional/switch syntax with an iterative cursor stack. Preserve lambda/quote boundaries and keep emitted region metadata separate. Use child cursors and an inline buffer to avoid wide sibling queues, advance unary chains in place, and bypass the cursor frame for simple discarded nodes. Keep the original ordering regressions and add deep/wide syntax, initializer, boundary and extension coverage. Validation: full Release build; Standard and LightExpression suites on net8.0, net10.0 and net472; fresh compile/runtime/allocation comparisons against master and the previous PR head. Shared delegates retain identical emitted IL.
|
Reworked and pushed in e95d0ff. The recursive visitor and the second walk of live arguments are gone. Normal collection tracks spill-relevant discarded conditional/switch syntax; an iterative cursor stack inspects only that discarded syntax. Child cursors avoid allocating a queue proportional to sibling count, unary chains advance in place, and simple nodes bypass the cursor-frame method. Bindings and emitted exception/label metadata are not collected from discarded syntax. The original tests remain. Added execution/order coverage includes 50,000-node discarded chains, 1,000-expression discarded blocks, nested invocations, lambda/quote boundaries, live versus discarded extensions, nested switches and member/list initializers. Full Release build and both expression APIs pass on net8.0, net10.0 and net472. Fresh comparisons against master and the prior PR head show 24 B less allocation per affected common compilation. The dead-try case drops from 1696 B to 1672 B, versus master's 1568 B: the remaining 104 B is spill setup. The measured wide discarded block saves 1184 B on .NET 10 LightExpression and 1232 B on .NET 8 LightExpression. Emitted IL is identical between prior and revised PR heads for the measured shared cases. Runtime timings/allocations and all slower observations are in the updated description; noisy timings do not justify a blanket speedup claim. The same visitor exists in #675. I am rebuilding that PR on this commit so dynamic target ordering reuses the collection metadata, with its own before/after measurements. A separate NoInvocationLambdaInlining ordering case involving an immediately invoked lambda body also reproduces on master and the prior PR. Its reproduction is retained separately; this rework does not add cached lambda-body spill metadata. |
|
@dadhi you can also look in the first commit, it's much simpler... but has a recursive walk.... |
|
Follow-up on the remaining loop: the discarded-syntax inspection could share the existing collection traversal, but those discarded nodes still need inspecting. Normal collection skips them, so it currently cannot discover their hidden try/goto syntax. The current rework visits live argument syntax once and separately inspects only discarded syntax. Integrating this into collection would require a syntax-only traversal mode that skips bindings, reductions and other compilation side effects. It could remove the separate walker implementation, but would not eliminate the necessary node visits. The implementation would also need to preserve iterative handling of deep discarded trees and avoid adding overhead to ordinary collection; fewer loops in the source do not by themselves establish a performance improvement. Doing the detection during IL emission is too late with the current design: FEC needs to decide whether to emit the implicit Compile call (or read a dynamic call-site Target) before emitting the arguments. Discovering a spill afterward would require buffering or rewriting emitted IL. Sharing the collection machinery is worth evaluating and measuring as a follow-up. The current implementation does not walk the live argument tree twice, but it still has a separate loop over discarded syntax. |
Fix argument/implicit-Compile ordering for runtime
Expression<TDelegate>invocations whose arguments containtry/gotosyntax in branches FEC discards. The difference is observable with side effects and null/invalid runtime expressions.Normal collection visits live argument syntax once. It records structural spill information when pruning conditional/switch syntax; nested runtime invocations propagate that information. Emitted exception-region and label metadata remain separate.
Discarded syntax uses an iterative cursor stack with eight inline entries. Child cursors avoid queueing wide sibling lists; unary chains advance in a local loop. The scanner does not reduce extensions, collect bindings, or enter nested lambda/quote data. Live extensions use their existing reduction/collection path. No recursive ExpressionVisitor or second walk of live arguments remains.
Validation
Full Release solution build and Standard/LightExpression native suites pass for net8.0, net10.0 and net472. The original regressions are retained. Added execution/exception-order tests cover 50,000-node discarded chains, 1,000-expression discarded blocks, nested invocations, lambda/quote boundaries, discarded bindings/extensions, switch pruning and initializer syntax.
Measurements
Fresh comparison: master
da14580f10fd19ae2361a8e91cfad4b2dfec182a, prior PR head3078c8b65b0c789f46fc5e80bd78b3f58495c780, revised heade95d0ffcf5bc4b33c5ea419f16d39bbb8532613c. All variants use SDK 10.0.301 and task-local ARM64 runtimes 8.0.28 / 10.0.9 on the same Windows machine.Both APIs use FEC; “System trees” identifies the expression API, not the System compiler. Trees, conversions and adapters are prepared outside measurement. Compilation includes delegate creation. Runtime expression invocation includes System Compile on every call, without caching.
Two opposite-load-order process runs; seven samples per run with rotated variant order; medians of 14 samples. Calibration targets 150 ms across all three variants. Tiering disabled; process pinned to one logical CPU. Allocations use GC.GetAllocatedBytesForCurrentThread. All observations, including slower results and unchanged-IL controls, are retained.
The visitor allocation is removed. Required spill setup remains: the measured dead-try path is 104 B above master, down from 128 B. Large discarded syntax still requires inspection, so compilation can remain slower than master, which skips that inspection. Timing differences do not support a blanket no-regression claim.
Full compilation and runtime results
Values are master → prior PR → revised PR.
nested_lambdaruntime_expression_dead_switchruntime_expression_dead_tryruntime_expression_deep_dead_plainruntime_expression_deep_dead_tryruntime_expression_live_addruntime_expression_plainruntime_expression_wide_dead_plainscalar_addnested_lambdaruntime_expression_dead_switchruntime_expression_dead_tryruntime_expression_deep_dead_plainruntime_expression_deep_dead_tryruntime_expression_live_addruntime_expression_plainruntime_expression_wide_dead_plainscalar_addnested_lambdaruntime_expression_dead_switchruntime_expression_dead_tryruntime_expression_deep_dead_plainruntime_expression_deep_dead_tryruntime_expression_live_addruntime_expression_plainruntime_expression_wide_dead_plainscalar_addnested_lambdaruntime_expression_dead_switchruntime_expression_dead_tryruntime_expression_deep_dead_plainruntime_expression_deep_dead_tryruntime_expression_live_addruntime_expression_plainruntime_expression_wide_dead_plainscalar_addRevised and prior PR delegates have identical emitted IL for all measured shared workloads. Runtime allocation changes are reported in the table; no runtime-code speedup is claimed for identical IL.
Known boundary: a separate ordering mismatch involving an immediately invoked lambda body with NoInvocationLambdaInlining also reproduces on master and the prior PR. This change does not add cached lambda-body spill metadata to address that separate case.