Skip to content

Commit bf5bf3a

Browse files
committed
Performance improvements for expression transformations
1 parent 65cb43e commit bf5bf3a

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

NotSoAutoMapper/ExpressionProcessing/MapperInliningOperations.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static Expression InlineCollection(
2929
// -----
3030
// collection.Select(y => new Thing { Name = y.Name })
3131

32-
var mapper = mapperExpression.CompileAndGet<IMapper>() ?? throw TransformerExceptions.NullMapperException;
32+
var mapper = mapperExpression.GetValue<IMapper>() ?? throw TransformerExceptions.NullMapperException;
3333

3434
// The item type of the source : IEnumerable<int> -> int
3535
var sourceItemType = FindEnumerableType(sourceExpression.Type).GenericTypeArguments[0];
@@ -66,7 +66,7 @@ static LambdaExpression CreateSelectLambda(Type enumerableItemType, IMapper mapp
6666

6767
public static Expression InlineObject(Expression sourceExpression, Expression mapperExpression)
6868
{
69-
var mapper = mapperExpression.CompileAndGet<IMapper>();
69+
var mapper = mapperExpression.GetValue<IMapper>();
7070
if (mapper is null)
7171
{
7272
throw TransformerExceptions.NullMapperException;

NotSoAutoMapper/ExpressionProcessing/RetrieveExpressionValueExtensions.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,35 @@
11
using System;
22
using System.Linq.Expressions;
3+
using System.Reflection;
34

45
namespace NotSoAutoMapper.ExpressionProcessing
56
{
67
internal static class RetrieveExpressionValueExtensions
78
{
8-
public static T? CompileAndGet<T>(this Expression expression)
9+
public static T? GetValue<T>(this Expression expression)
910
{
10-
var func = (Func<T?>) Expression.Lambda(expression).Compile();
11+
// Maintaining an expression cache is tricky, the Expression class doesn't implement .Equals!
12+
// Instead, we have some well-known common cases to boost the performance.
13+
// Reflection is much more faster than Expression compilation.
14+
15+
if (expression is ConstantExpression constantExpression)
16+
{
17+
return (T?) constantExpression.Value;
18+
}
19+
20+
if (expression is MemberExpression memberExpression)
21+
{
22+
var member = memberExpression.Member;
23+
switch (member)
24+
{
25+
case PropertyInfo propertyInfo:
26+
return (T?) propertyInfo.GetValue(memberExpression.Expression);
27+
case FieldInfo fieldInfo:
28+
return (T?) fieldInfo.GetValue(memberExpression.Expression);
29+
}
30+
}
31+
32+
var func = (Func<T?>) Expression.Lambda(expression).Compile(true);
1133
return func();
1234
}
1335
}

NotSoAutoMapper/NotSoAutoMapper.csproj

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>
88
<Authors>jeuxjeux20</Authors>
99
<Product />
10-
<Description>A simple and extensible library to map DTOs (or anything!) using powerful expressions.</Description>
10+
<Description>A library to map your DTOs with reusable expressions.</Description>
1111
<PackageProjectUrl>https://github.com/jeuxjeux20/NotSoAutoMapper</PackageProjectUrl>
1212
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1313
<PackageIcon>logo_512x512.png</PackageIcon>
@@ -16,6 +16,12 @@
1616
<RepositoryType>git</RepositoryType>
1717
<PackageTags>handmademapper mapper dto expression entityframework ef efcore entityframeworkcore map</PackageTags>
1818
<PackageReleaseNotes>
19+
3.0.0-preview4:
20+
* Creating a mapper is now much faster.
21+
22+
3.0.0-preview3:
23+
* Null bug fixes
24+
1925
3.0.0-preview2:
2026
* Added custom TransformedUsing attributes
2127
* Added inheritance for Merge
@@ -27,7 +33,7 @@
2733
* Removed recursion prevention as it is not longer possible to have recursion with eager-loading mappers
2834
* Removed MapperOptions
2935
</PackageReleaseNotes>
30-
<Version>3.0.0-preview3</Version>
36+
<Version>3.0.0-preview4</Version>
3137
<AssemblyVersion>3.0.0</AssemblyVersion>
3238
<FileVersion>3.0.0</FileVersion>
3339
</PropertyGroup>

0 commit comments

Comments
 (0)