Repository navigation
Fix expression compiler compatibility - #558
jogibear9988 wants to merge 68 commits into
Conversation
|
Maaaaaan, a lot of Visitors and seems a lot of allocations too. |
Allocation/performance measurements and independent reviewFollowing @dadhi's concern about visitors and allocations, I compared the current patch against both its parent and current upstream, and reviewed the production code independently of the earlier test-fixing work. The allocation concern is justified. I recommend keeping this draft while the common-path regressions and the newly reproduced correctness issue below are addressed. Passing the imported test suite alone is not sufficient evidence for merging this change. Revisions: upstream Main observations
Independent review findings
The added visitor families are feature-gated. ArgumentSpillScope scans runtime Expression invocation/Dynamic arguments; QuoteVariableScope examines quotes, and QuoteBinder rebuilds captured quotes at execution. They are not all run on every ordinary CompileFast call. There is still duplicated feature-specific traversal, linear bound/free-variable searches, and quote rebuilding allocation. Unconditional scope bookkeeping and broad capture representation have measurable costs even where those visitors do not run. Exception-region objects are also allocated for handler trees without jumps. A blanket visitor rewrite is not justified by this review; remove duplicate work and isolate common-path costs first. Source locations at the reviewed head:
Measurement scope and methodWindows ARM64 virtual machine (Apple Silicon host, six exposed logical processors), SDK 10.0.301; .NET 10.0.9 and 8.0.28, Release, native ARM64. Production source snapshots were compiled unchanged in isolated dependency-free library projects with the appropriate target-framework and LIGHT_EXPRESSION symbols. The main checkout and PR head were not changed. 31 scenario shapes: 28 work on both baselines/head, and 3 newly supported shapes (closed/captured quotes and RuntimeVariables) are reported without a before/after ratio. Each backend's observable outputs are checked against System compilation for inputs -2, 0 and 3 before timing; returned captures/quotes are executed during verification and RuntimeVariables get/set is checked. This is a targeted control, not exhaustive semantic equivalence. Strict FEC is used with no outer fallback. Measured separately: tree construction (there is no text parser here), compilation of a reused tree, compilation plus first outer execution, and warm invocation. For escaping captures, first-use/warm measurements also create and call the returned delegate; for quotes/RuntimeVariables, they create/return the object, while returned quote compilation is only part of correctness verification. System uses its IL compiler, not the interpreter. Light trees are constructed natively, and equivalent System trees are prepared before System compilation timing; Light -> System conversion is excluded from that comparison. The RuntimeVariables Light factory is unavailable publicly, so that one construction case uses System construction + ToLightExpression and is explicitly not a native-Light construction comparison. Two fresh-process rounds per version, forward then reverse version order, seven warmed samples per phase, adaptive batches targeting ~8ms (caps 2,000 compile/construction or 1,000,000 invocation operations). TieredCompilation and ReadyToRun are disabled consistently. GC is forced outside each timed sample. Allocations use This measures managed allocation volume, not retained heap size or native/JIT memory; static initialization and application cold startup are warmed away. First execution means the newly created method's first call in a warmed process. It does not claim all possible expression graphs, Framework/x64, or production workload performance were measured. No whole imported correctness rerun was needed for this read-only review; the independent reproduction above adds a known issue beyond that historical baseline. Complete measured phase resultsIn the tables, B -> A is parent -> head, .NET 10.0.9, standard: all 31 scenarios, all four phases
.NET 10.0.9, light: all 31 scenarios, all four phases
.NET 8.0.28, standard: all 31 scenarios, all four phases
.NET 8.0.28, light: all 31 scenarios, all four phases
ReproductionsFresh Dynamic reduction reproduction (.NET 8/10, Standard FEC)using System;
using System.Dynamic;
using System.Linq.Expressions;
using FastExpressionCompiler;
class FreshDynamic : Expression {
public int Reductions;
readonly Expression argument;
readonly bool cache; Expression cached;
public FreshDynamic(Expression a,bool cache=false){argument=a;this.cache=cache;}
public override Type Type=>typeof(int);
public override ExpressionType NodeType=>ExpressionType.Extension;
public override bool CanReduce=>true;
public override Expression Reduce(){Reductions++;if(cache&&cached!=null)return cached;return cached=Convert(Dynamic(new AddBinder(),typeof(object),argument,Constant(2)),typeof(int));}
}
class AddBinder : BinaryOperationBinder {
public AddBinder():base(ExpressionType.Add){}
public override DynamicMetaObject FallbackBinaryOperation(DynamicMetaObject target,DynamicMetaObject arg,DynamicMetaObject errorSuggestion)=>new DynamicMetaObject(Expression.Convert(Expression.Add(Expression.Convert(target.Expression,typeof(int)),Expression.Convert(arg.Expression,typeof(int))),typeof(object)),BindingRestrictions.GetTypeRestriction(target.Expression,typeof(int)).Merge(BindingRestrictions.GetTypeRestriction(arg.Expression,typeof(int))));
}
class Program {
static void Main(){
foreach(var flags in new[]{CompilerFlags.Default,CompilerFlags.NoInvocationLambdaInlining}) {
var x=Expression.Parameter(typeof(int));var extension=new FreshDynamic(x);var tree=Expression.Lambda<Func<int,int>>(extension,x);
try {var f=tree.CompileFast(true,flags);Console.WriteLine($"FEC {flags}: {(f==null?"null":f(5).ToString())}; reductions={extension.Reductions}");}catch(Exception e){Console.WriteLine($"FEC {flags}: reductions={extension.Reductions}; "+e);}
Console.WriteLine("System: "+tree.Compile()(5));
}
var p=Expression.Parameter(typeof(int));var direct=Expression.Lambda<Func<int,int>>(Expression.Convert(Expression.Dynamic(new AddBinder(),typeof(object),p,Expression.Constant(2)),typeof(int)),p);
var result=direct.CompileFast(true);Console.WriteLine("Direct Dynamic FEC: "+(result==null?"null":result(5).ToString()));
var stable=Expression.Lambda<Func<int,int>>(new FreshDynamic(p,true),p).CompileFast(true);
Console.WriteLine("Cached reduction FEC: "+(stable==null?"null":stable(5).ToString()));
}
}Head: NRE at Dynamic metadata lookup, reductions=2, under both flags. System/direct/cached controls return 7. Parent/upstream: null, not an exception. Inherited #555 integration control (Light)using System;
using System.Linq.Expressions;
using FastExpressionCompiler.LightExpression;
using E=System.Linq.Expressions.Expression;
class Program {
static void Main(){
var p=E.Parameter(typeof(decimal));
var tree=E.Lambda<Func<decimal,decimal>>(E.AddAssign(p,E.Constant(1m),typeof(decimal).GetMethod("op_Addition",new[]{typeof(decimal),typeof(decimal)}),null),p);
Console.WriteLine("System: "+tree.Compile()(2m));
var light=tree.ToLightExpression();Console.WriteLine("FEC direct: "+light.CompileFast(true)(2m));
try {var roundtrip=(System.Linq.Expressions.Expression<Func<decimal,decimal>>)light.ToExpression();Console.WriteLine("Roundtrip: "+roundtrip.Compile()(2m));}catch(Exception e){Console.WriteLine("Roundtrip: "+e);}
}
}Head/parent: System=3, direct FEC=3, round-trip=NRE. Upstream: all three return 3. Reproducing the measurementsThe source harness below is shared across versions. For Standard, compile it against the four production Set Benchmark harness and snapshot build scriptsusing System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text.Json;
#if LIGHT
using FastExpressionCompiler.LightExpression;
using E = FastExpressionCompiler.LightExpression.Expression;
using X = FastExpressionCompiler.LightExpression.Expression;
using L = FastExpressionCompiler.LightExpression.LambdaExpression;
#else
using FastExpressionCompiler;
using E = System.Linq.Expressions.Expression;
using X = System.Linq.Expressions.Expression;
using L = System.Linq.Expressions.LambdaExpression;
#endif
public class Box { public int Value; public Box(int x) { Value=x; } public int Add(int x)=>Value+x; }
public static class Program {
static object sink; static long checksum;
#if LIGHT
static L systemSource;
static System.Linq.Expressions.LambdaExpression systemPrepared;
#endif
record Scenario(string Name, Func<L> Build);
static X Balanced(X x,int depth) => depth==0?x:E.Add(Balanced(x,depth-1),Balanced(x,depth-1));
static L Build(string name) {
var x=E.Parameter(typeof(int),"x"); X body=x;
switch(name) {
case "constant": body=E.Constant(42);break;
case "add": body=E.Add(x,E.Constant(1));break;
case "arithmetic64": for(int i=0;i<64;i++)body=E.Add(body,E.Constant(i));break;
case "balanced256":body=Balanced(x,8);break;
case "conditional":body=E.Condition(E.LessThan(x,E.Constant(0)),E.Negate(x),x);break;
case "nullable":body=E.Coalesce(E.Convert(x,typeof(int?)),E.Constant(7));break;
case "new_member":body=E.Field(E.New(typeof(Box).GetConstructor(new[]{typeof(int)}),x),"Value");break;
case "constant_object":body=E.Call(E.Constant(new Box(7)),typeof(Box).GetMethod("Add"),x);break;
case "array":body=E.ArrayIndex(E.Constant(new[]{2,3,5,7}),E.And(x,E.Constant(3)));break;
case "new_array":body=E.ArrayLength(E.NewArrayInit(typeof(int),x,x,x));break;
case "interned_string":body=E.Property(E.Constant("fixed literal"),"Length");break;
case "noninterned_string":body=E.Property(E.Constant(new string('z',37)),"Length");break;
case "try_catch":body=E.TryCatch(E.Divide(E.Constant(42),x),E.Catch(typeof(DivideByZeroException),E.Constant(-1)));break;
case "try_finally":body=E.TryFinally(E.Add(x,E.Constant(1)),E.Empty());break;
case "switch":body=E.Switch(x,E.Constant(8),E.SwitchCase(E.Constant(3),E.Constant(0)),E.SwitchCase(E.Constant(4),E.Constant(1)));break;
case "goto":{var t=E.Label(typeof(int));body=E.Block(E.Return(t,x),E.Label(t,E.Constant(0)));break;}
case "loop":{var v=E.Variable(typeof(int));var t=E.Label(typeof(int));body=E.Block(new[]{v},E.Assign(v,E.Constant(0)),E.Loop(E.IfThenElse(E.LessThan(v,E.Constant(8)),E.Block(E.PostIncrementAssign(v),E.Empty()),E.Break(t,v)),t));break;}
case "blocks32":case "blocks128":{int n=name=="blocks32"?32:128;var arr=new List<X>();for(int i=0;i<n;i++){var v=E.Variable(typeof(int),"v"+i);arr.Add(E.Block(new[]{v},E.Assign(v,x),E.Add(v,E.Constant(i))));}body=E.Block(arr);break;}
case "invoke_literal":{var p=E.Parameter(typeof(int));body=E.Invoke(E.Lambda<Func<int,int>>(E.Add(p,E.Constant(2)),p),x);break;}
case "invoke_delegate":body=E.Invoke(E.Constant((Func<int,int>)(v=>v+2)),x);break;
case "invoke_try_argument":{var p=E.Parameter(typeof(int));body=E.Invoke(E.Lambda<Func<int,int>>(E.Add(p,E.Constant(2)),p),E.TryFinally(x,E.Empty()));break;}
case "capture1": return E.Lambda<Func<int,Func<int>>>(E.Lambda<Func<int>>(E.Add(x,E.Constant(1))),x);
case "capture_reference":{var v=E.Variable(typeof(Box),"box");return E.Lambda<Func<int,Func<int>>>(E.Block(new[]{v},E.Assign(v,E.Constant(new Box(7))),E.Lambda<Func<int>>(E.Field(v,"Value"))),x);}
case "nested_blocks8":case "nested_blocks32":case "nested_blocks128":{int count=int.Parse(name.Substring("nested_blocks".Length));for(int i=0;i<count;i++)body=E.Block(body);break;}
case "capture8":{var vs=Enumerable.Range(0,8).Select(i=>E.Variable(typeof(int),"v"+i)).ToArray();X sum=x;foreach(var v in vs)sum=E.Add(sum,v);return E.Lambda<Func<int,Func<int>>>(E.Block(vs,vs.Select(v=>(X)E.Assign(v,x)).Concat(new X[]{E.Lambda<Func<int>>(sum)})),x);}
case "quote_closed":return E.Lambda<Func<int,System.Linq.Expressions.Expression<Func<int>>>>(E.Quote(E.Lambda<Func<int>>(E.Constant(9))),x);
case "quote_capture":return E.Lambda<Func<int,System.Linq.Expressions.Expression<Func<int>>>>(E.Quote(E.Lambda<Func<int>>(x)),x);
case "runtime_variables":
#if LIGHT
var sp=System.Linq.Expressions.Expression.Parameter(typeof(int));
return System.Linq.Expressions.Expression.Lambda<Func<int,IRuntimeVariables>>(System.Linq.Expressions.Expression.RuntimeVariables(sp),sp).ToLightExpression();
#else
return E.Lambda<Func<int,IRuntimeVariables>>(E.RuntimeVariables(x),x);
#endif
default:throw new Exception(name);
}
return E.Lambda<Func<int,int>>(body,x);
}
static Delegate Compile(L tree,bool system) {
if(system) {
#if LIGHT
if(!ReferenceEquals(systemSource,tree)){systemSource=tree;systemPrepared=(System.Linq.Expressions.LambdaExpression)tree.ToExpression();}
return systemPrepared.Compile();
#else
return tree.Compile();
#endif
}
return tree.CompileFast(true)??throw new NotSupportedException("strict FEC returned null");
}
static string Verify(Delegate d) {
var parts=new List<string>();
foreach(int v in new[]{-2,0,3}) {
var result=d.DynamicInvoke(v);
if(result is Func<int> nested) result=nested();
if(result is System.Linq.Expressions.Expression<Func<int>> quote) result=quote.Compile()();
if(result is IRuntimeVariables vars) {var initial=vars[0];vars[0]=17;result=initial+":"+vars[0];}
parts.Add(result?.ToString()??"null");
}
return string.Join("|",parts);
}
static Action<int> Driver(Delegate d) {
if(d is Func<int,int> f)return n=>{long s=0;for(int i=0;i<n;i++)s+=f(3);checksum=s;};
if(d is Func<int,Func<int>> f2)return n=>{long s=0;for(int i=0;i<n;i++){var v=f2(3);sink=v;s+=v();}checksum=s;};
if(d is Func<int,System.Linq.Expressions.Expression<Func<int>>> fq)return n=>{for(int i=0;i<n;i++)sink=fq(3);};
if(d is Func<int,IRuntimeVariables> fr)return n=>{for(int i=0;i<n;i++)sink=fr(3);};
throw new Exception("driver "+d.GetType());
}
record Sample(string Version,string Runtime,string Flavor,string Scenario,string Phase,int Round,int SampleIndex,int Operations,double NsPerOp,double BytesPerOp,int Gen0,int Gen1,int Gen2);
static void Measure(string version,string flavor,string scenario,string phase,int round,Action<int> op) {
op(30);
int n=phase=="invoke"?10000:20;
var sw=Stopwatch.StartNew();op(n);sw.Stop();
n=(int)Math.Clamp(n*8.0/Math.Max(sw.Elapsed.TotalMilliseconds,0.01),phase=="invoke"?1000:10,phase=="invoke"?1000000:2000);
for(int k=0;k<7;k++) {
GC.Collect();GC.WaitForPendingFinalizers();GC.Collect();
long allocated=GC.GetAllocatedBytesForCurrentThread();int g0=GC.CollectionCount(0),g1=GC.CollectionCount(1),g2=GC.CollectionCount(2);
long start=Stopwatch.GetTimestamp();op(n);long end=Stopwatch.GetTimestamp();
long bytes=GC.GetAllocatedBytesForCurrentThread()-allocated;
Console.WriteLine(JsonSerializer.Serialize(new Sample(version,Environment.Version.ToString(),flavor,scenario,phase,round,k,n,(end-start)*1e9/Stopwatch.Frequency/n,bytes/(double)n,GC.CollectionCount(0)-g0,GC.CollectionCount(1)-g1,GC.CollectionCount(2)-g2)));
}
}
public static void Main(string[] args) {
string version=args[0],flavor=args[1];int round=int.Parse(args[2]);bool system=version=="system";
string[] names={"constant","add","arithmetic64","balanced256","conditional","nullable","new_member","constant_object","array","new_array","interned_string","noninterned_string","try_catch","try_finally","switch","goto","loop","blocks32","blocks128","invoke_literal","invoke_delegate","invoke_try_argument","capture1","capture8","quote_closed","quote_capture","runtime_variables"};
names=names.Concat(new[]{"capture_reference","nested_blocks8","nested_blocks32","nested_blocks128"}).ToArray();
if(args.Length>3)names=names.Where(n=>args.Skip(3).Contains(n)).ToArray();
foreach(string name in names) {
try {
var tree=Build(name);var oracle=Compile(tree,true);var d=Compile(tree,system);var expected=Verify(oracle);var actual=Verify(d);
if(expected!=actual)throw new Exception("behavior mismatch "+actual+" expected "+expected);
Console.Error.WriteLine("PASS "+version+" "+flavor+" "+name+" "+actual);
Action<int> build=n=>{for(int i=0;i<n;i++)sink=Build(name);};
Action<int> compile=n=>{for(int i=0;i<n;i++)sink=Compile(tree,system);};
Action<int> first=n=>{for(int i=0;i<n;i++){var c=Compile(tree,system);if(c is Func<int,int> f)checksum=f(3);else if(c is Func<int,Func<int>> ff){var nested=ff(3);sink=nested;checksum=nested();}else if(c is Func<int,IRuntimeVariables> fr)sink=fr(3);else sink=((Func<int,System.Linq.Expressions.Expression<Func<int>>>)c)(3);}};
Measure(version,flavor,name,"construct",round,build);
Measure(version,flavor,name,"compile",round,compile);
Measure(version,flavor,name,"compile_first",round,first);
Measure(version,flavor,name,"invoke",round,Driver(d));
} catch(Exception e) {Console.Error.WriteLine("FAIL "+version+" "+flavor+" "+name+" "+e);}
}
GC.KeepAlive(sink);GC.KeepAlive(checksum);
}
}
from pathlib import Path
import subprocess,json
root=Path(__file__).resolve().parent
repo=Path(r'C:\Data\Git\Github\FastExpressionCompiler')
commits={'upstream':'ff907e1986700a30f05a56e5cee28e952faee363','parent':'e4c232da108d98790f0ca73f23ec90756da0cad7','head':'b8f8b38bc44367f129b49a55b2b2043e81b04ccb'}
for name,commit in commits.items():
for kind in ('standard','light'):
dest=root/name/kind;dest.mkdir(parents=True,exist_ok=True)
paths=['src/FastExpressionCompiler/'+f for f in ['FastExpressionCompiler.cs','ImTools.cs','ILReader.cs','TestTools.cs']]
if kind=='light': paths+=['src/FastExpressionCompiler.LightExpression/'+f for f in ['Expression.cs','ExpressionVisitor.cs','FlatExpression.cs']]
for p in paths:
(dest/Path(p).name).write_bytes(subprocess.check_output(['git','show',commit+':'+p],cwd=repo))
defines='<DefineConstants>LIGHT_EXPRESSION</DefineConstants>' if kind=='light' else ''
(dest/'Library.csproj').write_text('<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><TargetFrameworks>net8.0;net10.0</TargetFrameworks><LangVersion>latest</LangVersion><AssemblyName>FecSnapshot.'+kind+'</AssemblyName><Optimize>true</Optimize>'+defines+'</PropertyGroup></Project>')
(root/'commits.json').write_text(json.dumps(commits,indent=2))
from pathlib import Path
import subprocess
root=Path(__file__).resolve().parent
for version in ('upstream','parent','head'):
for flavor in ('standard','light'):
dest=root/version/flavor
(dest/'Harness.csproj').write_text('<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><OutputType>Exe</OutputType><TargetFrameworks>net8.0;net10.0</TargetFrameworks><LangVersion>latest</LangVersion><EnableDefaultCompileItems>false</EnableDefaultCompileItems><Optimize>true</Optimize>'+('<DefineConstants>LIGHT</DefineConstants>' if flavor=='light' else '')+'</PropertyGroup><ItemGroup><Compile Include="../../Program.cs"/><ProjectReference Include="Library.csproj"/></ItemGroup></Project>')
# Exclude harness from library default items (harness source is outside project).
with (dest/'build.log').open('w') as log:
p=subprocess.run(['dotnet','build',str(dest/'Harness.csproj'),'-c','Release','--nologo','-v:q'],stdout=log,stderr=subprocess.STDOUT)
print(version,flavor,p.returncode,flush=True)
if p.returncode: print((dest/'build.log').read_text());raise SystemExit(1)No production fixes, new commits, pushes, or changes to #557/#559 were made as part of this review. The PR remains a draft. Suggested next steps: address the Dynamic reduction failure, remove duplicated Invoke reflection, reduce capture metadata/reflection allocations, and eliminate label-free scope-stack heap growth; then rerun the same paired measurements and correctness controls. Shared-cell semantics should be preserved throughout. |
Follow-up: regression repairs and measured resultsPushed 46 logical source/test repair commits plus the evidence commit, through @dadhi, the allocation concern was valid. The worst common-path allocation regressions are repaired, but before-PR performance parity is still not achieved. What improvedRepresentative Standard/.NET 10 managed B/op from the latest broad run (rounded):
The mechanisms are per-compilation metadata/signature reuse, conservative capture storage selection, lazy quote/runtime-variable metadata, and bounded/compressed scope buffers. There is no cross-compilation user-type cache or outer System compiler fallback. The direct emitter and object[] closure layout are preserved. Quote/runtime-variable exposure and shared mutation retain the required live storage. The final compact scope pool saves 8,104 B in the deep-scope cold causal control; measured warm/reentrant diagnostics show no new scope-buffer misses. Deferring unrelated quote metadata removes the 880 B (.NET 8) / 888 B (.NET 10) common cold overhead in the causal control. Light private array capture is 32 B below pre-PR on both runtimes. The independently reproduced fresh-extension-reduction + Dynamic failure is fixed by preserving collected reductions; regression coverage also includes Quote composition. Framework readonly reference loads now carry the required types in verified anonymous methods. The previous What is still worse
Ratios above one mean slower. Compile time is still about 11.4% / 9.8% worse by this descriptive median. These medians do not promise parity in each scenario; the report lists every timing residual, including runtime phases.
No total native/JIT or retained-memory parity claim is made. Matching finalized managed IL in a capture control also does not prove identical native code or invalidate an observed timing residual. The known deep-tree crash remains deferred as requested. Validation and reproduction
Evidence directory · Decisions and limitations · Exact remaining costs · Validation · Repair commits. The directory contains the interactive report, complete phase CSV/JSON (including System), raw latest/historical archives, reproduction scripts, source/assembly hashes, and causal controls. Historical failures and rejected experiments remain preserved separately. The PR description has been updated with the same results and limitations. This remains a draft with unresolved performance regressions. |
Further allocation/compilation repairs@dadhi, pushed 7 additional logical fixes and their full measurement/validation evidence. Measured source:
The five Framework stages separately reduce constructor reflection (-192 B in the mutable control), reuse typed rented signatures (-64/-120 B), restore owned boxed cells (-320 B), and omit proven redundant delegate/closure casts (-32 B and -120 to -264 B). Runtime allocations do not increase in these causal controls. Compared with pre-PR, mutable nested invocation improves 24 B -> 0 B (factory plus invocation 208 B -> 184 B), while compilation still costs more. The other repairs avoid unused validator JIT and bypass the large collector for eligible binary expressions without a second pass. Full per-commit observations and cold controls are included; no uniformly faster CPU claim is made.
Measured compilation medians remain above pre-PR. These samples do not establish CPU parity; every scenario and phase remains in the full results. Allocation lower/same/higher counts for 41 common Standard shapes: .NET 8 14/27/0; .NET 10 14/27/0. The full table includes every runtime/activation phase, both libraries, Framework and System controls. Small fractional allocation differences in the adaptive runs did not reproduce with equal operation counts: none of 100 targeted current-versus-published phase comparisons allocate more (52 modern, 48 Framework). Empty observer windows allocate 0 bytes; no counter overhead is subtracted. The original results and modern/Framework confirmations are both retained. The allocation table above uses these equal-count controls. Still open: deep mixed128 cold scope allocation cost, small-EH cold initialization costs, protected/typed Framework costs and the timing residuals listed in the report. Shared-VM timings have substantial variation; total native/JIT/retained memory was not measured. Universal pre-PR parity is not established. Rejected experiments are retained, including wrapper cleanup with a consistently higher sampled cold allocation mode. Validation: 35,064 imported passes, 0 failures/crashes among executed cases, existing 69 exclusions unchanged; full native .NET 8/.NET 10 and Framework suites plus all configured production builds pass. Fresh 47-shape matrices cover before/published/current/System on Standard/Light modern runtimes and Framework. Each phase has two opposite-order process rounds and 7 samples; causal microexperiments retain separate raw/reversed/cold observations. Report and complete evidence | Per-commit measurements | Exact remaining costs | Validation | Commits | Decisions. |
| // Framework prohibits using the anonymous module with the module overload. | ||
| return new DynamicMethod(string.Empty, returnType, parameterTypes, restrictedSkipVisibility: true); | ||
| #else | ||
| // Preserve the anonymous module's non-Exception catch policy, without the |
There was a problem hiding this comment.
@jogibear9988 The hell is that hack? :)
Anyway, I am already feeling the boost in my knowledge of .NET internals.
There was a problem hiding this comment.
From where should I know?
I now wanted to let AI finish this so we get nearly the same perf. and know it can work. Then we can use this as a base when you want to do it error by error.
There was a problem hiding this comment.
Yes — this is a runtime-specific workaround, and your objection is fair. Looking more closely at the runtime source, I should remove this version.
The reason for changing the host is observable exception behavior: the host assembly's WrapNonExceptionThrows policy affects matching of raw CLR throws (e.g. a string) and RuntimeWrappedException. FEC's ordinary owner-hosted method and System's anonymous method do not have the same policy. The regression tests exercise raw/wrapped payload identity, catch matching, filters/rethrow, and private-member access; simply returning to the ordinary host for these methods would undo that behavior fix.
The workaround tries to get both properties: reuse the anonymous module for its exception policy, but use the module constructor to avoid the restricted anonymous constructor's eager JIT in CreateDelegate. However, .NET's initialization code explicitly asserts that this anonymous module must not be passed by callers. It works in the tested .NET 8/10 release runtimes, but that is not a supported contract. Framework already rejects this combination, hence the conditional fallback.
I removed the module-reuse workaround in 70f297cb and now use the supported anonymous DynamicMethod(..., restrictedSkipVisibility: true) constructor for exception-aware methods, retaining ordinary owner hosting elsewhere. .NET explicitly compiles that restricted method in CreateDelegate, so the measurements report compile-only, compile + first execution, steady-state calls, and allocations separately. Moving JIT cost into compilation must not be described as an equivalent increase in total first-use cost.
The correction, validation and measurements are pushed, and the description is updated. Complete results and remaining regressions. The PR remains a draft: universal pre-PR performance/allocation parity is not established.
| @@ -645,6 +779,184 @@ public struct CompilerContext | |||
| internal SmallMap4<Expression, NoValue, RefEq<Expression>> ArgsContainingComplexExpression; | |||
|
|
|||
| internal bool HasComplexExpression; | |||
| internal bool HasExceptionBlock; | |||
There was a problem hiding this comment.
combine those and follow up bools into the bit flags
There was a problem hiding this comment.
I tried packing the nine internal booleans into a ushort flag field, keeping the public LastEmitIsAddress field and existing ClosureStatus unchanged. It saves 8 bytes in CompilerContext on every tested target. I also tested a compact single-load setter form: each setter's IL shrinks by 5 bytes, with the same representation and semantics.
The correctness checks pass, but I have held both versions under the requirement not to introduce performance regressions. In the bounded matched tests, the compact version's Framework Light capture8 compilation increased from 6896.625 to 7088.625 B, and compile+first from 7304 to 7496 B: +192 B in each phase. The exact allocation mechanism is unresolved. Some compilation controls also slowed, despite a small improvement in the median across scenarios. The smaller context alone is therefore not enough to claim this change is an improvement. The original sources, both candidates, raw samples, IL evidence and limitations are preserved in the report; the production flags remain unchanged.
| // Frames follow the existing collector traversal; persistent ancestry is allocated | ||
| // only for expressions that actually contain a label or a jump. | ||
| internal SmallList<LabelScopeFrame, Stack4<LabelScopeFrame>, ScopeArrayPool<LabelScopeFrame>> LabelScopes; | ||
| internal SmallList<(LabelTarget Target, int Scope), Stack4<(LabelTarget Target, int Scope)>, NoArrayPool<(LabelTarget Target, int Scope)>> ScopedLabels; |
There was a problem hiding this comment.
Do we need whole int for the Scope?
Maybe for the compactness and usability we can put Target and Scope into struct with strict layout?
(same for the Target Region pair above)
There was a problem hiding this comment.
I checked the actual layout: the existing target+int tuples occupy 16 bytes on all six tested Standard/Light runtime configurations. Named sequential structs keep that reference-alignment footprint, so replacing the tuples alone does not produce the proposed saving.
Scope and Region are ancestry indices with no established ushort bound; the 1024 pool-retention limit does not limit valid input size. I kept full-width IDs and the existing named tuple fields rather than adding an input limit or an unaligned-reference layout. The separate one-word region-frame experiment is reported below, including its costs.
| return; | ||
| } | ||
| } | ||
| PushNewLabelScopeFrame(kind); |
There was a problem hiding this comment.
wut? so here you specifically prevent inlining?
There was a problem hiding this comment.
Yes, deliberately. PushLabelScope is aggressively inlined: repeated adjacent scope kinds update the last frame and return. The separate helper keeps the append/list-growth path outlined, instead of pulling that path into the common wrapper. This is a performance choice, not required for correctness or added just for measuring.
I tested removing only NoInlining against 8f, leaving the automatic JIT policy to decide. All 252 flag/version/shape checks passed, but the bounded matched benchmark did not establish a consistent win: 42 compilation comparisons had a median paired ratio 1.015, with controls ranging 0.921–1.149. For example Light .NET 8 label/goto compilation was 14.9% slower and Light .NET 10 balanced compilation 9.5% slower in that run. These are observations, not proof that this attribute is universally optimal, and unchanged generated-delegate runtime differences should not be attributed to the compiler helper. I retained the outline and added a source comment explaining it in ed431c7. The raw samples and limitations are in the report.
| internal int Region; | ||
| } | ||
|
|
||
| // These scalar scope lists grow their heap part as 4, 9, 19, 39, ... . |
There was a problem hiding this comment.
The logarithm reduces the pool's bookkeeping array, not the size of the rented buffers. These lists grow their heap storage as 4, 9, 19, 39, ... (2*n+1), so each normal growth length has a different floor(log2(length)) bucket. The 1024-element retention limit therefore needs 11 array-reference slots, instead of a table indexed by every possible length (1024 or 1025 slots). This avoids allocating about 8 KB of bookkeeping at the first overflow on 64-bit runtimes.
Different arbitrary lengths can still share a bucket. That can evict a reusable buffer, but cannot give the caller the wrong size: exact rents check equality, minimum rents check the actual length and search larger buckets, and a miss allocates the requested length. Interlocked.Exchange transfers ownership; the 1024 retention bound is unchanged. I clarified this in the source comment in ed431c7; the pool policy is unchanged.
| internal SmallList<LightAndSysExpr> Converted; | ||
| } | ||
| #endif | ||
| private class QuoteVariableScope : System.Linq.Expressions.ExpressionVisitor |
There was a problem hiding this comment.
Those are visitors, each for the specific case, but there is already manual visiting traversal in CollectAndValidate. Can we move this code there?
There was a problem hiding this comment.
Some shared traversal is possible, but these visitors have different stages and semantics. QuoteBinder runs at execution time with the current activation's live capture cells, which CollectAndValidate does not have. Quoted syntax is returned data: executable collection would prune branches, lower Dynamic expressions, collect nested lambdas, or reduce opaque extensions that must be preserved/rejected without callbacks. The existing lexical scope handling is already shared between quote analysis and binding.
ArgumentSpillScope also deliberately sees Try/Goto in constant-pruned branches. That controls whether runtime-expression compilation and Dynamic target reads happen before or after argument effects/jumps; the ordinary collector prunes those branches. Re-running collection would also risk duplicate Dynamic reductions and extension callbacks. I added boundary tests for opaque quoted extensions and pruned Try/Goto argument ordering in 35622f3, comparing the latter with System.
I measured three bounded cleanups rather than merging the traversals blindly: a lazy free-variable list (32/40 B less per captured-quote activation), a separate compile-only quote collector (40/48 B less), and a shared lazy argument scanner (24 B less terminal-argument compilation). They pass the correctness controls, but the quote revisions slowed compile+first-use, and argument timings were mixed. I held these changes to respect the no-regression requirement. The report retains their sources, benefits, costs and limitations; no visitor change is included in this update.
|
@dadhi, the anonymous-module workaround is removed in Four logical commits:
Validation: 35,064 imported passes, zero failures/crashes among executed cases; all production/native Standard/Light builds and suites pass. The existing 69 exclusions, including deferred deep-tree modes, are unchanged. The final matrix compares the last pushed
The decimal-chain first-use cost improves 14–35% versus the last pushed revision across the six configurations. Warm compilation removes 5,832 B on modern .NET / 14,048 B on Framework. However, modern EH compile-only now includes eager JIT: the .NET 10 chain's compile-only time is 57.3x its previously deferred-JIT baseline, while compile + first is 0.703x that baseline. The phase boundary matters. Identical emitted IL alone does not establish identical call performance. The try/catch and real-finally controls have matching canonical instructions/locals/EH but different host flags; .NET 10 Standard try/catch call time changes approximately 2.29 -> 1.87 ns. This does not prove a particular native-entry mechanism. Separate same-compiler A/A and native-code diagnostics are included; they are not used to erase observed costs. Remaining costs, retained in full:
The restore-guard experiment was rejected and empty-finally remains held. The unsupported supplied-ref-operator and preexisting pending-operand/outward-goto families remain documented; no new imported exclusions were added. Earlier pre-PR measurements retain their original source identities rather than being relabeled as final results. |
|
Reviewed all seven comments in review 5577873439 and replied individually with the decisions and measurements. Pushed three commits: The proposed packing/visitor changes remain held. They have real memory benefits, but the measured costs prevent claiming an improvement under the requested performance constraint:
Full validation: 35,064 imported cases pass, zero executed failures or crashes, 69 existing exclusions unchanged. Both production builds and .NET 8/10 native suites pass; Framework passes 1,041 TestX and 1,595 legacy tests. No emitter or production behavior change is included in this follow-up. Report, exact sources, all 13,596 warm observations, 60 cold allocation observations, hashes and validation logs. Compilation, first invocation, activation and nested invocation are separate; these are bounded managed-allocation/time measurements, not a claim of universal CPU parity. Review threads are left for the maintainer to resolve. This PR stays draft; #557 and #559 were not changed. |
187b9b8 to
ae4d9a5
Compare
|
Rebased #558 onto current dadhi/master The conflict resolutions preserve upstream's map-to-set cleanup and the existing compatibility fixes. Both expression membership trackers now use Fresh validation at
Rebase mapping, integration diff, hashes and full validation logs. Earlier performance data retains its original source hashes; no new timing or performance-parity claim is made for this base. Draft status is retained, and #559 was not changed. |
|
Merged current dadhi/master
Validation at Narrow before/after measurements: merged source Timing medians are mixed: converted-shape compilation ranges from 8.9% faster to 2.8% slower. Slower cases are Light net8 addition (+2.8%) and Light net10 selected method (+2.7%). Runtime medians range from about 0.2% faster to 2.5% slower, with the largest absolute increase about 0.036 ns. These small synthetic measurements do not establish universal speedup or parity; cold/first-use, tree construction, Framework/x64 timing and application workloads were not remeasured. Historical reports remain tied to their original revisions. Exact results, all raw samples, hashes, emitted IL, scripts and full validation logs. This remains draft. #559 is unchanged. |
|
Follow-up merge is pushed: Fresh checks pass: 35,064 imported cases, zero executed failures/crashes, 69 unchanged exclusions; both production builds and full .NET 8/10 ARM64 / Framework native suites pass. Exact counters, hashes and full logs. The runtime-value source clarification and review reply are included in |
…sTrue, Quote; opti for Invocation(Lambda(..))
aaa79fe to
dd98968
Compare
Freeze master f49acef, locally merged dadhi#558 at 593c72e2, and optimized dadhi#559 source ef577910. Record 44 workloads, Standard/Light, .NET 8/10, construction/compilation/warm invocation plus a separate compilation-and-first-invocation pass. Retain 34496 main/reference samples, per-commit allocation/timing evidence, actual root and nested IL strings, source/binary hashes, rejected candidates and validation logs. Compile-only medians improve 0.7-2.2% versus dadhi#558 but remain 8.2-9.9% above master. Compilation plus first invocation remains 0.1-1.5% above master on median workload ratios. The apparent nested-exception compile-only outlier shifts JIT work into CreateDelegate for the correctness-required anonymous host; the end-to-end pass accounts for it. Keep slower System comparison cases and unchanged-code allocation controls explicit. Correct the historical IL claim: its serializer compared StringBuilder metadata. The new actual-instruction check matches all 176 root/nested bundles between dadhi#558 and dadhi#559. Production and native suites, Framework 1051 TestX plus 1601 legacy tests, and all 35064 executed imported tests pass. Existing exclusions unchanged; production source unchanged by this evidence commit.
Affected paths: LabelScopeDepth, LabelScopeKind, LabelScopes, NotSupported_HoistedVariable, PushLabelScope, RestoreLabelScopeDepth, RestoreScopeAndReturn, TryCollectElementInitInfo. Regression coverage: LexicalJumpScopeRegressionTests.cs.
Affected paths: LabelScopeDepth, LabelScopeKind, PushNewLabelScopeFrame.
Regression coverage: InvocationArgumentContextRegressionTests.cs.
Affected paths: CaptureMethodParameters, GetOrCollectCaptureMethodParameters.
Affected paths: TryEmitArithmeticOperation.
…ue slot Affected paths: CaptureAnalysisRoot, GetCaptureMethodParameters, IsLeadingCaptureInitializer, IsPrivateReferenceCapture, PrivateCaptureMethodIsByValue. Regression coverage: CapturedValueRefRegressionTests.cs.
…afety Affected paths: AnalyzeCaptureStorage, CaptureAnalysisRoot, CaptureStorageAnalyzed, CaptureStorageQuoteReferences, CaptureStorageRequiresCells, CaptureStorageRequiresTypedCells, GetHoistedType, IsKnownCaptureIntrinsic. Regression coverage: Issue347_InvalidProgramException_on_compiling_an_expression_that_returns_a_record_which_implements_IList.cs, CapturedValueRefRegressionTests.cs.
Affected paths: LabelScopeDepth, LabelScopeKind, LabelScopes, PushLabelScope. Regression coverage: LexicalJumpScopeRegressionTests.cs.
Affected paths: CaptureStorageAnalyzed, CaptureStorageRequiresTypedCells.
…tainers Affected paths: IsKnownCaptureIntrinsic, PrivateCaptureMethodIsByValue. Regression coverage: CapturedValueRefRegressionTests.cs.
…lection Affected paths: CaptureAnalysisRoot, LabelScopeDepth. Regression coverage: CapturedValueRefRegressionTests.cs.
Regression coverage: CapturedValueRefRegressionTests.cs.
Affected paths: LabelScopeFrame, LabelScopes, MaxPooledScopeFrames, ScopeArrayPool, ScopeInfo, ScopeInfos. Regression coverage: LexicalJumpScopeRegressionTests.cs.
Affected paths: ScopedGotos, TryCollectInfo.
Regression coverage: CapturedValueRefRegressionTests.cs.
Affected paths: TryCollectSimpleBinary, TryCollectTerminalInfo. Regression coverage: LexicalJumpScopeRegressionTests.cs.
Affected paths: CaptureStorageAnalyzed, CaptureStorageRequiresTypedCells, OwnedValueCaptureRoot. Regression coverage: CapturedValueRefRegressionTests.cs.
Affected paths: CaptureStorageAnalyzed, CaptureStorageRequiresTypedCells, OwnedValueCaptureRoot.
Affected paths: CaptureStorageAnalyzed, CaptureStorageRequiresTypedCells, OwnedValueCaptureRoot.
…tion-aware methods
Keep opaque/custom intrinsic operands on the general collection/emission path. Preserve the actual root and exception-bearing state when deciding whether a scalar arithmetic path is safe.
Document the supported compiler behavior, strict compatibility checks and remaining exclusions.
Use Args and direct expression accessors throughout capture analysis, prepared calls and invocation emission. Retain shared complex-argument collection and the existing index-operand helper.
Use GetSurePresentRef only where loop bounds or prior argument/bound collection establish a present element. Avoid copying the large writeback record. Leave arbitrary scope-id accesses checked. Full Standard/LightExpression tests pass on .NET 8, .NET 10 and .NET Framework.
6ae7938 to
80e6fbf
Compare
Compile expressions that require shared mutable captures, expression-tree data and scoped control flow while retaining FEC's direct emitter and common fast paths.
Changes
Validation
Based on master
da14580f10fd19ae2361a8e91cfad4b2dfec182a. Full Release solution build and native runners pass on .NET 8, .NET 10 and .NET Framework 4.7.2, covering Standard and LightExpression. TestX passes 839 cases on .NET 8/10 and 812 on Framework; legacy runners pass 1,744 / 1,745 / 1,610 tests respectively.The imported runtime suite passes 35,064 executions, with zero failures and zero process crashes. Its 21 recoverable batches cover the upstream inventory. The two added compatibility/representation proof classes are run separately: 24 passed, zero failed.
Compatibility boundaries
The import uses strict FEC compilation and retains upstream assertions. System-specific IL/closure layouts and interpreter-only expectations are excluded only with linked behavior/rejection proofs. The run reports 58 skipped discovery entries, including skipped theory groups; these are not passes.
The two 10,000-deep stress cases remain explicitly deferred and are not claimed to work. Captures of by-ref parameters, pointers or byref-like values cannot escape into heap cells. Framework's unsupported filter emission is rejected.
See
test/FastExpressionCompiler.DotNetRuntimeTests/README.mdfor the import scope and reproduction instructions.