From 27312a437e9ffc4bc2c3ad63c4c7175d88c31631 Mon Sep 17 00:00:00 2001 From: maximv Date: Wed, 30 Sep 2020 10:46:16 +0300 Subject: [PATCH 1/6] passing the all ConstructorTests for the 261 --- Apex.Serialization/Apex.Serialization.csproj | 6 +- Apex.Serialization/Binary.Internal.cs | 13 ++-- .../Internal/DynamicCode.Arrays.cs | 5 +- .../Internal/DynamicCode.Helpers.cs | 2 +- Apex.Serialization/Internal/DynamicCode.cs | 21 ++++-- .../Internal/Reflection/FieldInfoModifier.cs | 6 +- .../Internal/Reflection/TypeFields.cs | 12 +-- .../VersionUniqueIdExpressionVisitor.cs | 73 +++++++++---------- Benchmark/BufferedStream.cs | 7 +- Benchmark/Program.cs | 2 +- 10 files changed, 81 insertions(+), 66 deletions(-) diff --git a/Apex.Serialization/Apex.Serialization.csproj b/Apex.Serialization/Apex.Serialization.csproj index 529da33..f3a8b50 100644 --- a/Apex.Serialization/Apex.Serialization.csproj +++ b/Apex.Serialization/Apex.Serialization.csproj @@ -15,10 +15,11 @@ enable enable true + $(DefineConstants);LIGHT_EXPRESSION - TRACE;DEV + $(DefineConstants);TRACE;DEV @@ -37,6 +38,9 @@ + + + diff --git a/Apex.Serialization/Binary.Internal.cs b/Apex.Serialization/Binary.Internal.cs index 65e46b1..f41c576 100644 --- a/Apex.Serialization/Binary.Internal.cs +++ b/Apex.Serialization/Binary.Internal.cs @@ -4,7 +4,8 @@ using System; using System.Collections.Generic; using System.Diagnostics; -using System.Linq.Expressions; +// using System.Linq.Expressions; +using FastExpressionCompiler.LightExpression; using System.Reflection; namespace Apex.Serialization @@ -455,22 +456,24 @@ public override int GetHashCode() private static Func CreateCloneFunc() { var p = Expression.Parameter(typeof(object)); - return Expression.Lambda>( + var f = Expression.Lambda>( Expression.Call(p, typeof(object).GetMethod("MemberwiseClone", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly)) , p - ).Compile(); + ).CompileFast(true); + return f; } private static Action CreateSetTargetAction() { var p = Expression.Parameter(typeof(Delegate)); var t = Expression.Parameter(typeof(object)); - return Expression.Lambda>( + var f = Expression.Lambda>( Expression.Assign( Expression.MakeMemberAccess(p, typeof(Delegate).GetField("_target", BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.NonPublic)), t) , p, t - ).Compile(); + ).CompileFast(true); + return f; } internal Delegate ReadFunction() diff --git a/Apex.Serialization/Internal/DynamicCode.Arrays.cs b/Apex.Serialization/Internal/DynamicCode.Arrays.cs index b7acae1..164fcea 100644 --- a/Apex.Serialization/Internal/DynamicCode.Arrays.cs +++ b/Apex.Serialization/Internal/DynamicCode.Arrays.cs @@ -3,7 +3,8 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; -using System.Linq.Expressions; +using static System.Linq.Expressions.ExpressionType; +using FastExpressionCompiler.LightExpression; namespace Apex.Serialization.Internal { @@ -44,7 +45,7 @@ internal static partial class DynamicCode var skipLabel = Expression.Label("skipWrite"); statements.Add( Expression.IfThen( - Expression.Equal(Expression.Constant(0), lengths.Aggregate((Expression)Expression.Empty(), (a,b) => a.NodeType == ExpressionType.Default ? (Expression)b : Expression.Or(a,b))), + Expression.Equal(Expression.Constant(0), lengths.Aggregate((Expression)Expression.Empty(), (a,b) => a.NodeType == Default ? (Expression)b : Expression.Or(a,b))), Expression.Goto(skipLabel) ) ); diff --git a/Apex.Serialization/Internal/DynamicCode.Helpers.cs b/Apex.Serialization/Internal/DynamicCode.Helpers.cs index 6c62e6f..d61e922 100644 --- a/Apex.Serialization/Internal/DynamicCode.Helpers.cs +++ b/Apex.Serialization/Internal/DynamicCode.Helpers.cs @@ -3,7 +3,7 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; -using System.Linq.Expressions; +using FastExpressionCompiler.LightExpression; using System.Reflection; using System.Runtime.InteropServices; using System.Runtime.Serialization; diff --git a/Apex.Serialization/Internal/DynamicCode.cs b/Apex.Serialization/Internal/DynamicCode.cs index 93c767f..f751a36 100644 --- a/Apex.Serialization/Internal/DynamicCode.cs +++ b/Apex.Serialization/Internal/DynamicCode.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; -using System.Linq.Expressions; +using FastExpressionCompiler.LightExpression; using System.Reflection; using System.Threading; using Apex.Serialization.Extensions; @@ -62,7 +62,7 @@ internal static DynamicCodeMethods.GeneratedDelegate GenerateWriteMethodImpl( GetWriteFieldExpression(x, isolatedSource, stream, output, settings, visitedTypes, 0))); var isolatedBody = Expression.Block(writeStatements); - var isolatedLambda = Expression.Lambda(isolatedBody, $"Apex.Serialization.Write_Isolated_{type.FullName}", new[] { isolatedSource, stream, output }).Compile(); + var isolatedLambda = Expression.Lambda(isolatedBody, $"Apex.Serialization.Write_Isolated_{type.FullName}", new[] { isolatedSource, stream, output }).CompileFast(true); return new DynamicCodeMethods.GeneratedDelegate { Delegate = isolatedLambda, SerializedVersionUniqueId = GetSerializedVersionUniqueId(type, isolatedBody) }; } @@ -93,7 +93,7 @@ internal static DynamicCodeMethods.GeneratedDelegate GenerateWriteMethodImpl( var finalBody = Expression.Block(localVariables, writeStatements); - var lambda = Expression.Lambda(finalBody, $"Apex.Serialization.Write_{type.FullName}", new[] { source, stream, output }).Compile(); + var lambda = Expression.Lambda(finalBody, $"Apex.Serialization.Write_{type.FullName}", new[] { source, stream, output }).CompileFast(ifFastFailedReturnNull: false); // todo: @perf may contain the DebugInfo which is not yet supported by FEC - that's why we are fallback for the System Compile if failed var uniqueId = GetSerializedVersionUniqueId(type, finalBody); @@ -587,7 +587,7 @@ internal static DynamicCodeMethods.GeneratedDelegate GenerateReadMethodImpl(T readStatements.AddRange(isolatedFields.Select(x => GetReadFieldExpression(x, isolatedResult, stream, output, settings, localVariables, visitedTypes, 0))); var isolatedBody = Expression.Block(localVariables, readStatements); - var isolatedLambda = Expression.Lambda(isolatedBody, $"Apex.Serialization.Read_Isolated_{type.FullName}", new[] { isolatedResult, stream, output }).Compile(); + var isolatedLambda = Expression.Lambda(isolatedBody, $"Apex.Serialization.Read_Isolated_{type.FullName}", new[] { isolatedResult, stream, output }).CompileFast(true); return new DynamicCodeMethods.GeneratedDelegate { Delegate = isolatedLambda, SerializedVersionUniqueId = GetSerializedVersionUniqueId(type, isolatedBody) }; } @@ -613,7 +613,16 @@ internal static DynamicCodeMethods.GeneratedDelegate GenerateReadMethodImpl(T } var finalBody = Expression.Block(localVariables, readStatements); - var lambda = Expression.Lambda(finalBody, $"Apex.Serialization.Read_{type.FullName}", new [] {stream, output}).Compile(); + var lambdaExpr = Expression.Lambda(finalBody, $"Apex.Serialization.Read_{type.FullName}", new [] {stream, output}); +#if DEBUG + var ep = lambdaExpr.ToExpressionString(out var ps, out var exprs, out var labelTargets, + stripNamespace: true, printType: (_, x) => x.Replace("-", "_")); + if (exprs.Count > 40) + { + var cs = lambdaExpr.ToCSharpString(); + } +#endif + var lambda = lambdaExpr.CompileFast(true); return new DynamicCodeMethods.GeneratedDelegate { Delegate = lambda, SerializedVersionUniqueId = GetSerializedVersionUniqueId(type, finalBody) }; } @@ -863,7 +872,7 @@ private static List GetReadStatementsForType(Type type, ImmutableSet Expression.Block( methods.Select(m => AfterDeserializeCallExpression(type, m, objectParameter, contextParameter)) ) - , $"AfterDeserialize_{type.FullName}", new[] {objectParameter, contextParameter}).Compile(); + , $"AfterDeserialize_{type.FullName}", new[] {objectParameter, contextParameter}).CompileFast(true); readStatements.Add(Expression.Call(output, QueueAfterDeserializationHook, Expression.Constant(action), result)); diff --git a/Apex.Serialization/Internal/Reflection/FieldInfoModifier.cs b/Apex.Serialization/Internal/Reflection/FieldInfoModifier.cs index 282b6e8..5166915 100644 --- a/Apex.Serialization/Internal/Reflection/FieldInfoModifier.cs +++ b/Apex.Serialization/Internal/Reflection/FieldInfoModifier.cs @@ -1,6 +1,6 @@ using System; using System.IO; -using System.Linq.Expressions; +using FastExpressionCompiler.LightExpression; using System.Reflection; using System.Security; @@ -47,7 +47,7 @@ static FieldInfoModifier() Expression.Label(returnLabel) ) , fieldInfoParam - ).Compile(); + ).CompileFast(true); SetFieldInfoReadonly = (Action)Expression.Lambda( Expression.Block( Expression.Assign(Expression.MakeMemberAccess(castedType, fieldInfo_m_Attributes), @@ -58,7 +58,7 @@ static FieldInfoModifier() Expression.Label(returnLabel) ) , fieldInfoParam - ).Compile(); + ).CompileFast(true); var s = Binary.Create(new Settings()); try diff --git a/Apex.Serialization/Internal/Reflection/TypeFields.cs b/Apex.Serialization/Internal/Reflection/TypeFields.cs index f2bca4d..4eb0643 100644 --- a/Apex.Serialization/Internal/Reflection/TypeFields.cs +++ b/Apex.Serialization/Internal/Reflection/TypeFields.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Linq.Expressions; +using FastExpressionCompiler.LightExpression; using System.Reflection; using System.Runtime.CompilerServices; @@ -23,11 +23,11 @@ static TypeFields() var logicallyNullMethod = typeof(MulticastDelegate).GetMethod("InvocationListLogicallyNull", BindingFlags.Instance | BindingFlags.NonPublic); - getInvocationCount = Expression.Lambda>(Expression.Field(delegateParam, "_invocationCount"), delegateParam).Compile(); - getInvocationList = Expression.Lambda>(Expression.Field(delegateParam, "_invocationList"), delegateParam).Compile(); - setInvocationCount = Expression.Lambda>(Expression.Assign(Expression.Field(delegateParam, "_invocationCount"), intPtrParam), delegateParam, intPtrParam).Compile(); - setInvocationList = Expression.Lambda>(Expression.Assign(Expression.Field(delegateParam, "_invocationList"), objectParam), delegateParam, objectParam).Compile(); - getInvocationListLogicallyNull = Expression.Lambda>(Expression.Call(delegateParam, logicallyNullMethod), delegateParam).Compile(); + getInvocationCount = Expression.Lambda>(Expression.Field(delegateParam, "_invocationCount"), delegateParam).CompileFast(true); + getInvocationList = Expression.Lambda>(Expression.Field(delegateParam, "_invocationList"), delegateParam).CompileFast(true); + setInvocationCount = Expression.Lambda>(Expression.Assign(Expression.Field(delegateParam, "_invocationCount"), intPtrParam), delegateParam, intPtrParam).CompileFast(true); + setInvocationList = Expression.Lambda>(Expression.Assign(Expression.Field(delegateParam, "_invocationList"), objectParam), delegateParam, objectParam).CompileFast(true); + getInvocationListLogicallyNull = Expression.Lambda>(Expression.Call(delegateParam, logicallyNullMethod), delegateParam).CompileFast(true); } private static DictionarySlim> _cache = new DictionarySlim>(); diff --git a/Apex.Serialization/Internal/VersionUniqueIdExpressionVisitor.cs b/Apex.Serialization/Internal/VersionUniqueIdExpressionVisitor.cs index 99255fc..eabdcd1 100644 --- a/Apex.Serialization/Internal/VersionUniqueIdExpressionVisitor.cs +++ b/Apex.Serialization/Internal/VersionUniqueIdExpressionVisitor.cs @@ -1,5 +1,5 @@ using System; -using System.Linq.Expressions; +using FastExpressionCompiler.LightExpression; using System.Reflection; namespace Apex.Serialization.Internal @@ -86,7 +86,7 @@ private void Combine(MethodInfo? a) } } - protected override Expression VisitBinary(BinaryExpression node) + protected internal override Expression VisitBinary(BinaryExpression node) { Combine(node.NodeType); Combine(node.Type); @@ -94,27 +94,27 @@ protected override Expression VisitBinary(BinaryExpression node) return base.VisitBinary(node); } - protected override Expression VisitBlock(BlockExpression node) + protected internal override Expression VisitBlock(BlockExpression node) { Combine(node.NodeType); Combine(node.Type); return base.VisitBlock(node); } - protected override CatchBlock VisitCatchBlock(CatchBlock node) + protected internal override CatchBlock VisitCatchBlock(CatchBlock node) { Combine(node.Test.FullName); return base.VisitCatchBlock(node); } - protected override Expression VisitConditional(ConditionalExpression node) + protected internal override Expression VisitConditional(ConditionalExpression node) { Combine(node.NodeType); Combine(node.Type); return base.VisitConditional(node); } - protected override Expression VisitConstant(ConstantExpression node) + protected internal override Expression VisitConstant(ConstantExpression node) { Combine(node.NodeType); Combine(node.Type); @@ -122,14 +122,14 @@ protected override Expression VisitConstant(ConstantExpression node) return base.VisitConstant(node); } - protected override Expression VisitDefault(DefaultExpression node) + protected internal override Expression VisitDefault(DefaultExpression node) { Combine(node.NodeType); Combine(node.Type); return base.VisitDefault(node); } - protected override Expression VisitDynamic(DynamicExpression node) + protected internal override Expression VisitDynamic(DynamicExpression node) { Combine(node.NodeType); Combine(node.Type); @@ -137,20 +137,20 @@ protected override Expression VisitDynamic(DynamicExpression node) return base.VisitDynamic(node); } - protected override ElementInit VisitElementInit(ElementInit node) + protected internal override ElementInit VisitElementInit(ElementInit node) { Combine(node.AddMethod); return base.VisitElementInit(node); } - protected override Expression VisitExtension(Expression node) + protected internal override Expression VisitExtension(Expression node) { Combine(node.NodeType); Combine(node.Type); return base.VisitExtension(node); } - protected override Expression VisitGoto(GotoExpression node) + protected internal override Expression VisitGoto(GotoExpression node) { Combine(node.NodeType); Combine(node.Type); @@ -158,7 +158,7 @@ protected override Expression VisitGoto(GotoExpression node) return base.VisitGoto(node); } - protected override Expression VisitIndex(IndexExpression node) + protected internal override Expression VisitIndex(IndexExpression node) { Combine(node.NodeType); Combine(node.Type); @@ -175,31 +175,31 @@ protected override Expression VisitIndex(IndexExpression node) return base.VisitIndex(node); } - protected override Expression VisitInvocation(InvocationExpression node) + protected internal override Expression VisitInvocation(InvocationExpression node) { Combine(node.NodeType); Combine(node.Type); return base.VisitInvocation(node); } - protected override Expression VisitLabel(LabelExpression node) + protected internal override Expression VisitLabel(LabelExpression node) { Combine(node.NodeType); Combine(node.Type); return base.VisitLabel(node); } - protected override LabelTarget VisitLabelTarget(LabelTarget? node) + protected internal override LabelTarget VisitLabelTarget(LabelTarget? node) { if (node != null) { Combine(node.Name); Combine(node.Type); } - return base.VisitLabelTarget(node); + return base.VisitLabelTarget(node!); } - protected override Expression VisitLambda(Expression node) + protected internal override Expression VisitLambda(Expression node) { Combine(node.NodeType); Combine(node.Type); @@ -207,21 +207,21 @@ protected override Expression VisitLambda(Expression node) return base.VisitLambda(node); } - protected override Expression VisitListInit(ListInitExpression node) + protected internal override Expression VisitListInit(ListInitExpression node) { Combine(node.NodeType); Combine(node.Type); return base.VisitListInit(node); } - protected override Expression VisitLoop(LoopExpression node) + protected internal override Expression VisitLoop(LoopExpression node) { Combine(node.NodeType); Combine(node.Type); return base.VisitLoop(node); } - protected override Expression VisitMember(MemberExpression node) + protected internal override Expression VisitMember(MemberExpression node) { Combine(node.NodeType); Combine(node.Type); @@ -229,20 +229,20 @@ protected override Expression VisitMember(MemberExpression node) return base.VisitMember(node); } - protected override MemberAssignment VisitMemberAssignment(MemberAssignment node) + protected internal override MemberAssignment VisitMemberAssignment(MemberAssignment node) { Combine(node.BindingType); Combine(node.Member); return base.VisitMemberAssignment(node); } - protected override MemberBinding VisitMemberBinding(MemberBinding node) + protected internal override MemberBinding VisitMemberBinding(MemberBinding node) { Combine(node.BindingType); Combine(node.Member); return base.VisitMemberBinding(node); } - protected override Expression VisitMemberInit(MemberInitExpression node) + protected internal override Expression VisitMemberInit(MemberInitExpression node) { Combine(node.NodeType); Combine(node.Type); @@ -250,21 +250,21 @@ protected override Expression VisitMemberInit(MemberInitExpression node) return base.VisitMemberInit(node); } - protected override MemberListBinding VisitMemberListBinding(MemberListBinding node) + protected internal override MemberListBinding VisitMemberListBinding(MemberListBinding node) { Combine(node.BindingType); Combine(node.Member); return base.VisitMemberListBinding(node); } - protected override MemberMemberBinding VisitMemberMemberBinding(MemberMemberBinding node) + protected internal override MemberMemberBinding VisitMemberMemberBinding(MemberMemberBinding node) { Combine(node.BindingType); Combine(node.Member); return base.VisitMemberMemberBinding(node); } - protected override Expression VisitMethodCall(MethodCallExpression node) + protected internal override Expression VisitMethodCall(MethodCallExpression node) { Combine(node.NodeType); Combine(node.Type); @@ -272,7 +272,7 @@ protected override Expression VisitMethodCall(MethodCallExpression node) return base.VisitMethodCall(node); } - protected override Expression VisitNew(NewExpression node) + protected internal override Expression VisitNew(NewExpression node) { Combine(node.NodeType); Combine(node.Type); @@ -280,14 +280,14 @@ protected override Expression VisitNew(NewExpression node) return base.VisitNew(node); } - protected override Expression VisitNewArray(NewArrayExpression node) + protected internal override Expression VisitNewArray(NewArrayExpression node) { Combine(node.NodeType); Combine(node.Type); return base.VisitNewArray(node); } - protected override Expression VisitParameter(ParameterExpression node) + protected internal override Expression VisitParameter(ParameterExpression node) { Combine(node.NodeType); Combine(node.Type); @@ -296,14 +296,14 @@ protected override Expression VisitParameter(ParameterExpression node) return base.VisitParameter(node); } - protected override Expression VisitRuntimeVariables(RuntimeVariablesExpression node) + protected internal override Expression VisitRuntimeVariables(RuntimeVariablesExpression node) { Combine(node.NodeType); Combine(node.Type); return base.VisitRuntimeVariables(node); } - protected override Expression VisitSwitch(SwitchExpression node) + protected internal override Expression VisitSwitch(SwitchExpression node) { Combine(node.NodeType); Combine(node.Type); @@ -311,19 +311,19 @@ protected override Expression VisitSwitch(SwitchExpression node) return base.VisitSwitch(node); } - protected override SwitchCase VisitSwitchCase(SwitchCase node) + protected internal override SwitchCase VisitSwitchCase(SwitchCase node) { Combine(1); return base.VisitSwitchCase(node); } - protected override Expression VisitTry(TryExpression node) + protected internal override Expression VisitTry(TryExpression node) { Combine(node.NodeType); return base.VisitTry(node); } - protected override Expression VisitTypeBinary(TypeBinaryExpression node) + protected internal override Expression VisitTypeBinary(TypeBinaryExpression node) { Combine(node.NodeType); Combine(node.Type); @@ -331,17 +331,16 @@ protected override Expression VisitTypeBinary(TypeBinaryExpression node) return base.VisitTypeBinary(node); } - protected override Expression VisitUnary(UnaryExpression node) + protected internal override Expression VisitUnary(UnaryExpression node) { Combine(node.NodeType); Combine(node.Type); return base.VisitUnary(node); } - protected override Expression VisitDebugInfo(DebugInfoExpression node) + protected internal override Expression VisitDebugInfo(DebugInfoExpression node) { Combine(node.Document.FileName); - return base.VisitDebugInfo(node); } } diff --git a/Benchmark/BufferedStream.cs b/Benchmark/BufferedStream.cs index 79ba01b..4d2b95a 100644 --- a/Benchmark/BufferedStream.cs +++ b/Benchmark/BufferedStream.cs @@ -3,8 +3,7 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq.Expressions; -using System.Text; +using FastExpressionCompiler.LightExpression; using BufferedStream = Apex.Serialization.Internal.BufferedStream; namespace Benchmark @@ -22,8 +21,8 @@ private static writeSig CreateWriteByteMethod() var p = Expression.Parameter(typeof(BufferedStream).MakeByRefType(), "stream"); var b = Expression.Parameter(typeof(byte), "b"); var lambda = Expression.Lambda(Expression.Call(p, BinaryStreamMethods.GenericMethods.WriteValueMethodInfo, b), p, b); - var compiledLamda = lambda.Compile(); - return (writeSig)compiledLamda; + var compiledLambda = lambda.CompileFast(true); + return (writeSig)compiledLambda; } [Benchmark] diff --git a/Benchmark/Program.cs b/Benchmark/Program.cs index 41eadf0..db445a9 100644 --- a/Benchmark/Program.cs +++ b/Benchmark/Program.cs @@ -3,7 +3,7 @@ using System.Diagnostics; using System.IO; using System.Linq; -using System.Linq.Expressions; +using FastExpressionCompiler.LightExpression; using System.Reflection; using Apex.Serialization; using BenchmarkDotNet.Columns; From bead5e0837945b8393da68c16cfa69d66c6f0a36 Mon Sep 17 00:00:00 2001 From: maximv Date: Tue, 20 Oct 2020 13:31:45 +0300 Subject: [PATCH 2/6] small things debug --- Apex.Serialization/Internal/DynamicCode.cs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/Apex.Serialization/Internal/DynamicCode.cs b/Apex.Serialization/Internal/DynamicCode.cs index f751a36..55314a3 100644 --- a/Apex.Serialization/Internal/DynamicCode.cs +++ b/Apex.Serialization/Internal/DynamicCode.cs @@ -62,7 +62,8 @@ internal static DynamicCodeMethods.GeneratedDelegate GenerateWriteMethodImpl( GetWriteFieldExpression(x, isolatedSource, stream, output, settings, visitedTypes, 0))); var isolatedBody = Expression.Block(writeStatements); - var isolatedLambda = Expression.Lambda(isolatedBody, $"Apex.Serialization.Write_Isolated_{type.FullName}", new[] { isolatedSource, stream, output }).CompileFast(true); + var isolatedLambda = Expression.Lambda(isolatedBody, $"Apex.Serialization.Write_Isolated_{type.FullName}", new[] { isolatedSource, stream, output }) + .CompileFast(true); return new DynamicCodeMethods.GeneratedDelegate { Delegate = isolatedLambda, SerializedVersionUniqueId = GetSerializedVersionUniqueId(type, isolatedBody) }; } @@ -93,7 +94,18 @@ internal static DynamicCodeMethods.GeneratedDelegate GenerateWriteMethodImpl( var finalBody = Expression.Block(localVariables, writeStatements); - var lambda = Expression.Lambda(finalBody, $"Apex.Serialization.Write_{type.FullName}", new[] { source, stream, output }).CompileFast(ifFastFailedReturnNull: false); // todo: @perf may contain the DebugInfo which is not yet supported by FEC - that's why we are fallback for the System Compile if failed + var lambdaExpr = Expression.Lambda(finalBody, $"Apex.Serialization.Write_{type.FullName}", new[] { source, stream, output }); + +#if DEBUG + var ep = lambdaExpr.ToExpressionString(out var ps, out var exprs, out var labelTargets, + stripNamespace: true, printType: (_, x) => x.Replace("-", "_")); + if (exprs.Count > 10) + { + var cs = lambdaExpr.ToCSharpString(); + } +#endif + + var lambda = lambdaExpr.CompileFast(true); var uniqueId = GetSerializedVersionUniqueId(type, finalBody); @@ -587,7 +599,8 @@ internal static DynamicCodeMethods.GeneratedDelegate GenerateReadMethodImpl(T readStatements.AddRange(isolatedFields.Select(x => GetReadFieldExpression(x, isolatedResult, stream, output, settings, localVariables, visitedTypes, 0))); var isolatedBody = Expression.Block(localVariables, readStatements); - var isolatedLambda = Expression.Lambda(isolatedBody, $"Apex.Serialization.Read_Isolated_{type.FullName}", new[] { isolatedResult, stream, output }).CompileFast(true); + var isolatedLambda = Expression.Lambda(isolatedBody, $"Apex.Serialization.Read_Isolated_{type.FullName}", new[] { isolatedResult, stream, output }) + .CompileFast(true); return new DynamicCodeMethods.GeneratedDelegate { Delegate = isolatedLambda, SerializedVersionUniqueId = GetSerializedVersionUniqueId(type, isolatedBody) }; } From e18e24c15366d7e70b3fcd14e911933ede4a1ba1 Mon Sep 17 00:00:00 2001 From: maximv Date: Fri, 23 Oct 2020 20:06:17 +0300 Subject: [PATCH 3/6] cleanup debug code --- Apex.Serialization/Binary.Internal.cs | 4 +-- Apex.Serialization/Internal/DynamicCode.cs | 28 ++++--------------- .../Internal/Reflection/FieldInfoModifier.cs | 4 +-- .../Internal/Reflection/TypeFields.cs | 10 +++---- Benchmark/BufferedStream.cs | 2 +- 5 files changed, 15 insertions(+), 33 deletions(-) diff --git a/Apex.Serialization/Binary.Internal.cs b/Apex.Serialization/Binary.Internal.cs index f41c576..f182771 100644 --- a/Apex.Serialization/Binary.Internal.cs +++ b/Apex.Serialization/Binary.Internal.cs @@ -459,7 +459,7 @@ private static Func CreateCloneFunc() var f = Expression.Lambda>( Expression.Call(p, typeof(object).GetMethod("MemberwiseClone", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly)) , p - ).CompileFast(true); + ).CompileFast(); return f; } @@ -472,7 +472,7 @@ private static Action CreateSetTargetAction() Expression.MakeMemberAccess(p, typeof(Delegate).GetField("_target", BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.NonPublic)), t) , p, t - ).CompileFast(true); + ).CompileFast(); return f; } diff --git a/Apex.Serialization/Internal/DynamicCode.cs b/Apex.Serialization/Internal/DynamicCode.cs index 55314a3..92b2bfc 100644 --- a/Apex.Serialization/Internal/DynamicCode.cs +++ b/Apex.Serialization/Internal/DynamicCode.cs @@ -63,7 +63,7 @@ internal static DynamicCodeMethods.GeneratedDelegate GenerateWriteMethodImpl( var isolatedBody = Expression.Block(writeStatements); var isolatedLambda = Expression.Lambda(isolatedBody, $"Apex.Serialization.Write_Isolated_{type.FullName}", new[] { isolatedSource, stream, output }) - .CompileFast(true); + .CompileFast(); return new DynamicCodeMethods.GeneratedDelegate { Delegate = isolatedLambda, SerializedVersionUniqueId = GetSerializedVersionUniqueId(type, isolatedBody) }; } @@ -95,17 +95,7 @@ internal static DynamicCodeMethods.GeneratedDelegate GenerateWriteMethodImpl( var finalBody = Expression.Block(localVariables, writeStatements); var lambdaExpr = Expression.Lambda(finalBody, $"Apex.Serialization.Write_{type.FullName}", new[] { source, stream, output }); - -#if DEBUG - var ep = lambdaExpr.ToExpressionString(out var ps, out var exprs, out var labelTargets, - stripNamespace: true, printType: (_, x) => x.Replace("-", "_")); - if (exprs.Count > 10) - { - var cs = lambdaExpr.ToCSharpString(); - } -#endif - - var lambda = lambdaExpr.CompileFast(true); + var lambda = lambdaExpr.CompileFast(); var uniqueId = GetSerializedVersionUniqueId(type, finalBody); @@ -600,7 +590,7 @@ internal static DynamicCodeMethods.GeneratedDelegate GenerateReadMethodImpl(T var isolatedBody = Expression.Block(localVariables, readStatements); var isolatedLambda = Expression.Lambda(isolatedBody, $"Apex.Serialization.Read_Isolated_{type.FullName}", new[] { isolatedResult, stream, output }) - .CompileFast(true); + .CompileFast(); return new DynamicCodeMethods.GeneratedDelegate { Delegate = isolatedLambda, SerializedVersionUniqueId = GetSerializedVersionUniqueId(type, isolatedBody) }; } @@ -627,15 +617,7 @@ internal static DynamicCodeMethods.GeneratedDelegate GenerateReadMethodImpl(T var finalBody = Expression.Block(localVariables, readStatements); var lambdaExpr = Expression.Lambda(finalBody, $"Apex.Serialization.Read_{type.FullName}", new [] {stream, output}); -#if DEBUG - var ep = lambdaExpr.ToExpressionString(out var ps, out var exprs, out var labelTargets, - stripNamespace: true, printType: (_, x) => x.Replace("-", "_")); - if (exprs.Count > 40) - { - var cs = lambdaExpr.ToCSharpString(); - } -#endif - var lambda = lambdaExpr.CompileFast(true); + var lambda = lambdaExpr.CompileFast(); return new DynamicCodeMethods.GeneratedDelegate { Delegate = lambda, SerializedVersionUniqueId = GetSerializedVersionUniqueId(type, finalBody) }; } @@ -885,7 +867,7 @@ private static List GetReadStatementsForType(Type type, ImmutableSet Expression.Block( methods.Select(m => AfterDeserializeCallExpression(type, m, objectParameter, contextParameter)) ) - , $"AfterDeserialize_{type.FullName}", new[] {objectParameter, contextParameter}).CompileFast(true); + , $"AfterDeserialize_{type.FullName}", new[] {objectParameter, contextParameter}).CompileFast(); readStatements.Add(Expression.Call(output, QueueAfterDeserializationHook, Expression.Constant(action), result)); diff --git a/Apex.Serialization/Internal/Reflection/FieldInfoModifier.cs b/Apex.Serialization/Internal/Reflection/FieldInfoModifier.cs index 5166915..7902ea0 100644 --- a/Apex.Serialization/Internal/Reflection/FieldInfoModifier.cs +++ b/Apex.Serialization/Internal/Reflection/FieldInfoModifier.cs @@ -47,7 +47,7 @@ static FieldInfoModifier() Expression.Label(returnLabel) ) , fieldInfoParam - ).CompileFast(true); + ).CompileFast(); SetFieldInfoReadonly = (Action)Expression.Lambda( Expression.Block( Expression.Assign(Expression.MakeMemberAccess(castedType, fieldInfo_m_Attributes), @@ -58,7 +58,7 @@ static FieldInfoModifier() Expression.Label(returnLabel) ) , fieldInfoParam - ).CompileFast(true); + ).CompileFast(); var s = Binary.Create(new Settings()); try diff --git a/Apex.Serialization/Internal/Reflection/TypeFields.cs b/Apex.Serialization/Internal/Reflection/TypeFields.cs index 4eb0643..4d1f5d5 100644 --- a/Apex.Serialization/Internal/Reflection/TypeFields.cs +++ b/Apex.Serialization/Internal/Reflection/TypeFields.cs @@ -23,11 +23,11 @@ static TypeFields() var logicallyNullMethod = typeof(MulticastDelegate).GetMethod("InvocationListLogicallyNull", BindingFlags.Instance | BindingFlags.NonPublic); - getInvocationCount = Expression.Lambda>(Expression.Field(delegateParam, "_invocationCount"), delegateParam).CompileFast(true); - getInvocationList = Expression.Lambda>(Expression.Field(delegateParam, "_invocationList"), delegateParam).CompileFast(true); - setInvocationCount = Expression.Lambda>(Expression.Assign(Expression.Field(delegateParam, "_invocationCount"), intPtrParam), delegateParam, intPtrParam).CompileFast(true); - setInvocationList = Expression.Lambda>(Expression.Assign(Expression.Field(delegateParam, "_invocationList"), objectParam), delegateParam, objectParam).CompileFast(true); - getInvocationListLogicallyNull = Expression.Lambda>(Expression.Call(delegateParam, logicallyNullMethod), delegateParam).CompileFast(true); + getInvocationCount = Expression.Lambda>(Expression.Field(delegateParam, "_invocationCount"), delegateParam).CompileFast(); + getInvocationList = Expression.Lambda>(Expression.Field(delegateParam, "_invocationList"), delegateParam).CompileFast(); + setInvocationCount = Expression.Lambda>(Expression.Assign(Expression.Field(delegateParam, "_invocationCount"), intPtrParam), delegateParam, intPtrParam).CompileFast(); + setInvocationList = Expression.Lambda>(Expression.Assign(Expression.Field(delegateParam, "_invocationList"), objectParam), delegateParam, objectParam).CompileFast(); + getInvocationListLogicallyNull = Expression.Lambda>(Expression.Call(delegateParam, logicallyNullMethod), delegateParam).CompileFast(); } private static DictionarySlim> _cache = new DictionarySlim>(); diff --git a/Benchmark/BufferedStream.cs b/Benchmark/BufferedStream.cs index 4d2b95a..2816d10 100644 --- a/Benchmark/BufferedStream.cs +++ b/Benchmark/BufferedStream.cs @@ -21,7 +21,7 @@ private static writeSig CreateWriteByteMethod() var p = Expression.Parameter(typeof(BufferedStream).MakeByRefType(), "stream"); var b = Expression.Parameter(typeof(byte), "b"); var lambda = Expression.Lambda(Expression.Call(p, BinaryStreamMethods.GenericMethods.WriteValueMethodInfo, b), p, b); - var compiledLambda = lambda.CompileFast(true); + var compiledLambda = lambda.CompileFast(); return (writeSig)compiledLambda; } From de6c59e0c4fda3111293fb9c9c94a1028c056ff9 Mon Sep 17 00:00:00 2001 From: maximv Date: Fri, 23 Oct 2020 20:11:19 +0300 Subject: [PATCH 4/6] switching from the fec sources to the package reference --- Apex.Serialization/Apex.Serialization.csproj | 4 +- .../VersionUniqueIdExpressionVisitor.cs | 68 +++++++++---------- 2 files changed, 35 insertions(+), 37 deletions(-) diff --git a/Apex.Serialization/Apex.Serialization.csproj b/Apex.Serialization/Apex.Serialization.csproj index f3a8b50..3fbab58 100644 --- a/Apex.Serialization/Apex.Serialization.csproj +++ b/Apex.Serialization/Apex.Serialization.csproj @@ -38,9 +38,6 @@ - - - @@ -52,6 +49,7 @@ + diff --git a/Apex.Serialization/Internal/VersionUniqueIdExpressionVisitor.cs b/Apex.Serialization/Internal/VersionUniqueIdExpressionVisitor.cs index eabdcd1..20db333 100644 --- a/Apex.Serialization/Internal/VersionUniqueIdExpressionVisitor.cs +++ b/Apex.Serialization/Internal/VersionUniqueIdExpressionVisitor.cs @@ -86,7 +86,7 @@ private void Combine(MethodInfo? a) } } - protected internal override Expression VisitBinary(BinaryExpression node) + protected override Expression VisitBinary(BinaryExpression node) { Combine(node.NodeType); Combine(node.Type); @@ -94,27 +94,27 @@ protected internal override Expression VisitBinary(BinaryExpression node) return base.VisitBinary(node); } - protected internal override Expression VisitBlock(BlockExpression node) + protected override Expression VisitBlock(BlockExpression node) { Combine(node.NodeType); Combine(node.Type); return base.VisitBlock(node); } - protected internal override CatchBlock VisitCatchBlock(CatchBlock node) + protected override CatchBlock VisitCatchBlock(CatchBlock node) { Combine(node.Test.FullName); return base.VisitCatchBlock(node); } - protected internal override Expression VisitConditional(ConditionalExpression node) + protected override Expression VisitConditional(ConditionalExpression node) { Combine(node.NodeType); Combine(node.Type); return base.VisitConditional(node); } - protected internal override Expression VisitConstant(ConstantExpression node) + protected override Expression VisitConstant(ConstantExpression node) { Combine(node.NodeType); Combine(node.Type); @@ -122,14 +122,14 @@ protected internal override Expression VisitConstant(ConstantExpression node) return base.VisitConstant(node); } - protected internal override Expression VisitDefault(DefaultExpression node) + protected override Expression VisitDefault(DefaultExpression node) { Combine(node.NodeType); Combine(node.Type); return base.VisitDefault(node); } - protected internal override Expression VisitDynamic(DynamicExpression node) + protected override Expression VisitDynamic(DynamicExpression node) { Combine(node.NodeType); Combine(node.Type); @@ -137,20 +137,20 @@ protected internal override Expression VisitDynamic(DynamicExpression node) return base.VisitDynamic(node); } - protected internal override ElementInit VisitElementInit(ElementInit node) + protected override ElementInit VisitElementInit(ElementInit node) { Combine(node.AddMethod); return base.VisitElementInit(node); } - protected internal override Expression VisitExtension(Expression node) + protected override Expression VisitExtension(Expression node) { Combine(node.NodeType); Combine(node.Type); return base.VisitExtension(node); } - protected internal override Expression VisitGoto(GotoExpression node) + protected override Expression VisitGoto(GotoExpression node) { Combine(node.NodeType); Combine(node.Type); @@ -158,7 +158,7 @@ protected internal override Expression VisitGoto(GotoExpression node) return base.VisitGoto(node); } - protected internal override Expression VisitIndex(IndexExpression node) + protected override Expression VisitIndex(IndexExpression node) { Combine(node.NodeType); Combine(node.Type); @@ -175,21 +175,21 @@ protected internal override Expression VisitIndex(IndexExpression node) return base.VisitIndex(node); } - protected internal override Expression VisitInvocation(InvocationExpression node) + protected override Expression VisitInvocation(InvocationExpression node) { Combine(node.NodeType); Combine(node.Type); return base.VisitInvocation(node); } - protected internal override Expression VisitLabel(LabelExpression node) + protected override Expression VisitLabel(LabelExpression node) { Combine(node.NodeType); Combine(node.Type); return base.VisitLabel(node); } - protected internal override LabelTarget VisitLabelTarget(LabelTarget? node) + protected override LabelTarget VisitLabelTarget(LabelTarget? node) { if (node != null) { @@ -199,7 +199,7 @@ protected internal override LabelTarget VisitLabelTarget(LabelTarget? node) return base.VisitLabelTarget(node!); } - protected internal override Expression VisitLambda(Expression node) + protected override Expression VisitLambda(Expression node) { Combine(node.NodeType); Combine(node.Type); @@ -207,21 +207,21 @@ protected internal override Expression VisitLambda(Expression node) return base.VisitLambda(node); } - protected internal override Expression VisitListInit(ListInitExpression node) + protected override Expression VisitListInit(ListInitExpression node) { Combine(node.NodeType); Combine(node.Type); return base.VisitListInit(node); } - protected internal override Expression VisitLoop(LoopExpression node) + protected override Expression VisitLoop(LoopExpression node) { Combine(node.NodeType); Combine(node.Type); return base.VisitLoop(node); } - protected internal override Expression VisitMember(MemberExpression node) + protected override Expression VisitMember(MemberExpression node) { Combine(node.NodeType); Combine(node.Type); @@ -229,20 +229,20 @@ protected internal override Expression VisitMember(MemberExpression node) return base.VisitMember(node); } - protected internal override MemberAssignment VisitMemberAssignment(MemberAssignment node) + protected override MemberAssignment VisitMemberAssignment(MemberAssignment node) { Combine(node.BindingType); Combine(node.Member); return base.VisitMemberAssignment(node); } - protected internal override MemberBinding VisitMemberBinding(MemberBinding node) + protected override MemberBinding VisitMemberBinding(MemberBinding node) { Combine(node.BindingType); Combine(node.Member); return base.VisitMemberBinding(node); } - protected internal override Expression VisitMemberInit(MemberInitExpression node) + protected override Expression VisitMemberInit(MemberInitExpression node) { Combine(node.NodeType); Combine(node.Type); @@ -250,21 +250,21 @@ protected internal override Expression VisitMemberInit(MemberInitExpression node return base.VisitMemberInit(node); } - protected internal override MemberListBinding VisitMemberListBinding(MemberListBinding node) + protected override MemberListBinding VisitMemberListBinding(MemberListBinding node) { Combine(node.BindingType); Combine(node.Member); return base.VisitMemberListBinding(node); } - protected internal override MemberMemberBinding VisitMemberMemberBinding(MemberMemberBinding node) + protected override MemberMemberBinding VisitMemberMemberBinding(MemberMemberBinding node) { Combine(node.BindingType); Combine(node.Member); return base.VisitMemberMemberBinding(node); } - protected internal override Expression VisitMethodCall(MethodCallExpression node) + protected override Expression VisitMethodCall(MethodCallExpression node) { Combine(node.NodeType); Combine(node.Type); @@ -272,7 +272,7 @@ protected internal override Expression VisitMethodCall(MethodCallExpression node return base.VisitMethodCall(node); } - protected internal override Expression VisitNew(NewExpression node) + protected override Expression VisitNew(NewExpression node) { Combine(node.NodeType); Combine(node.Type); @@ -280,14 +280,14 @@ protected internal override Expression VisitNew(NewExpression node) return base.VisitNew(node); } - protected internal override Expression VisitNewArray(NewArrayExpression node) + protected override Expression VisitNewArray(NewArrayExpression node) { Combine(node.NodeType); Combine(node.Type); return base.VisitNewArray(node); } - protected internal override Expression VisitParameter(ParameterExpression node) + protected override Expression VisitParameter(ParameterExpression node) { Combine(node.NodeType); Combine(node.Type); @@ -296,14 +296,14 @@ protected internal override Expression VisitParameter(ParameterExpression node) return base.VisitParameter(node); } - protected internal override Expression VisitRuntimeVariables(RuntimeVariablesExpression node) + protected override Expression VisitRuntimeVariables(RuntimeVariablesExpression node) { Combine(node.NodeType); Combine(node.Type); return base.VisitRuntimeVariables(node); } - protected internal override Expression VisitSwitch(SwitchExpression node) + protected override Expression VisitSwitch(SwitchExpression node) { Combine(node.NodeType); Combine(node.Type); @@ -311,19 +311,19 @@ protected internal override Expression VisitSwitch(SwitchExpression node) return base.VisitSwitch(node); } - protected internal override SwitchCase VisitSwitchCase(SwitchCase node) + protected override SwitchCase VisitSwitchCase(SwitchCase node) { Combine(1); return base.VisitSwitchCase(node); } - protected internal override Expression VisitTry(TryExpression node) + protected override Expression VisitTry(TryExpression node) { Combine(node.NodeType); return base.VisitTry(node); } - protected internal override Expression VisitTypeBinary(TypeBinaryExpression node) + protected override Expression VisitTypeBinary(TypeBinaryExpression node) { Combine(node.NodeType); Combine(node.Type); @@ -331,14 +331,14 @@ protected internal override Expression VisitTypeBinary(TypeBinaryExpression node return base.VisitTypeBinary(node); } - protected internal override Expression VisitUnary(UnaryExpression node) + protected override Expression VisitUnary(UnaryExpression node) { Combine(node.NodeType); Combine(node.Type); return base.VisitUnary(node); } - protected internal override Expression VisitDebugInfo(DebugInfoExpression node) + protected override Expression VisitDebugInfo(DebugInfoExpression node) { Combine(node.Document.FileName); return base.VisitDebugInfo(node); From b6ada0fdea77b09dbf61447b30fed7d67ac51ec5 Mon Sep 17 00:00:00 2001 From: dadhi Date: Thu, 18 Mar 2021 09:10:52 +0000 Subject: [PATCH 5/6] updating the FEC to V3 RTM --- Apex.Serialization/Apex.Serialization.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Apex.Serialization/Apex.Serialization.csproj b/Apex.Serialization/Apex.Serialization.csproj index 3fbab58..122b1f0 100644 --- a/Apex.Serialization/Apex.Serialization.csproj +++ b/Apex.Serialization/Apex.Serialization.csproj @@ -49,7 +49,7 @@ - + From d43be3cd211c98fa774b1b10fe95dd46d06980e8 Mon Sep 17 00:00:00 2001 From: dadhi Date: Wed, 21 Apr 2021 17:12:06 +0300 Subject: [PATCH 6/6] update FastExpressionCompiler.LightExpression to v3.0.5 --- Apex.Serialization/Apex.Serialization.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Apex.Serialization/Apex.Serialization.csproj b/Apex.Serialization/Apex.Serialization.csproj index 45e242d..40e7813 100644 --- a/Apex.Serialization/Apex.Serialization.csproj +++ b/Apex.Serialization/Apex.Serialization.csproj @@ -49,7 +49,7 @@ - +