Skip to content

Conversation

@davkean
Copy link
Member

@davkean davkean commented Jul 14, 2021

Context

CompareInfo.IndexOf(string, char, ...) on .NET Framework has two paths:

OrdinalIgnoreCase: Which goes through string.IndexOf(string, ...)
Everything else: Win32's FindNLSStringEx

Both paths allocate a string that represents the char. Instead just call through string.IndexOf(char) which does a flat ordinal comparison which is what we want. This was about 0.3% of allocations in devenv opening a 500 project solution.

image

I walked every instance of this across the tree and found only the ones in the PR. It doesn't look like MSBuild uses the banned API analyzer so I couldn't prevent future consumption of this.

@davkean davkean changed the title Avoid string allocation which searching for a char Avoid string allocation while searching for a char Jul 14, 2021
CompareInfo.IndexOf(string, char, ...) on .NET Framework has two paths:

OrdinalIgnoreCase: Which goes through string.IndexOf(string, ...)
Everything else: Win32's FindNLSStringEx

Both paths allocate a string that represents the char. Instead just call through string.IndexOf(char) which does a flat ordinal comparison which is what we want. This was about 0.3% of allocations in devenv opening a 500 project solution.
rainersigwald added a commit to rainersigwald/msbuild that referenced this pull request Jul 14, 2021
For OrdinalIgnoreCase, this uses string.IndexOf and allocates a string.

See dotnet#6671.
@rainersigwald
Copy link
Member

It doesn't look like MSBuild uses the banned API analyzer so I couldn't prevent future consumption of this.

#6675.

@davkean
Copy link
Member Author

davkean commented Jul 15, 2021

Thanks! @rainersigwald Can you merge this in? I don't have permissions.

@Forgind Forgind added the merge-when-branch-open PRs that are approved, except that there is a problem that means we are not merging stuff right now. label Jul 15, 2021
@benvillalobos benvillalobos merged commit 98dd7fa into dotnet:main Jul 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Performance merge-when-branch-open PRs that are approved, except that there is a problem that means we are not merging stuff right now.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants