Skip to content

Conversation

Alex-Sob
Copy link

Issue

System.Data.Common.ADP internal helper class has BuildSchemaTableInfoTableNames method that calls ToLowerInvariant on a string in a loop only to perform a dictionary lookup.

Suggested fix

Using Dictionary<K, V> with StringComparer.InvariantCultureIgnoreCase can avoid multiple string allocations in that method.

How issue was found

I came up with an idea of an analyzer that would look for Dictionary<string, TValue> lookups and find cases when a string allocation is made only to perform a lookup. In such a case an analyzer could recommend using Dictionary<K, V>.GetAlternateLookup<ReadOnlySpan<char>> to avoid an allocation. I prototyped such an analyzer and ran it as a standalone analysis tool against projects that are part of src/libraries/sfx.slnx solution. This is how I found this code. And in this particular case, I actually realized it should be even easier to fix by using StringComparer. This could probably be a second rule in the same analyzer.

Analyzer proposal

[I'll provide a link to the proposal shortly]

@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Oct 10, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Oct 10, 2025
}
else
{
columnName = columnName.ToLowerInvariant();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also before the hash.TryAdd about 20 lines lower?

@jkotas jkotas added area-System.Data and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Oct 10, 2025
Copy link
Contributor

Tagging subscribers to this area: @roji, @SamMonoRT
See info in area-owners.md if you want to be subscribed.

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

Labels

area-System.Data community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants