-
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
Performance: Make constructor List<T>(IEnumerable<T> collection) know about IReadOnlyCollection #27517
Comments
This will cause a performance degradation in existing code, the majority will pay the cost for something they are unlikely to use and have no opportunity to opt out of it. Casting is relatively slow and generic casts are slower still and can cause jit-ing of the IReadonlyCollection type if it isn't used already. Adding code to core parts of the clr like Dictionary and List has a multiplicative cost in size because of native copies of common types are precompiled for performance. While that doesn't mean common methods can't increase in size it makes the bar for change approval on high-use types is higher than you'd expect and that evidence of improvement must be considerable. In this case you could provide an Extension method to create a list from a readonly collection that presizes using the existing api couldn't you? |
Well, that is not exactly unexpected... I just keep coming across this on a fairly regular basis... It popped up in a trace again today - in fact, it wasn't even that This And while I do think that it's unfortunate, that e.g. |
In the following
List<T>
constructor:https://github.com/dotnet/corefx/blob/e043326e204a9566ee26403b522cf95cda2d54ee/src/Common/src/CoreLib/System/Collections/Generic/List.cs#L67
there is some special logic built-in already to support the efficient initialization of a
List<T>
based on an existingICollection<T>
. With the advent ofIReadOnlyCollection<T>
this constructor logic could be extended to also support this type.IReadOnlyCollection<T>
does not support aCopyTo()
method at this stage but at least the cost of array resizes could be alleviated here at the expense of another branch/type check.The text was updated successfully, but these errors were encountered: