Skip to content

Improve allocations in evaluations by using ImmutableArray here and there #8753

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

Merged
merged 1 commit into from
May 17, 2023

Conversation

rokonec
Copy link
Member

@rokonec rokonec commented May 12, 2023

Related to #8673

Context

When analyzing traces from VS Orchard evaluation I have noticed few places which are easy to optimize by using ImmutableArray instead of ImmutableList.
ImmutableArray is almost always better in patters loop {mutate by builder}; ToImmutable since it is struct, allocates less, and have faster enumerator.

Changes Made

There are many places where using ImmutableArray instead of ImmutableList would render better performance. I however limit changes only on places which sticks out from memory traces as non-negligable.

Testing

Local. PR gate.

Notes

Overall gain will be small, in percent's of overall allocations, but given relatively simple and safe changes, I think it is good tradeoff.

@rokonec rokonec requested a review from ladipro May 12, 2023 13:05
Copy link
Member

@ladipro ladipro left a comment

Choose a reason for hiding this comment

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

Looks good! Would it maybe help if the code used an interface (IEnumerable<T>?) instead of the concrete type to indicate what operations on the collection are actually used?

@ladipro
Copy link
Member

ladipro commented May 12, 2023

Oh, that would mean boxing. Never mind 😀

@rainersigwald
Copy link
Member

Yeah, I remember a bunch of "concretize these types to avoid boxing" PRs a few years ago and was about to jump in :)

Copy link
Member

@rainersigwald rainersigwald left a comment

Choose a reason for hiding this comment

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

LGTM but I'd prefer to see the first two commits go in with #8747 and then rebase/squash this.

Also I think ImmutableArray is a good choice here, but we also have access to some Roslyn collections like ImmutableSegmentedList that can be helpful in other cases (here I don't think we're likely to hit the LOH for metadata so the segmentation doesn't help).

@rokonec rokonec force-pushed the rokonec/immutable-perf2 branch from 7f8790b to c71a8a9 Compare May 15, 2023 15:12
@JaynieBai JaynieBai merged commit f3408b0 into dotnet:main May 17, 2023
@rokonec rokonec deleted the rokonec/immutable-perf2 branch May 17, 2023 09:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants