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

Correct small inefficiency in private internal function in EFCore - FindIndexerPropertyInfo #34823

Open
yinzara opened this issue Oct 3, 2024 · 1 comment

Comments

@yinzara
Copy link
Contributor

yinzara commented Oct 3, 2024

While reading the codebase of Efcore I noticed a small inefficiency in a function used extensively throughout Efcore and I thought it was probably worth addressing.

Efcore/Metadata/RuntimeModel.cs -> Line 295

    private PropertyInfo? FindIndexerPropertyInfo([DynamicallyAccessedMembers(IEntityType.DynamicallyAccessedMemberTypes)] Type type)
        => _indexerPropertyInfoMap.GetOrAdd(type, type.FindIndexerProperty());

In the above code, though the _indexerPropertyMap is used to ensure we get the same indexer for the same type, because we're calling type.FindIndexerProperty() we're not getting much benefit from it because the FindIndexerProperty function is run no matter if there is a key found or not.

The GetOrAdd function of the ConcurrentDictionary has an overload that accepts a Func<TKey, TValue> factory method specifically for this purpose.

If we instead change the function to the following:

    private PropertyInfo? FindIndexerPropertyInfo([DynamicallyAccessedMembers(IEntityType.DynamicallyAccessedMemberTypes)] Type type)
        => _indexerPropertyInfoMap.GetOrAdd(type, EntityFrameworkCore.Internal.TypeExtensions.FindIndexerProperty);

We use the overload that accepts the Func<TKey, TValue> and only call the function if the key was not found in the map.

@AndriySvyryd
Copy link
Member

The other implementation that should be fixed is at
https://github.com/dotnet/efcore/blob/main/src/EFCore/Metadata/Internal/Model.cs#L1181-L1182

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants