Skip to content

Add optimisation for IReadOnlyCollection<TSource> - #135262

Draft
MikeBarker-MSFT wants to merge 1 commit into
dotnet:mainfrom
MikeBarker-MSFT:feature/ireadonlycollection-count
Draft

MikeBarker-MSFT wants to merge 1 commit into
dotnet:mainfrom
MikeBarker-MSFT:feature/ireadonlycollection-count

Conversation

@MikeBarker-MSFT

@MikeBarker-MSFT MikeBarker-MSFT commented Oct 6, 2026 •

Copy link
Copy Markdown

Add optimisation for IReadOnlyCollection<TSource> when using the .Count property.

The optimisations in LINQ take consideration of ICollection<T>, Iterator<T> and ICollection; these optimisations can be expanded for the IReadOnlyCollection<T> case too. This occurs in Count, TryGetNonEnumeratedCount, Any, and various helper methods.

I have also added a test case for the optimisation route for TryGetNonEnumeratedCount. Other routes do not have specific tests.

(FYI: A further code clean-up could collapse these occurrences to all utilise TryGetNonEnumeratedCount and reduce code duplication. This is not done in this PR.)

Fix #135266

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 6, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-linq
See info in area-owners.md if you want to be subscribed.

@MikeBarker-MSFT
MikeBarker-MSFT marked this pull request as draft October 6, 2026 08:46
@huoyaoyuan

Copy link
Copy Markdown
Member

It's still under discussion for whether we should do the optimization like this. See #42254.

@MikeBarker-MSFT
MikeBarker-MSFT marked this pull request as ready for review October 6, 2026 09:11
@MikeBarker-MSFT

MikeBarker-MSFT commented Oct 6, 2026 •

Copy link
Copy Markdown
Author

It's still under discussion for whether we should do the optimization like this. See #42254.

Thank you @huoyaoyuan , I had searched for such an issue, but has missed this one.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Linq community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add IReadOnlyCollection<T> as a optimised path for LINQ methods (e.g. .Count())

2 participants