-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Reduce allocations around empty ReadOnlyCollections #76097
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
Conversation
- Add an internal (for now) `ReadOnlyCollection<T>.Empty` - Return the singleton `ReadOnlyCollection<T>.Empty` from `Array.AsReadOnly` when the source is empty - Return a singleton empty enumerator from an empty `ReadOnlyCollection<T>` - Fix up some reflection code to avoid unnecessary `ReadOnlyCollection<T>` allocations (e.g. just to wrap empty arrays) - Fix a null reference bug in RuntimeCustomAttributeData.NamedArguments (I don't know if it's actually reachable, but this property shouldn't be returning null) - Avoid a few unnecessary allocations in TimeZoneInfo.GetSystemTimeZones
|
I couldn't figure out the best area label to add to this PR. If you have write-permissions please help me learn by adding exactly one area label. |
|
Tagging subscribers to this area: @dotnet/area-system-collections Issue Details
Contributes to #76028
|
src/libraries/System.Private.CoreLib/src/System/Collections/ObjectModel/ReadOnlyCollection.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Collections/ObjectModel/ReadOnlyCollection.cs
Show resolved
Hide resolved
That would be a breaking change for code like var list = new List<int>();
ReadOnlyCollection<int> readOnly = list.AsReadOnly();
list.Add(42);
Debug.Assert(readOnly.Count == 1);However, the PR does not actually include such a change, as it does not modify src/libraries/System.Private.CoreLib/src/System/Collections/Generic/List.cs. |
Yes, that's why it doesn't. I wrote the description before I actually implemented things, thought I removed that part of the description, but apparently didn't. |
src/libraries/Common/tests/System/Collections/IEnumerable.Generic.Tests.cs
Show resolved
Hide resolved
src/libraries/Common/tests/System/Collections/IDictionary.Generic.Tests.cs
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
ReadOnlyCollection<T>.EmptyReadOnlyCollection<T>.EmptyfromArray.AsReadOnlywhen the source is emptyReadOnlyCollection<T>ReadOnlyCollection<T>allocations (e.g. just to wrap empty arrays)Contributes to #76028