Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/FastExpressionCompiler/FastExpressionCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4453,6 +4453,9 @@ private static bool TryEmitListInit(ListInitExpression expr, ILGenerator il, ref
var inits = expr.Initializers;
var initCount = inits.Count;
var ok = true;
MethodInfo lastAddMethod = null;
ParameterInfo[] methodParams = null;
var popAddResult = false;

// see the TryEmitMethodCall for the reason of the callFlags
var callFlags = (parent
Expand All @@ -4468,7 +4471,12 @@ private static bool TryEmitListInit(ListInitExpression expr, ILGenerator il, ref

var elemInit = inits.GetArgument(i);
var method = elemInit.AddMethod;
var methodParams = method.GetParameters();
if (!ReferenceEquals(method, lastAddMethod))
{
lastAddMethod = method;
methodParams = method.GetParameters();
popAddResult = method.ReturnType != typeof(void);
}
var addArgs = elemInit.GetArgExprs();
var addArgCount = addArgs.GetArgCount();

Expand All @@ -4485,11 +4493,14 @@ private static bool TryEmitListInit(ListInitExpression expr, ILGenerator il, ref
il.Demit(OpCodes.Constrained, exprType); // todo: @clarify it is a value type so... can we de-virtualize the call?
ok = EmitVirtualMethodCall(il, method);
}
if (ok && popAddResult)
il.Demit(OpCodes.Pop);
}

if (valueVarIndex != -1)
EmitLoadLocalVariable(il, valueVarIndex);
return ok;
context.LastEmitIsAddress = false;
return ok && il.EmitPopIfIgnoreResult(parent);
}

// the `right = null` argument indicates the increment/decrement operation
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Discard non-void list initializer Add results

Discard each non-void Add return value during list initialization and discard the final collection when its enclosing expression ignores the result. HashSet and custom non-void Add methods currently leave invalid values on the evaluation stack.

A small change confined to the existing list emitter, including reuse of adjacent Add-method signatures. Five regression methods per expression flavor cover HashSet, mutable struct collections, ignored results, side-effect order, subsequent member access, and alternating overloads with different return/argument types.

Independent extraction from #558, based directly on `dadhi/master` `6702eab6`. It requires neither #558 nor either of the other standalone fixes. All six merge orders of the three standalone fixes were checked without conflicts. #558 is unchanged.

## Validation

Focused regressions: **10 methods passed per runtime**, covering Standard and LightExpression on .NET 8, .NET 10, and .NET Framework 4.7.2. Loading the untouched master compiler with the same regression assembly reproduces the failure on .NET 8. All three Release solution builds pass with zero warnings/errors.

Full .NET 8/.NET 10 native runners were also executed. Each reports the same existing failure in `LightExpressionTests.Binary_operators_with_methods_and_nullable_operands_are_the_same_as_system` (two nullable MultiplyChecked rows produce OverflowException). The identical failure reproduces on untouched master; no new full-run failures were observed. No tests are skipped or weakened to obtain this result.

## Performance and allocations

Windows ARM64, Release, SDK 10.0.301. Before and after compiler assemblies are loaded in separate AssemblyLoadContexts in the same process. Nine alternating-order samples per phase, common operation counts, approximately 35 ms per side, warmed delegates, tiering disabled, and the process pinned to one available logical CPU. GC runs outside samples and a no-GC region isolates allocation throughput. Compilation reuses prebuilt expression trees; execution reuses compiled delegates. This measures CompileFast and delegate invocation, not tree construction. Ratios for previously incorrect/invalid cases are intentionally not claimed.

Compilation allocations decrease for the repeated Add-method control; invocation allocations are unchanged. No shared workload has an allocation increase. The raw timing samples and all unchanged controls are retained: small timing differences and code-placement/host variation do not establish universal throughput parity. These are correctness fixes with no new runtime helper calls or storage allocations; no broad performance improvement is claimed.

| Runtime / flavor / workload | Compile ns, master → fix | Invoke ns, master → fix | Compile B/op | Invoke B/op |
| --- | ---: | ---: | ---: | ---: |
| net8.0 / Standard / void_add_list_control | 1753.9 → 1635.6 | 22.91 → 17.80 | 1240 → 1176 | 72 → 72 |
| net8.0 / Light / void_add_list_control | 1432.3 → 1322.8 | 11.53 → 11.41 | 1240 → 1176 | 72 → 72 |
| net10.0 / Standard / void_add_list_control | 1490.5 → 1618.3 | 12.07 → 10.88 | 1240 → 1176 | 72 → 72 |
| net10.0 / Light / void_add_list_control | 1895.7 → 1663.5 | 10.02 → 8.57 | 1240 → 1176 | 72 → 72 |

The table uses the already-valid void-Add List<int> control. Its emitted return-value IL is unchanged. The broken non-void path has no valid before/after runtime ratio; it now passes the correctness checks. The standalone pilot records also include the fixed-only HashSet workload. An initial version checked ReturnType for every initializer and cost compilation time. This version reuses the last Add-method signature, avoiding repeated GetParameters arrays and ReturnType lookups. Alternating overloads are covered by a separate regression.

Raw samples, harness sources, source hashes and validation logs are committed in `test/FastExpressionCompiler.Benchmarks/results/standalone-list-initializer/evidence.zip`. Earlier separate-process timing attempts had unstable unchanged controls; their raw results are retained as exploratory evidence and are not the final paired comparison.
Binary file not shown.
2 changes: 2 additions & 0 deletions test/FastExpressionCompiler.TestsRunner.Net472/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ void Run(Func<int> run, string name = null)
Run(new LightExpression.UnitTests.HoistedLambdaExprTests().Run);
Run(new LoopTests().Run);
Run(new LightExpression.UnitTests.LoopTests().Run);
Run(new NonVoidListInitializerRegressionTests().Run);
Run(new ListInitTests().Run);
Run(new LightExpression.UnitTests.NonVoidListInitializerRegressionTests().Run);
Run(new LightExpression.UnitTests.ListInitTests().Run);
Run(new ManuallyComposedExprTests().Run);
Run(new LightExpression.UnitTests.ManuallyComposedExprTests().Run);
Expand Down
2 changes: 2 additions & 0 deletions test/FastExpressionCompiler.TestsRunner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ void Run(Func<int> run, string name = null)
Run(new LightExpression.UnitTests.HoistedLambdaExprTests().Run);
Run(new LoopTests().Run);
Run(new LightExpression.UnitTests.LoopTests().Run);
Run(new NonVoidListInitializerRegressionTests().Run);
Run(new ListInitTests().Run);
Run(new LightExpression.UnitTests.NonVoidListInitializerRegressionTests().Run);
Run(new LightExpression.UnitTests.ListInitTests().Run);
Run(new ManuallyComposedExprTests().Run);
Run(new LightExpression.UnitTests.ManuallyComposedExprTests().Run);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using System;
using System.Collections;
using System.Collections.Generic;
#if LIGHT_EXPRESSION
using Expr = FastExpressionCompiler.LightExpression.Expression;
using ElementInitializer = FastExpressionCompiler.LightExpression.ElementInit;
namespace FastExpressionCompiler.LightExpression.UnitTests;
#else
using Expr = System.Linq.Expressions.Expression;
using ElementInitializer = System.Linq.Expressions.ElementInit;
namespace FastExpressionCompiler.UnitTests;
#endif
public struct NonVoidListInitializerRegressionTests : ITest
{
public int Run()
{
Hashset_initializer_discards_boolean_add_results();
Struct_initializer_discards_integer_add_results();
Ignored_initializers_preserve_add_effects();
Struct_initializer_result_supports_member_access();
Alternating_add_overloads_keep_their_own_signatures();
return 5;
}
public void Hashset_initializer_discards_boolean_add_results()
{
var expression = Expr.Lambda<Func<HashSet<int>>>(CreateInitializer(typeof(HashSet<int>), 1, 2, 4, 16, 42, 2));
var fast = expression.CompileFast(true);
Asserts.IsNotNull(fast);
var result = fast();
Asserts.AreEqual(5, result.Count);
foreach (var value in new[] { 1, 2, 4, 16, 42 }) Asserts.IsTrue(result.Contains(value));
}
public void Struct_initializer_discards_integer_add_results()
{
var expression = Expr.Lambda<Func<ValueList>>(CreateInitializer(typeof(ValueList), 5, 6, 7, 8));
var fast = expression.CompileFast(true);
Asserts.IsNotNull(fast);
Trace = "";
var result = fast();
Asserts.AreEqual("5;6;7;8;", Trace);
Asserts.AreEqual(4, result.Count);
Asserts.AreEqual(5, result[0]); Asserts.AreEqual(6, result[1]); Asserts.AreEqual(7, result[2]); Asserts.AreEqual(8, result[3]);
}
public void Ignored_initializers_preserve_add_effects()
{
foreach (var type in new[] { typeof(ReferenceList), typeof(ValueList) })
{
var body = CreateInitializer(type, 5, 6);
var fast = Expr.Lambda<Action>(Expr.Block(body, Expr.Empty())).CompileFast(true);
Asserts.IsNotNull(fast);
Trace = "";
fast();
Asserts.AreEqual("5;6;", Trace);
}
}
public void Struct_initializer_result_supports_member_access()
{
var body = CreateInitializer(typeof(ValueList), 5, 6);
var fast = Expr.Lambda<Func<int>>(Expr.Property(body, nameof(ValueList.Count))).CompileFast(true);
Asserts.IsNotNull(fast);
Asserts.AreEqual(2, fast());
}
public void Alternating_add_overloads_keep_their_own_signatures()
{
var addInt = typeof(OverloadedList).GetMethod("Add", new[] { typeof(int) });
var addString = typeof(OverloadedList).GetMethod("Add", new[] { typeof(string) });
var body = Expr.ListInit(Expr.New(typeof(OverloadedList)),
Expr.ElementInit(addInt, Expr.Constant(1)),
Expr.ElementInit(addString, Expr.Constant("x")),
Expr.ElementInit(addInt, Expr.Constant(2)));
var fast = Expr.Lambda<Func<OverloadedList>>(body).CompileFast(true);
Asserts.IsNotNull(fast);
Trace = "";
fast();
Asserts.AreEqual("1;x;2;", Trace);
}
public class OverloadedList : IEnumerable<int>
{
public bool Add(int value) { Trace += value + ";"; return true; }
public void Add(string value) { Trace += value + ";"; }
public IEnumerator<int> GetEnumerator() => ((IEnumerable<int>)Array.Empty<int>()).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
private static Expr CreateInitializer(Type type, params int[] values)
{
var add = type.GetMethod("Add", new[] { typeof(int) });
var initializers = new ElementInitializer[values.Length];
for (var i = 0; i != values.Length; ++i) initializers[i] = Expr.ElementInit(add, Expr.Constant(values[i]));
return Expr.ListInit(Expr.New(type), initializers);
}
public static string Trace;
public struct ValueList : IEnumerable<int>
{
private List<int> _values;
public int Add(int value) { Trace += value + ";"; (_values ??= new List<int>()).Add(value); return value; }
public int Count => _values?.Count ?? 0;
public int this[int index] => _values[index];
public IEnumerator<int> GetEnumerator() => (_values ??= new List<int>()).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
public class ReferenceList : IEnumerable<int>
{
private readonly List<int> _values = new List<int>();
public int Add(int value) { Trace += value + ";"; _values.Add(value); return value; }
public IEnumerator<int> GetEnumerator() => _values.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}
Loading