-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Update ICollection<T> usage to IReadOnlyCollection<T> where applicable #101469
Conversation
Anywhere we're casting to `ICollection<T>` just to use its `Count`, we can instead now cast to `IReadOnlyCollection<T>`, as the former inherits the latter as of .NET 9. This expands the set of types that can light-up with the check; in a couple of places it also lets us remove what would then be a duplicative check. We can do the same for `IList<T>` and `IReadOnlyList<T>`. While dispatch via the DIM could result in an extra interface dispatch for types that weren't previously implementing the read-only interface, a) our collection types already implement both, and b) these cases all represent fast paths where the extra savings from the faster path should more than make up for additional call overheads. I audited for anywhere we were missing explicit implementations and added a few corner-cases in.
Tagging subscribers to this area: @dotnet/area-system-collections |
Shouldn't ReadOnlyCollection also be added to this PR to change it's backing field from IList to IROL? |
It shouldn't matter much -- the constructor itself accepts IList and we can't change that. |
I meant changing the constructor along with the backing field. If constructor can't be changed due to potential breaking changes, then changing the backing field allows to create a factory method instead. |
I don't think it would be possible even if we considered new API. If you take a closer look at the implementation you'll see that it meaningfully relies on the underlying collection being an |
…pplicable (dotnet#101469)" This reverts commit e92b7d0.
…y collection interfaces (#95830)" (#101644) * Revert "Update ICollection<T> usage to IReadOnlyCollection<T> where applicable (#101469)" This reverts commit e92b7d0. * Revert "Make mutable generic collection interfaces implement read-only collection interfaces (#95830)" This reverts commit a2bd583. * Update src/coreclr/System.Private.CoreLib/src/System/Array.CoreCLR.cs
dotnet#101469) Anywhere we're casting to `ICollection<T>` just to use its `Count`, we can instead now cast to `IReadOnlyCollection<T>`, as the former inherits the latter as of .NET 9. This expands the set of types that can light-up with the check; in a couple of places it also lets us remove what would then be a duplicative check. We can do the same for `IList<T>` and `IReadOnlyList<T>`. While dispatch via the DIM could result in an extra interface dispatch for types that weren't previously implementing the read-only interface, a) our collection types already implement both, and b) these cases all represent fast paths where the extra savings from the faster path should more than make up for additional call overheads. I audited for anywhere we were missing explicit implementations and added a few corner-cases in.
…y collection interfaces (dotnet#95830)" (dotnet#101644) * Revert "Update ICollection<T> usage to IReadOnlyCollection<T> where applicable (dotnet#101469)" This reverts commit e92b7d0. * Revert "Make mutable generic collection interfaces implement read-only collection interfaces (dotnet#95830)" This reverts commit a2bd583. * Update src/coreclr/System.Private.CoreLib/src/System/Array.CoreCLR.cs
dotnet#101469) Anywhere we're casting to `ICollection<T>` just to use its `Count`, we can instead now cast to `IReadOnlyCollection<T>`, as the former inherits the latter as of .NET 9. This expands the set of types that can light-up with the check; in a couple of places it also lets us remove what would then be a duplicative check. We can do the same for `IList<T>` and `IReadOnlyList<T>`. While dispatch via the DIM could result in an extra interface dispatch for types that weren't previously implementing the read-only interface, a) our collection types already implement both, and b) these cases all represent fast paths where the extra savings from the faster path should more than make up for additional call overheads. I audited for anywhere we were missing explicit implementations and added a few corner-cases in.
…y collection interfaces (dotnet#95830)" (dotnet#101644) * Revert "Update ICollection<T> usage to IReadOnlyCollection<T> where applicable (dotnet#101469)" This reverts commit e92b7d0. * Revert "Make mutable generic collection interfaces implement read-only collection interfaces (dotnet#95830)" This reverts commit a2bd583. * Update src/coreclr/System.Private.CoreLib/src/System/Array.CoreCLR.cs
Anywhere we're casting to
ICollection<T>
just to use itsCount
, we can instead now cast toIReadOnlyCollection<T>
, as the former inherits the latter as of .NET 9. This expands the set of types that can light-up with the check; in a couple of places it also lets us remove what would then be a duplicative check. We can do the same forIList<T>
andIReadOnlyList<T>
.While dispatch via the DIM could result in an extra interface dispatch for types that weren't previously implementing the read-only interface, a) our collection types already implement both, and b) these cases all represent fast paths where the extra savings from the faster path should more than make up for additional call overheads. I audited for anywhere we were missing explicit implementations and added in a few corner-cases.