Skip to content
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

Switch to HashSet in LazyLoader #32445

Merged
merged 1 commit into from
Nov 29, 2023
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
10 changes: 5 additions & 5 deletions src/EFCore.Proxies/Proxies/Internal/LazyLoadingInterceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private static readonly PropertyInfo LazyLoaderProperty
private static readonly MethodInfo LazyLoaderSetter = LazyLoaderProperty.SetMethod!;

private ILazyLoader? _loader;
private readonly Dictionary<string, bool> _navigations;
private readonly HashSet<string> _navigations;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -34,10 +34,11 @@ public LazyLoadingInterceptor(
ILazyLoader loader)
{
_loader = loader;
_navigations = entityType!.GetNavigations()
_navigations = entityType!.GetNavigations().Where(n => !n.ForeignKey.IsOwnership)
.Cast<INavigationBase>()
.Concat(entityType.GetSkipNavigations())
.ToDictionary(n => n.Name, n => n is INavigation { ForeignKey.IsOwnership: true });
.Select(n => n.Name)
.ToHashSet();
}

/// <summary>
Expand All @@ -64,8 +65,7 @@ public virtual void Intercept(IInvocation invocation)
&& methodName.StartsWith("get_", StringComparison.Ordinal))
{
var navigationName = methodName[4..];
if (_navigations.TryGetValue(navigationName, out var isOwnership)
&& !isOwnership)
if (_navigations.Contains(navigationName))
{
_loader.Load(invocation.Proxy, navigationName);
}
Expand Down
18 changes: 10 additions & 8 deletions src/EFCore/Infrastructure/Internal/LazyLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class LazyLoader : ILazyLoader, IInjectableService
private bool _detached;
private IDictionary<string, bool>? _loadedStates;
private List<(object Entity, string NavigationName)>? _isLoading;
private Dictionary<string, bool>? _navigations;
private HashSet<string>? _nonLazyNavigations;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -45,7 +45,8 @@ public LazyLoader(
public virtual void Injected(DbContext context, object entity, ParameterBindingInfo bindingInfo)
{
_queryTrackingBehavior = bindingInfo.QueryTrackingBehavior;
_navigations ??= InitNavigationsMetadata(bindingInfo.StructuralType as IEntityType ?? throw new NotImplementedException());
_nonLazyNavigations ??= InitNavigationsMetadata(bindingInfo.StructuralType as IEntityType
?? throw new NotImplementedException("Navigations on complex types are not supported"));
}

/// <summary>
Expand Down Expand Up @@ -214,9 +215,8 @@ private bool ShouldLoad(object entity, string navigationName, [NotNullWhen(true)
{
if (!_detached && !IsLoaded(entity, navigationName))
{
if (_navigations == null
|| !_navigations.TryGetValue(navigationName, out var lazyLoadingEnabled)
|| lazyLoadingEnabled)
if (_nonLazyNavigations == null
|| !_nonLazyNavigations.Contains(navigationName))
{
if (_disposed)
{
Expand Down Expand Up @@ -275,12 +275,14 @@ public virtual void Attaching(DbContext context, IEntityType entityType, object
_disposed = false;
_detached = false;
Context = context;
_navigations ??= InitNavigationsMetadata(entityType);
_nonLazyNavigations ??= InitNavigationsMetadata(entityType);
}

private Dictionary<string, bool> InitNavigationsMetadata(IEntityType entityType)
private HashSet<string> InitNavigationsMetadata(IEntityType entityType)
=> entityType!.GetNavigations()
.Cast<INavigationBase>()
.Concat(entityType.GetSkipNavigations())
.ToDictionary(n => n.Name, n => n.LazyLoadingEnabled);
.Where(n => !n.LazyLoadingEnabled)
.Select(t => t.Name)
.ToHashSet();
}
Loading