Skip to content

Lazily load dynamic linq types #940

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
40 changes: 40 additions & 0 deletions src/System.Linq.Dynamic.Core/Compatibility/Lazy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Threading;

#if NET35
namespace System
{
internal class Lazy<T>
{
private readonly Func<T> _valueFactory;
private readonly object _lock;

private T? _value;
private bool _valueCreated;

public Lazy(Func<T> valueFactory, LazyThreadSafetyMode mode)
{
_valueFactory = valueFactory;
_lock = new object();
}

public T Value
{
get
{
lock (_lock)
{
if (_valueCreated)
{
return _value!;
}

_value = _valueFactory();
_valueCreated = true;
return _value;
}
}
}

}
}
#endif
11 changes: 11 additions & 0 deletions src/System.Linq.Dynamic.Core/Compatibility/LazyThreadSafetyMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#if NET35
namespace System.Threading
{
internal enum LazyThreadSafetyMode
{
None,
PublicationOnly,
ExecutionAndPublication,
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
using System.Linq.Dynamic.Core.Validation;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;

namespace System.Linq.Dynamic.Core.CustomTypeProviders;

/// <summary>
/// The default implementation for <see cref="DefaultDynamicLinqCustomTypeProvider"/>.
///
///
/// Scans the current AppDomain for all types marked with <see cref="DynamicLinqTypeAttribute"/>, and adds them as custom Dynamic Link types.
///
/// This class is used as default for full .NET Framework and .NET Core App 2.x and higher.
Expand All @@ -16,8 +17,8 @@ public class DefaultDynamicLinqCustomTypeProvider : AbstractDynamicLinqCustomTyp
{
private readonly IAssemblyHelper _assemblyHelper;
private readonly bool _cacheCustomTypes;
private readonly Lazy<HashSet<Type>>? _cachedCustomTypes;

private HashSet<Type>? _cachedCustomTypes;
private Dictionary<Type, List<MethodInfo>>? _cachedExtensionMethods;

/// <summary>
Expand Down Expand Up @@ -49,19 +50,17 @@ public DefaultDynamicLinqCustomTypeProvider(ParsingConfig config, IList<Type> ad
{
_assemblyHelper = new DefaultAssemblyHelper(Check.NotNull(config));
_cacheCustomTypes = cacheCustomTypes;
_cachedCustomTypes = cacheCustomTypes
? new Lazy<HashSet<Type>>(GetCustomTypesInternal, LazyThreadSafetyMode.ExecutionAndPublication)
: null;
}

/// <inheritdoc cref="IDynamicLinqCustomTypeProvider.GetCustomTypes"/>
public virtual HashSet<Type> GetCustomTypes()
{
if (_cacheCustomTypes)
{
if (_cachedCustomTypes == null)
{
_cachedCustomTypes = GetCustomTypesInternal();
}

return _cachedCustomTypes;
return _cachedCustomTypes!.Value;
}

return GetCustomTypesInternal();
Expand Down
30 changes: 18 additions & 12 deletions src/System.Linq.Dynamic.Core/Parser/KeywordsHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq.Dynamic.Core.Validation;
using System.Linq.Expressions;
using System.Threading;
using AnyOfTypes;

namespace System.Linq.Dynamic.Core.Parser;
Expand Down Expand Up @@ -32,7 +33,7 @@ internal class KeywordsHelper : IKeywordsHelper
private static readonly Dictionary<string, Type> PreDefinedTypeMapping = new();

// Custom DefinedTypes are not IgnoreCase
private readonly Dictionary<string, Type> _customTypeMapping = new();
private readonly Lazy<Dictionary<string, Type>> _customTypeMapping;

static KeywordsHelper()
{
Expand Down Expand Up @@ -66,22 +67,27 @@ public KeywordsHelper(ParsingConfig config)
{ FUNCTION_CAST, FUNCTION_CAST }
};

_customTypeMapping = new Lazy<Dictionary<string, Type>>(() =>
{
Dictionary<string, Type> customTypeMapping = new();
if (config.CustomTypeProvider != null)
{
foreach (var type in config.CustomTypeProvider.GetCustomTypes())
{
customTypeMapping[type.FullName!] = type;
customTypeMapping[type.Name] = type;
}
}

return customTypeMapping;
}, LazyThreadSafetyMode.PublicationOnly);

if (config.AreContextKeywordsEnabled)
{
_mappings.Add(KEYWORD_IT, KEYWORD_IT);
_mappings.Add(KEYWORD_PARENT, KEYWORD_PARENT);
_mappings.Add(KEYWORD_ROOT, KEYWORD_ROOT);
}

// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
if (config.CustomTypeProvider != null)
{
foreach (var type in config.CustomTypeProvider.GetCustomTypes())
{
_customTypeMapping[type.FullName!] = type;
_customTypeMapping[type.Name] = type;
}
}
}

public bool IsItOrRootOrParent(AnyOf<string, Expression, Type> value)
Expand Down Expand Up @@ -125,7 +131,7 @@ public bool TryGetValue(string text, out AnyOf<string, Expression, Type> value)
}

// 5. Try to get as custom type
if (_customTypeMapping.TryGetValue(text, out var customType))
if (_customTypeMapping.Value.TryGetValue(text, out var customType))
{
value = customType;
return true;
Expand Down