diff --git a/src/FastExpressionCompiler/FastExpressionCompiler.cs b/src/FastExpressionCompiler/FastExpressionCompiler.cs index 776f9e2cf..a9121696f 100644 --- a/src/FastExpressionCompiler/FastExpressionCompiler.cs +++ b/src/FastExpressionCompiler/FastExpressionCompiler.cs @@ -5196,7 +5196,7 @@ internal static void EmitLoadIndirectlyByRef(ILGenerator il, Type type) switch (Type.GetTypeCode(type)) { case TypeCode.Boolean: il.Demit(OpCodes.Ldind_U1); break; - case TypeCode.Char: il.Demit(OpCodes.Ldind_U1); break; + case TypeCode.Char: il.Demit(OpCodes.Ldind_U2); break; case TypeCode.Byte: il.Demit(OpCodes.Ldind_U1); break; case TypeCode.SByte: il.Demit(OpCodes.Ldind_I1); break; case TypeCode.Int16: il.Demit(OpCodes.Ldind_I2); break; @@ -5226,7 +5226,7 @@ private static void EmitStoreIndirectlyByRef(ILGenerator il, Type type) switch (Type.GetTypeCode(type)) { case TypeCode.Boolean: il.Demit(OpCodes.Stind_I1); break; - case TypeCode.Char: il.Demit(OpCodes.Stind_I1); break; + case TypeCode.Char: il.Demit(OpCodes.Stind_I2); break; case TypeCode.Byte: il.Demit(OpCodes.Stind_I1); break; case TypeCode.SByte: il.Demit(OpCodes.Stind_I1); break; case TypeCode.Int16: il.Demit(OpCodes.Stind_I2); break; diff --git a/test/FastExpressionCompiler.Benchmarks/results/standalone-byref-char/README.md b/test/FastExpressionCompiler.Benchmarks/results/standalone-byref-char/README.md new file mode 100644 index 000000000..6aa0fd234 --- /dev/null +++ b/test/FastExpressionCompiler.Benchmarks/results/standalone-byref-char/README.md @@ -0,0 +1,34 @@ +# Preserve both bytes of ref char reads and writes + +Use ldind.u2/stind.i2 for char managed references. The byte-sized opcodes currently truncate non-ASCII reads and leave the old high byte behind on writes. + +Two production opcode replacements. Two regression methods per expression flavor cover all read/write combinations of eight boundary values, including surrogate code units. + +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: **4 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. + +Allocation counts are unchanged on every shared workload measured, for both compilation and invocation. 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 / ref_char_read | 1093.2 → 1104.5 | 1.87 → 1.83 | 872 → 872 | 0 → 0 | +| net8.0 / Standard / ref_char_write | 1250.6 → 1301.6 | 1.86 → 1.92 | 880 → 880 | 0 → 0 | +| net8.0 / Light / ref_char_read | 1526.0 → 1617.0 | 1.89 → 2.02 | 872 → 872 | 0 → 0 | +| net8.0 / Light / ref_char_write | 1452.0 → 1273.2 | 1.88 → 1.90 | 880 → 880 | 0 → 0 | +| net10.0 / Standard / ref_char_read | 1044.4 → 1144.6 | 1.87 → 1.87 | 872 → 872 | 0 → 0 | +| net10.0 / Standard / ref_char_write | 1259.3 → 1341.7 | 1.84 → 1.87 | 880 → 880 | 0 → 0 | +| net10.0 / Light / ref_char_read | 988.6 → 965.8 | 1.91 → 1.87 | 872 → 872 | 0 → 0 | +| net10.0 / Light / ref_char_write | 1214.5 → 1201.1 | 1.96 → 1.98 | 880 → 880 | 0 → 0 | + +No affected row has a paired-sample bootstrap interval wholly above parity. These descriptive intervals, stored with the raw data, are based on nine temporal samples and are not a universal performance guarantee. + +Raw samples, harness sources, source hashes and validation logs are committed in `test/FastExpressionCompiler.Benchmarks/results/standalone-byref-char/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. diff --git a/test/FastExpressionCompiler.Benchmarks/results/standalone-byref-char/evidence.zip b/test/FastExpressionCompiler.Benchmarks/results/standalone-byref-char/evidence.zip new file mode 100644 index 000000000..6b880d036 Binary files /dev/null and b/test/FastExpressionCompiler.Benchmarks/results/standalone-byref-char/evidence.zip differ diff --git a/test/FastExpressionCompiler.TestsRunner.Net472/Program.cs b/test/FastExpressionCompiler.TestsRunner.Net472/Program.cs index ae61cdc2c..aa68d59bc 100644 --- a/test/FastExpressionCompiler.TestsRunner.Net472/Program.cs +++ b/test/FastExpressionCompiler.TestsRunner.Net472/Program.cs @@ -137,7 +137,9 @@ void Run(Func run, string name = null) Run(new LightExpression.UnitTests.CoalesceTests().Run); Run(new ConditionalOperatorsTests().Run); Run(new LightExpression.UnitTests.ConditionalOperatorsTests().Run); + Run(new ByRefCharacterWidthRegressionTests().Run); Run(new ConstantAndConversionTests().Run); + Run(new LightExpression.UnitTests.ByRefCharacterWidthRegressionTests().Run); Run(new LightExpression.UnitTests.ConstantAndConversionTests().Run); Run(new ConvertOperatorsTests().Run); Run(new LightExpression.UnitTests.ConvertOperatorsTests().Run); diff --git a/test/FastExpressionCompiler.TestsRunner/Program.cs b/test/FastExpressionCompiler.TestsRunner/Program.cs index 0f60c49fd..b7b04afc6 100644 --- a/test/FastExpressionCompiler.TestsRunner/Program.cs +++ b/test/FastExpressionCompiler.TestsRunner/Program.cs @@ -133,7 +133,9 @@ void Run(Func run, string name = null) Run(new LightExpression.UnitTests.CoalesceTests().Run); Run(new ConditionalOperatorsTests().Run); Run(new LightExpression.UnitTests.ConditionalOperatorsTests().Run); + Run(new ByRefCharacterWidthRegressionTests().Run); Run(new ConstantAndConversionTests().Run); + Run(new LightExpression.UnitTests.ByRefCharacterWidthRegressionTests().Run); Run(new LightExpression.UnitTests.ConstantAndConversionTests().Run); Run(new ConvertOperatorsTests().Run); Run(new LightExpression.UnitTests.ConvertOperatorsTests().Run); diff --git a/test/FastExpressionCompiler.UnitTests/ByRefCharacterWidthRegressionTests.cs b/test/FastExpressionCompiler.UnitTests/ByRefCharacterWidthRegressionTests.cs new file mode 100644 index 000000000..1d79c826a --- /dev/null +++ b/test/FastExpressionCompiler.UnitTests/ByRefCharacterWidthRegressionTests.cs @@ -0,0 +1,55 @@ +using System; +using S = System.Linq.Expressions.Expression; +#if LIGHT_EXPRESSION +namespace FastExpressionCompiler.LightExpression.UnitTests; +#else +namespace FastExpressionCompiler.UnitTests; +#endif + +public struct ByRefCharacterWidthRegressionTests : ITest +{ + public delegate char Read(ref char value); + public delegate char Write(ref char value, char replacement); + private static readonly char[] Values = { '\0', '\u00FF', '\u0100', '\u1234', '\uABCD', '\uD800', '\uDC00', '\uFFFF' }; + + public int Run() + { + Ref_character_reads_preserve_both_bytes(); + Ref_character_assignments_replace_both_bytes(); + return 2; + } + + public void Ref_character_reads_preserve_both_bytes() + { + var parameter = S.Parameter(typeof(char).MakeByRefType()); + var tree = S.Lambda(parameter, parameter); + var fast = tree.FromSysExpression().CompileFast(true); + Asserts.IsNotNull(fast); + var system = tree.Compile(); + foreach (var initial in Values) + { + var value = initial; + Asserts.AreEqual(initial, fast(ref value)); + Asserts.AreEqual(system(ref value), fast(ref value)); + Asserts.AreEqual(initial, value); + } + } + + public void Ref_character_assignments_replace_both_bytes() + { + var parameter = S.Parameter(typeof(char).MakeByRefType()); + var replacement = S.Parameter(typeof(char)); + var tree = S.Lambda(S.Assign(parameter, replacement), parameter, replacement); + var fast = tree.FromSysExpression().CompileFast(true); + Asserts.IsNotNull(fast); + var system = tree.Compile(); + foreach (var initial in Values) foreach (var next in Values) + { + var actual = initial; + var expected = initial; + Asserts.AreEqual(system(ref expected, next), fast(ref actual, next)); + Asserts.AreEqual(next, actual); + Asserts.AreEqual(expected, actual); + } + } +}