-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Add optimized path for IReadOnlyCollection/IReadOnlyList in System.Linq #23910
Comments
Optimized path in what? What optimizations? What's the scenario? |
Sorry for forgetting type LINQ in the title. |
@stephentoub @huoyaoyuan It's not just about Linq but also in many other areas like: List.InsertRange, see: http://referencesource.microsoft.com/#mscorlib/system/collections/generic/list.cs,711 If you give an IReadOnlyCollection, and that instance does not implement ICollection too, it will then instanciate an Enumerator and iterate, otherwise it could be super optimized. By the way, shouldn't ICollection implement IReadOnlyCollection? Then less code would need to touched: we would only need to change the cast to IReadOnlyCollection here. |
Yes. But adding that
It's actually a breaking change. See https://github.com/dotnet/corefx/issues/5489 for an example. |
Thanks @stephentoub for the quick feedback. Just another remark related to this, Array.cs doesn't implement IReadOnlyList neither IReadOnlyCollection. Would you accept a PR doing so? |
Arrays do, they're just a very special type and the implementation is provided by the runtime, i.e. this works fine: using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
var data = new int[1] { 42 };
IReadOnlyList<int> list = data;
Console.WriteLine(list[0]);
}
} |
That's interesting! I expected what is in Array.cs to be the source of truth. |
@molinch |
Many things are much more clear, thanks @mattwar ! |
Closing as dup of https://github.com/dotnet/corefx/issues/26579 |
It's kind of historical reason for the readonly interfaces not inherited by
ICollection<T>
/IList<T>
.Adding a optimized path needs a copy of all
ICollection<T>
/IList<T>
paths and classes.Implementing
ICollection<T>
withIsReadOnly = true
does get the optimizations, but it's kind of violating the interfaces.The text was updated successfully, but these errors were encountered: