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
412 changes: 412 additions & 0 deletions src/Mapster.Tests/WhenCachingAttributeMetadata.cs

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions src/Mapster/Compile/AttributeMetadataCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Reflection;

namespace Mapster
{
// Shared by built-in source member models for one root expression, including its inline mappings.
// The lock protects this cache only; CompileContext's other mutable state is not thread-safe.
internal sealed class AttributeMetadataCache
{
private readonly Dictionary<MemberInfo, IReadOnlyList<CustomAttributeData>> _metadata = new();
private bool _completed;

internal IEnumerable<CustomAttributeData> Get(MemberInfo member)
{
lock (_metadata)
{
if (_completed)
return member.GetCustomAttributesData();
if (!_metadata.TryGetValue(member, out var attributes))
{
attributes = Array.AsReadOnly(new List<CustomAttributeData>(member.GetCustomAttributesData()).ToArray());
_metadata.Add(member, attributes);
}
return attributes;
}
}

internal void Complete()
{
lock (_metadata)
{
// Callbacks and CompileException can retain models/context after compilation.
// Release metadata and prevent retained models from repopulating the cache.
_completed = true;
_metadata.Clear();
}
}
}
}
2 changes: 2 additions & 0 deletions src/Mapster/Compile/CompileContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class CompileContext
public HashSet<ParameterExpression> ExtraParameters { get; } = new();
public HashSet<(Expression param, CompileArgument arg)> NullChecks { get; } = new();

internal AttributeMetadataCache AttributeMetadata { get; } = new();

internal bool IsSubFunction()
{
return MaxDepth.HasValue || ExtraParameters.Count > 0;
Expand Down
9 changes: 8 additions & 1 deletion src/Mapster/Models/FieldModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@ namespace Mapster.Models
public class FieldModel : IMemberModelEx
{
private readonly FieldInfo _fieldInfo;
private readonly AttributeMetadataCache? _attributeMetadata;
public FieldModel(FieldInfo fieldInfo)
{
_fieldInfo = fieldInfo;
}

internal FieldModel(FieldInfo fieldInfo, AttributeMetadataCache? attributeMetadata)
: this(fieldInfo)
{
_attributeMetadata = attributeMetadata;
}

public Type Type => _fieldInfo.FieldType;
public string Name => _fieldInfo.Name;
public object Info => _fieldInfo;
Expand All @@ -33,7 +40,7 @@ public IEnumerable<object> GetCustomAttributes(bool inherit)
}
public IEnumerable<CustomAttributeData> GetCustomAttributesData()
{
return _fieldInfo.GetCustomAttributesData();
return _attributeMetadata?.Get(_fieldInfo) ?? _fieldInfo.GetCustomAttributesData();
}
}
}
9 changes: 8 additions & 1 deletion src/Mapster/Models/PropertyModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@ namespace Mapster.Models
public class PropertyModel : IMemberModelEx
{
private readonly PropertyInfo _propertyInfo;
private readonly AttributeMetadataCache? _attributeMetadata;
public PropertyModel(PropertyInfo propertyInfo)
{
_propertyInfo = propertyInfo;
}

internal PropertyModel(PropertyInfo propertyInfo, AttributeMetadataCache? attributeMetadata)
: this(propertyInfo)
{
_attributeMetadata = attributeMetadata;
}

public Type Type => _propertyInfo.PropertyType;
public virtual string Name => _propertyInfo.Name;
public object Info => _propertyInfo;
Expand Down Expand Up @@ -48,7 +55,7 @@ public IEnumerable<object> GetCustomAttributes(bool inherit)
}
public IEnumerable<CustomAttributeData> GetCustomAttributesData()
{
return _propertyInfo.GetCustomAttributesData();
return _attributeMetadata?.Get(_propertyInfo) ?? _propertyInfo.GetCustomAttributesData();
}
}
}
3 changes: 2 additions & 1 deletion src/Mapster/Settings/ValueAccessingStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public static class ValueAccessingStrategy

private static Expression? PropertyOrFieldFn(Expression source, IMemberModel destinationMember, CompileArgument arg)
{
var members = source.Type.GetFieldsAndProperties(true);
// Repeated source scans create fresh wrappers; share metadata, not mapping decisions.
var members = source.Type.GetFieldsAndProperties(true, arg.Context.AttributeMetadata);
var strategy = arg.Settings.NameMatchingStrategy;
var destinationMemberName = destinationMember.GetMemberName(MemberSide.Destination, arg.Settings.GetMemberNames, strategy.DestinationMemberNameConverter, arg);
return members
Expand Down
1 change: 1 addition & 0 deletions src/Mapster/TypeAdapterConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ public LambdaExpression CreateMapExpression(TypeTuple tuple, MapType mapType)
}
finally
{
context.AttributeMetadata.Complete();
if (fork != null)
context.Configs.Pop();
context.Running.Remove(tuple);
Expand Down
6 changes: 3 additions & 3 deletions src/Mapster/Utils/ReflectionUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static bool IsPoco(this Type type)
return type.GetFieldsAndProperties().Any(it => (it.SetterModifier & (AccessModifier.Public | AccessModifier.NonPublic)) != 0);
}

public static IEnumerable<IMemberModelEx> GetFieldsAndProperties(this Type type, bool includeNonPublic = false)
public static IEnumerable<IMemberModelEx> GetFieldsAndProperties(this Type type, bool includeNonPublic = false, AttributeMetadataCache? attributeMetadata = null)
{
var bindingFlags = BindingFlags.Instance | BindingFlags.Public;
if (includeNonPublic)
Expand All @@ -90,11 +90,11 @@ public static IEnumerable<IMemberModelEx> GetFieldsAndProperties(this Type type,

IEnumerable<IMemberModelEx> GetPropertiesFunc(Type t, MemberInfo[] currentTypeMembers) => t.GetProperties(bindingFlags)
.Where(x => x.GetIndexParameters().Length == 0).DropHiddenMembers(currentTypeMembers)
.Select(CreateModel);
.Select(x => new PropertyModel(x, attributeMetadata));

IEnumerable<IMemberModelEx> GetFieldsFunc(Type t, MemberInfo[] overlapMembers) =>
t.GetFields(bindingFlags).DropHiddenMembers(overlapMembers)
.Select(CreateModel);
.Select(x => new FieldModel(x, attributeMetadata));
}

public static IEnumerable<T> DropHiddenMembers<T>(this IEnumerable<T> allMembers, ICollection<MemberInfo> currentTypeMembers) where T : MemberInfo
Expand Down
Loading