Reevaluate default capacity for List<T> #78114
Replies: 5 comments 4 replies
-
Moving to runtime. This is an api question, not a lang question. Thanks! :) Note: you might be interested in the work we're looking into in the next C# with collections: dotnet/csharplang#5354 |
Beta Was this translation helpful? Give feedback.
-
Probably? Whoever wants to change that needs to do some sort of analysis of different workloads to estimate impact (how many lists are not small? what is the potential working set impact from a more aggressive resizing policy?)
This is one is not easy - it is unlikely we want to introduce a new field in List<> to keep user's policy on how to grow it. It, probably, can be a static [readonly] field. |
Beta Was this translation helpful? Give feedback.
-
I know that back in the days the .NET team did serious research into hard-coded numbers like this, but over the years lot's of useful publication material got lost due to various reasons. Like you I could not find any material on this particular number any more, but I really hope someone can shed some light on the origin of it. |
Beta Was this translation helpful? Give feedback.
-
It would make sense to me if the default size would be 0 and increased to 4 with the addition of the first element. This doesn't matter much. If the programmer is interested in optimization, he enters his own value in the second constructor. |
Beta Was this translation helpful? Give feedback.
-
I imagine that the best initial default for List should have changed around the time |
Beta Was this translation helpful? Give feedback.
-
Reevaluating the default capacity in List
The current default capacity for the internal array for the List type is set to 4 and a user has little to no control over how the internal array grows unless they implement their own generic List type.
This number, 4, with unknown origins, has remained unchanged since it's first implementation and might not be the optimal default capacity. The language has evolved with new types, and a new approach that extends beyond traditional OOP.
Would it make sense to reevaluate the default capacity?
It's current number as well as it's role in resizing the internal array?
And what about giving the user more control over how the internal array grows?
Note:
This discussion started between me and a Java developer when we compared the internal array default capacities between the two languages (Java uses 10). The number was based on an empirical study, and the author seemed to be regretful :)
Beta Was this translation helpful? Give feedback.
All reactions