KEMBAR78
Reduce allocations around empty ReadOnlyCollections by stephentoub · Pull Request #76097 · dotnet/runtime · GitHub
Skip to content

Conversation

stephentoub
Copy link
Member

@stephentoub stephentoub commented Sep 23, 2022

  • 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

Contributes to #76028

- 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
@ghost
Copy link

ghost commented Sep 23, 2022

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.

@ghost
Copy link

ghost commented Sep 23, 2022

Tagging subscribers to this area: @dotnet/area-system-collections
See info in area-owners.md if you want to be subscribed.

Issue Details
  • Add an internal (for now) ReadOnlyCollection<T>.Empty
  • Return the singleton ReadOnlyCollection<T>.Empty from List<T>.AsReadOnly and 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

Contributes to #76028

Author: stephentoub
Assignees: stephentoub
Labels:

area-System.Collections

Milestone: -

@KalleOlaviNiemitalo
Copy link

Return the singleton ReadOnlyCollection<T>.Empty from List<T>.AsReadOnly

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.

@stephentoub
Copy link
Member Author

That would be a breaking change for code like

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.

Copy link
Member

@krwq krwq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@stephentoub stephentoub merged commit ef6cb3d into dotnet:main Sep 29, 2022
@stephentoub stephentoub deleted the rocempty branch September 29, 2022 11:37
@ghost ghost locked as resolved and limited conversation to collaborators Oct 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants