Skip to content

Commit

Permalink
Reflection cache (#155)
Browse files Browse the repository at this point in the history
* Added reflection cache

* Update for instance/class variables

Co-authored-by: axunonb <nb@axuno.net>
  • Loading branch information
karljj1 and axunonb authored Apr 16, 2021
1 parent 5301af8 commit 8f5bcc1
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/SmartFormat/Extensions/ReflectionSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// Licensed under the MIT license.
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using SmartFormat.Core.Extensions;
Expand All @@ -11,6 +13,10 @@ namespace SmartFormat.Extensions
{
public class ReflectionSource : ISource
{
private static readonly object[] Empty = Array.Empty<object>();

private readonly Dictionary<(Type, string?), (FieldInfo? field, MethodInfo? method)> _typeCache = new();

public ReflectionSource(SmartFormatter formatter)
{
// Add some special info to the parser:
Expand All @@ -32,6 +38,24 @@ public bool TryEvaluateSelector(ISelectorInfo selectorInfo)
// Let's see if the argSelector is a Selectors/Field/ParseFormat:
var sourceType = current.GetType();

// Check the type cache
if (_typeCache.TryGetValue((sourceType, selector), out var found))
{
if (found.field != null)
{
selectorInfo.Result = found.field.GetValue(current);
return true;
}

if (found.method != null)
{
selectorInfo.Result = found.method.Invoke(current, Empty);
return true;
}

return false;
}

// Important:
// GetMembers (opposite to GetMember!) returns all members,
// both those defined by the type represented by the current T:System.Type object
Expand All @@ -45,6 +69,7 @@ public bool TryEvaluateSelector(ISelectorInfo selectorInfo)
// Selector is a Field; retrieve the value:
var field = (FieldInfo) member;
selectorInfo.Result = field.GetValue(current);
_typeCache[(sourceType, selector)] = (field, null);
return true;
case MemberTypes.Property:
case MemberTypes.Method:
Expand Down Expand Up @@ -72,11 +97,17 @@ public bool TryEvaluateSelector(ISelectorInfo selectorInfo)
// Make sure that this method is not void! It has to be a Function!
if (method?.ReturnType == typeof(void)) continue;

// Add to cache
_typeCache[(sourceType, selector)] = (null, method);

// Retrieve the Selectors/ParseFormat value:
selectorInfo.Result = method?.Invoke(current, new object[0]);
return true;
}

// We also cache failures so we dont need to call GetMembers again
_typeCache[(sourceType, selector)] = (null, null);

return false;
}
}
Expand Down

0 comments on commit 8f5bcc1

Please sign in to comment.