Avoid redundant boxing in ValueTuple<T1,...,T7,TRest>#128814
Conversation
Use pattern variable capture (`is IValueTupleInternal rest`) instead of a separate type-check followed by a cast to IValueTupleInternal. This avoids a redundant boxing of TRest when it is a ValueTuple<TRest> containing reference-type fields, reducing heap allocations in GetHashCode, ToString, ITuple.Length, and the ITuple indexer.
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Refactors ValueTuple.cs to use pattern matching with type-checked variable declarations, replacing redundant cast expressions after is checks.
Changes:
- Replaces
is IValueTupleInternalchecks followed by((IValueTupleInternal)Rest)casts withis IValueTupleInternal restpattern matching. - Applies the change in
GetHashCode,ToString,IValueTupleInternal.ToStringEnd,ITuple.Length, and the indexer.
EgorBo
left a comment
There was a problem hiding this comment.
There is a known issue somewhere against JIT for this pattern around shared generics, but I think it's fine to accept this change anyway as it simplifies code.
|
Thank you for the review! |
|
Tagging subscribers to this area: @dotnet/area-system-runtime |
This PR use pattern variable capture (
is IValueTupleInternal rest) instead of a separate type-check followed by a cast toIValueTupleInternal. This avoids a redundant boxing ofTRestwhen it is aValueTuple<TRest>containing reference-type fields, reducing heap allocations inGetHashCode,ToString,ITuple.Length, and theITupleindexer.Benchmark