-
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
IListProvider<T>.ToList()
implementations use list.Add(element)
instead of direct array initialization.
#80760
Comments
Tagging subscribers to this area: @dotnet/area-system-collections Issue DetailsDescription
This is to not forget to update the implementations once #80311 / #80311 or #80756 is closed. ConfigurationAll versions Regression?No AnalysisOnce we have an API for direct
|
Which implementations do you specifically have in mind? I'm looking at the implementation of runtime/src/libraries/System.Linq/src/System/Linq/Select.SpeedOpt.cs Lines 310 to 320 in 47f171e
If you could identify the implementations that could be optimized we might take a look at accepting a PR that improves things, provided it does not regress assembly size substantially. |
This issue has been marked |
As you noted, implementations do specialize Once BCL gets #55217 (or any of the alternatives), the performance can be improved by removing such overhead. This issue tracks this optimization. |
Can you clarify? Looks like it does presize the list: runtime/src/libraries/System.Linq/src/System/Linq/Range.SpeedOpt.cs Lines 30 to 39 in ad9efe8
|
It presizes the capacity, but not the count, and as such it still goes through the Add call to then fill in each member. They're suggesting that instead of: for (int cur = _start; cur != _end; cur++)
{
list.Add(cur);
} which will incur a bounds check, a version increment, etc. on every Add, that we could instead do something like that: CollectionsMarshal.SetCount(list, _end - _start);
Span<int> span = CollectionsMarshal.AsSpan(list);
for (int i = 0; i < span.Length; i++)
{
span[i] = i + _start;
} |
Sounds reasonable. These components are constrained to the @neon-sunset would you be interested in contributing a PR? |
It's not possible to do until SetCount is added, and PR #77794 was closed. Do we have a plan for finishing that? |
I think we do. Seems it was closed by the bot for inactivity. |
Yes, thanks, this was the intention when creating this issue, to not miss it when it becomes possible. |
It is already possible to initialize a |
It appears that the blocker has been resolved, |
Thanks. You're welcome to if you find opportunity. I think most of it has been done, however. This probably should have been closed as completed. |
I'm still finding a few spots I think we can optimize if we feel it's warranted. These are locations where the list has a known size but where we must iterate the source using
|
Description
IListProvider<T>.ToList()
implementations inSystem.Linq
for iterators of known lengths uselist.Add(element)
to initialize the list over directly writing to its underlying array.This is to not forget to update the implementations once any of the following is done: #80311, #55217 or #80756.
Configuration
All versions
Regression?
No
Analysis
Once we have an API for direct
List<T>
initialization (aside fromnew List<T>(collection)
), it's a good idea to utilize it in BCL to reduce the overhead.The text was updated successfully, but these errors were encountered: