Skip to content

Replace an O(n) lookup in LINQ query parsing by an O(1) one #1869

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

Merged
Merged
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
2 changes: 1 addition & 1 deletion src/NHibernate.Test/Async/Hql/HQLFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1079,9 +1079,9 @@ public async Task Current_DateAsync()
public async Task Current_Date_IsLowestTimeOfDayAsync()
{
AssumeFunctionSupported("current_date");
var now = DateTime.Now;
if (!TestDialect.SupportsNonDataBoundCondition)
Assert.Ignore("Test is not supported by the target database");
var now = DateTime.Now;
if (now.TimeOfDay < TimeSpan.FromMinutes(5) || now.TimeOfDay > TimeSpan.Parse("23:55"))
Assert.Ignore("Test is unreliable around midnight");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

Expand All @@ -10,6 +11,9 @@ public class DefaultLinqToHqlGeneratorsRegistry : ILinqToHqlGeneratorsRegistry
private readonly Dictionary<MemberInfo, IHqlGeneratorForProperty> registeredProperties = new Dictionary<MemberInfo, IHqlGeneratorForProperty>();
private readonly List<IRuntimeMethodHqlGenerator> runtimeMethodHqlGenerators = new List<IRuntimeMethodHqlGenerator>();

private readonly ConcurrentDictionary<MethodInfo, IHqlGeneratorForMethod> _cachedRuntimeMethodHqlGenerators =
new ConcurrentDictionary<MethodInfo, IHqlGeneratorForMethod>();

public DefaultLinqToHqlGeneratorsRegistry()
{
RegisterGenerator(new StandardLinqExtensionMethodGenerator());
Expand Down Expand Up @@ -69,14 +73,15 @@ public DefaultLinqToHqlGeneratorsRegistry()

protected bool GetRuntimeMethodGenerator(MethodInfo method, out IHqlGeneratorForMethod methodGenerator)
{
methodGenerator = null;

foreach (var typeGenerator in runtimeMethodHqlGenerators.Where(typeGenerator => typeGenerator.SupportsMethod(method)))
{
methodGenerator = typeGenerator.GetMethodGenerator(method);
return true;
}
return false;
methodGenerator = _cachedRuntimeMethodHqlGenerators.GetOrAdd(
method,
m =>
runtimeMethodHqlGenerators
.Where(g => g.SupportsMethod(m))
.Select(g => g.GetMethodGenerator(m))
.FirstOrDefault());

return methodGenerator != null;
}

public virtual bool TryGetGenerator(MethodInfo method, out IHqlGeneratorForMethod generator)
Expand Down Expand Up @@ -112,6 +117,7 @@ public virtual void RegisterGenerator(MemberInfo property, IHqlGeneratorForPrope
public void RegisterGenerator(IRuntimeMethodHqlGenerator generator)
{
runtimeMethodHqlGenerators.Add(generator);
_cachedRuntimeMethodHqlGenerators.Clear();
}
}
}