Skip to content

Conversation

@DustinCampbell
Copy link
Member

Instead of providing separate implementations, ToArray() and ToArrayAndClear() should just call ToImmutable() and ToImmutableAndClear() and return the underlying array. This ensures that we get the benefit of returning the builder's array when the capacity matches the count.

Instead of providing separate implementations, `ToArray()` and `ToArrayAndClear()` should just call `ToImmutable()` and `ToImmutableAndClear()` and return the underlying array. This ensures that we get the benefit of returning the builder's array when the capacity matches the count.
@DustinCampbell DustinCampbell requested a review from a team as a code owner September 11, 2025 16:12
_ => [_element0, _element1, _element2, _element3]
};
}
=> ImmutableCollectionsMarshal.AsArray(ToImmutable()).AssumeNotNull();
Copy link
Contributor

Choose a reason for hiding this comment

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

=> ImmutableCollectionsMarshal.AsArray(ToImmutable()).AssumeNotNull();

Curious why you didn't keep the ToArray* impls and change the ToImmutable* impls to use those instead.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's simpler this way. ImmutableArray<T>.Builder.DrainToImmutable() returns an ImmutableArray<T> -- there's no DrainToArray(). So, we'd have to unwrap the result in ToArray() and then the ToImmutable() methods would need to re-wrap the array. Seems more awkward that way.

Copy link
Contributor

Choose a reason for hiding this comment

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

Dur, Todd

Copy link
Contributor

@ToddGrun ToddGrun left a comment

Choose a reason for hiding this comment

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

:shipit:

@DustinCampbell DustinCampbell merged commit f3543e7 into dotnet:main Sep 11, 2025
11 checks passed
@DustinCampbell DustinCampbell deleted the improve-toarray branch September 11, 2025 20:37
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Sep 11, 2025
@akhera99 akhera99 modified the milestones: Next, 18.0 P1, 18.0 P2 Sep 22, 2025
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.

5 participants