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

Followup PR to https://github.com/dotnet/roslyn/pull/69677 to reduce allocations further. #69746

Conversation

ToddGrun
Copy link
Contributor

This prevents a string.Split call (which is called from quite a few places) and replaces it with ReadOnlyMemory operations instead.

This prevents a string.Split call (which is called from quite a few places) and replaces it with ReadOnlyMemory operations instead.
@ToddGrun ToddGrun requested a review from a team as a code owner August 28, 2023 23:16
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Aug 28, 2023
{
var result = ArrayBuilder<ReadOnlyMemory<char>>.GetInstance();

var index = s.Span.IndexOf(separator);
Copy link
Member

Choose a reason for hiding this comment

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

Is this the same semantics as before? What if the file only has \n newlines? Before I believe it would still split ok. But with this new logic, I'm not sure.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Didn't the string.Split call take in a 1-element array, indicating "\r\n" is the delimiter? Was it intending to send in an array with '\r' and '\n' as delimiters instead?

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure (and I'm on my phone). If the are the same semantics, great. It's just something I was nervous about. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It sure looks like the same semantics to me, but maybe I'm missing something as it does seem a little odd. (the Split call didn't change in your PR either, so if it's the same and it's wrong, it's been as such for a while)

}

formattedLines.Add(sb.ToString());
}

public static ImmutableArray<ReadOnlyMemory<char>> Split(
Copy link
Member

Choose a reason for hiding this comment

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

Consider making an extension. Consider returning an IEnumerable

Copy link
Member

Choose a reason for hiding this comment

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

Note: on net-8 something, this method exists right? So maybe use that, and polyfill it in if on net standard?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I intentionally made it return a type with a struct enumerator. Let me know if that's generally not preferred in this scenario.

As for net-8, there are Split extensions, but I don't think I'm fully understanding those. It looks like they require a Span argument in which to store the results, but I'm not sure how large to make that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As for making this an extension method, I thought about that too, but it looks like the other extension methods on ReadOnly(Memory/Span) are down in the compiler, which the features assembly doesn't have IVT to.

Copy link
Member

Choose a reason for hiding this comment

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

We don't need ivt. A common thing we do is just link the same files into the ide layer.

Copy link
Member

Choose a reason for hiding this comment

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

Let me know if that's generally not preferred in this scenario.

It's basically a tradeoff. One form allocatesa linear sequence up front, but has free enumeration.

The other form allocates for enumeration, but doesn't need the contiguous initial allocation.

I'm personally fine with either.

Copy link
Member

Choose a reason for hiding this comment

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

As for net-8, there are Split extensions, but I don't think I'm fully understanding those. It looks like they require a Span argument in which to store the results, but I'm not sure how large to make that.

Ah. My mistake then. Ignore that idea then :-)

I thought there was some sort of SplitLines helper that streamed results.

}

formattedLines.Add(sb.ToString());
}

public static ImmutableArray<ReadOnlyMemory<char>> Split(
ReadOnlyMemory<char> s,
Copy link
Member

Choose a reason for hiding this comment

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

Not: use single character names for lambdas. Give a real name for full method.

@CyrusNajmabadi
Copy link
Member

Only nits. Feel free to ignore.

@ToddGrun ToddGrun merged commit 71a2074 into dotnet:main Aug 29, 2023
@ghost ghost added this to the Next milestone Aug 29, 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.

3 participants