[rc2] Optimize FindIndexerPropertyInfo methods to avoid unnecessary reflection calls #36648
+2 −2
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Fixes #34823
Description
Optimizes FindIndexerPropertyInfo methods in both RuntimeModel.cs and Model.cs to avoid unnecessary reflection calls. The current implementation calls the expensive FindIndexerProperty() method on every GetOrAdd call, even when the key already exists in the cache. Changed to use the factory overload of GetOrAdd with a static lambda that only executes when the key is not found.
Customer impact
Applications that repeatedly process the same entity types will see improved performance, as cached lookups now avoid expensive reflection operations. This is particularly beneficial in high-throughput scenarios where the same types are processed frequently. No breaking changes - existing applications continue to work without modification.
How found
Customer reported via GitHub issue #34823. The issue was identified by a contributor while reading the EF Core codebase.
Regression
Not a regression - this is an existing inefficiency that has been present in the codebase.
Testing
All existing tests continue to pass. The change is functionally equivalent to the previous implementation but with better performance characteristics for cached lookups.
Risk
Low. The change is minimal (only 2 lines modified) and preserves the exact same public API and behavior. The optimization uses well-established .NET patterns and only affects the internal implementation detail of how the cache factory is invoked.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.