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

Optimize allocations in TextReader.ReadToEnd utilized by SourceText.From #70017

Merged
merged 2 commits into from
Sep 20, 2023

Conversation

ToddGrun
Copy link
Contributor

@ToddGrun ToddGrun commented Sep 19, 2023

The profile I'm looking currently shows 9.4% of allocation in our codeanalysis process under SourceText.From. These allocations occur during the TextReader.ReadToEnd call. Fortunately, Roslyn is already passing in a TextReader that knows it's length, and thus we can optimize this path.

In netfx, this should at least get rid of the extra sizing allocations done during TextReader.ReadToEnd as it appends to it's internal stringbuilder. So, this should see a reduction of about 20% of the allocations in this codepath.

In netcore (which is the case for this profile), we do better as this essentially gets rid of all allocations except for the equivalent of the StringBuilder.ToString call. So, this should see a reduction of about 2/3 of the allocations of this codepath.

image

The profile I'm looking currently shows 9.4% of allocation in our codeanalysis process under SourceText.From. These allocations occur during the TextReader.ReadToEnd call. Fortunately, Roslyn is already passing in a TextReader that knows it's length, and thus we can optimize this path.

In netfx, this should at least get rid of the extra sizing allocations done during TextReader.ReadToEnd as it appends to it's internal stringbuilder. So, this should see a reduction of about 20% of the allocations in this codepath.

In netcore (which is the case for this profile), we do better as this essentially gets rid of all allocations except for the equivalent of the StringBuilder.ToString call. So, this should see a reduction of about 2/3 of the allocations of this codepath.
@ToddGrun ToddGrun requested a review from a team as a code owner September 19, 2023 17:49
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Sep 19, 2023
Copy link
Member

@tmat tmat left a comment

Choose a reason for hiding this comment

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

Actually, the previous impl ended up calling LargeText.Decode if the length was greater than LargeObjectHeapLimitInChars. The new impl would allocate the string on LOH

Should we call SourceText.From(reader, length, encoding, checksumAlgorithm) from CreateText?

public override string ReadToEnd()
{
#if NETCOREAPP
return string.Create(Length, this, static (chars, state) => state.Read(chars));
Copy link
Member

Choose a reason for hiding this comment

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

snazzy.

@ToddGrun
Copy link
Contributor Author

That's a good observation. I thought I was being smart with the TextFactoryService change, but I didn't consider the LOH case.


In reply to: 1633907243

…override that takes in the length as it prevents LOH allocations.
@ToddGrun ToddGrun merged commit f455ada into dotnet:main Sep 20, 2023
24 checks passed
@ghost ghost added this to the Next milestone Sep 20, 2023
@Cosifne Cosifne modified the milestones: Next, 17.8 P3 Sep 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants