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
4 changes: 3 additions & 1 deletion Apex.Serialization/Apex.Serialization.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
<Nullable>enable</Nullable>
<NullableContextOptions>enable</NullableContextOptions>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<DefineConstants>$(DefineConstants);LIGHT_EXPRESSION</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DefineConstants>TRACE;DEV</DefineConstants>
<DefineConstants>$(DefineConstants);TRACE;DEV</DefineConstants>
</PropertyGroup>

<PropertyGroup>
Expand Down Expand Up @@ -48,6 +49,7 @@
<PackageReference Include="System.Collections.Immutable" Version="5.0.0" />
<PackageReference Include="System.Memory" Version="4.5.4" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="5.0.0" />
<PackageReference Include="FastExpressionCompiler.LightExpression" Version="3.0.5" />
</ItemGroup>

</Project>
13 changes: 8 additions & 5 deletions Apex.Serialization/Binary.Internal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -455,22 +456,24 @@ public override int GetHashCode()
private static Func<object, object> CreateCloneFunc()
{
var p = Expression.Parameter(typeof(object));
return Expression.Lambda<Func<object, object>>(
var f = Expression.Lambda<Func<object, object>>(
Expression.Call(p, typeof(object).GetMethod("MemberwiseClone", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly)!)
, p
).Compile();
).CompileFast();
return f;
}

private static Action<Delegate, object> CreateSetTargetAction()
{
var p = Expression.Parameter(typeof(Delegate));
var t = Expression.Parameter(typeof(object));
return Expression.Lambda<Action<Delegate, object>>(
var f = Expression.Lambda<Action<Delegate, object>>(
Expression.Assign(
Expression.MakeMemberAccess(p,
typeof(Delegate).GetField("_target", BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.NonPublic)!), t)
, p, t
).Compile();
).CompileFast();
return f;
}

internal Delegate ReadFunction()
Expand Down
5 changes: 3 additions & 2 deletions Apex.Serialization/Internal/DynamicCode.Arrays.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -44,7 +45,7 @@ internal static partial class DynamicCode<TStream, TBinary>
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)
)
);
Expand Down
2 changes: 1 addition & 1 deletion Apex.Serialization/Internal/DynamicCode.Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 10 additions & 6 deletions Apex.Serialization/Internal/DynamicCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,7 +62,8 @@ internal static DynamicCodeMethods.GeneratedDelegate GenerateWriteMethodImpl<T>(
GetWriteFieldExpression(x, isolatedSource, stream, output, settings, visitedTypes, 0)));

var isolatedBody = Expression.Block(writeStatements);
var isolatedLambda = Expression.Lambda<T>(isolatedBody, $"Apex.Serialization.Write_Isolated_{type.FullName}", new[] { isolatedSource, stream, output }).Compile();
var isolatedLambda = Expression.Lambda<T>(isolatedBody, $"Apex.Serialization.Write_Isolated_{type.FullName}", new[] { isolatedSource, stream, output })
.CompileFast();

return new DynamicCodeMethods.GeneratedDelegate { Delegate = isolatedLambda, SerializedVersionUniqueId = GetSerializedVersionUniqueId(type, isolatedBody) };
}
Expand Down Expand Up @@ -93,7 +94,8 @@ internal static DynamicCodeMethods.GeneratedDelegate GenerateWriteMethodImpl<T>(

var finalBody = Expression.Block(localVariables, writeStatements);

var lambda = Expression.Lambda<T>(finalBody, $"Apex.Serialization.Write_{type.FullName}", new[] { source, stream, output }).Compile();
var lambdaExpr = Expression.Lambda<T>(finalBody, $"Apex.Serialization.Write_{type.FullName}", new[] { source, stream, output });
var lambda = lambdaExpr.CompileFast();

var uniqueId = GetSerializedVersionUniqueId(type, finalBody);

Expand Down Expand Up @@ -587,7 +589,8 @@ internal static DynamicCodeMethods.GeneratedDelegate GenerateReadMethodImpl<T>(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<T>(isolatedBody, $"Apex.Serialization.Read_Isolated_{type.FullName}", new[] { isolatedResult, stream, output }).Compile();
var isolatedLambda = Expression.Lambda<T>(isolatedBody, $"Apex.Serialization.Read_Isolated_{type.FullName}", new[] { isolatedResult, stream, output })
.CompileFast();

return new DynamicCodeMethods.GeneratedDelegate { Delegate = isolatedLambda, SerializedVersionUniqueId = GetSerializedVersionUniqueId(type, isolatedBody) };
}
Expand All @@ -613,7 +616,8 @@ internal static DynamicCodeMethods.GeneratedDelegate GenerateReadMethodImpl<T>(T
}

var finalBody = Expression.Block(localVariables, readStatements);
var lambda = Expression.Lambda<T>(finalBody, $"Apex.Serialization.Read_{type.FullName}", new [] {stream, output}).Compile();
var lambdaExpr = Expression.Lambda<T>(finalBody, $"Apex.Serialization.Read_{type.FullName}", new [] {stream, output});
var lambda = lambdaExpr.CompileFast();

return new DynamicCodeMethods.GeneratedDelegate { Delegate = lambda, SerializedVersionUniqueId = GetSerializedVersionUniqueId(type, finalBody) };
}
Expand Down Expand Up @@ -863,7 +867,7 @@ private static List<Expression> 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();

readStatements.Add(Expression.Call(output, QueueAfterDeserializationHook,
Expression.Constant(action), result));
Expand Down
6 changes: 3 additions & 3 deletions Apex.Serialization/Internal/Reflection/FieldInfoModifier.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.IO;
using System.Linq.Expressions;
using FastExpressionCompiler.LightExpression;
using System.Reflection;
using System.Security;

Expand Down Expand Up @@ -47,7 +47,7 @@ static FieldInfoModifier()
Expression.Label(returnLabel)
)
, fieldInfoParam
).Compile();
).CompileFast();
SetFieldInfoReadonly = (Action<FieldInfo>)Expression.Lambda(
Expression.Block(
Expression.Assign(Expression.MakeMemberAccess(castedType, fieldInfo_m_Attributes),
Expand All @@ -58,7 +58,7 @@ static FieldInfoModifier()
Expression.Label(returnLabel)
)
, fieldInfoParam
).Compile();
).CompileFast();

var s = Binary.Create(new Settings());
try
Expand Down
12 changes: 6 additions & 6 deletions Apex.Serialization/Internal/Reflection/TypeFields.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -23,11 +23,11 @@ static TypeFields()

var logicallyNullMethod = typeof(MulticastDelegate).GetMethod("InvocationListLogicallyNull", BindingFlags.Instance | BindingFlags.NonPublic)!;

getInvocationCount = Expression.Lambda<Func<MulticastDelegate, IntPtr>>(Expression.Field(delegateParam, "_invocationCount"), delegateParam).Compile();
getInvocationList = Expression.Lambda<Func<MulticastDelegate, object>>(Expression.Field(delegateParam, "_invocationList"), delegateParam).Compile();
setInvocationCount = Expression.Lambda<Action<MulticastDelegate, IntPtr>>(Expression.Assign(Expression.Field(delegateParam, "_invocationCount"), intPtrParam), delegateParam, intPtrParam).Compile();
setInvocationList = Expression.Lambda<Action<MulticastDelegate, object>>(Expression.Assign(Expression.Field(delegateParam, "_invocationList"), objectParam), delegateParam, objectParam).Compile();
getInvocationListLogicallyNull = Expression.Lambda<Func<MulticastDelegate, bool>>(Expression.Call(delegateParam, logicallyNullMethod), delegateParam).Compile();
getInvocationCount = Expression.Lambda<Func<MulticastDelegate, IntPtr>>(Expression.Field(delegateParam, "_invocationCount"), delegateParam).CompileFast();
getInvocationList = Expression.Lambda<Func<MulticastDelegate, object>>(Expression.Field(delegateParam, "_invocationList"), delegateParam).CompileFast();
setInvocationCount = Expression.Lambda<Action<MulticastDelegate, IntPtr>>(Expression.Assign(Expression.Field(delegateParam, "_invocationCount"), intPtrParam), delegateParam, intPtrParam).CompileFast();
setInvocationList = Expression.Lambda<Action<MulticastDelegate, object>>(Expression.Assign(Expression.Field(delegateParam, "_invocationList"), objectParam), delegateParam, objectParam).CompileFast();
getInvocationListLogicallyNull = Expression.Lambda<Func<MulticastDelegate, bool>>(Expression.Call(delegateParam, logicallyNullMethod), delegateParam).CompileFast();
}

private static DictionarySlim<Type, List<FieldInfo>> _cache = new DictionarySlim<Type, List<FieldInfo>>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using System.Linq.Expressions;
using FastExpressionCompiler.LightExpression;
using System.Reflection;

namespace Apex.Serialization.Internal
Expand Down Expand Up @@ -196,7 +196,7 @@ protected override Expression VisitLabel(LabelExpression node)
Combine(node.Name);
Combine(node.Type);
}
return base.VisitLabelTarget(node);
return base.VisitLabelTarget(node!);
}

protected override Expression VisitLambda<T>(Expression<T> node)
Expand Down Expand Up @@ -341,7 +341,6 @@ protected override Expression VisitUnary(UnaryExpression node)
protected override Expression VisitDebugInfo(DebugInfoExpression node)
{
Combine(node.Document.FileName);

return base.VisitDebugInfo(node);
}
}
Expand Down
7 changes: 3 additions & 4 deletions Benchmark/BufferedStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<writeSig>(Expression.Call(p, BinaryStreamMethods<BufferedStream>.GenericMethods<byte>.WriteValueMethodInfo, b), p, b);
var compiledLamda = lambda.Compile();
return (writeSig)compiledLamda;
var compiledLambda = lambda.CompileFast();
return (writeSig)compiledLambda;
}

[Benchmark]
Expand Down
2 changes: 1 addition & 1 deletion Benchmark/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down