Skip to content

Conversation

@stephentoub
Copy link
Member

When initialized with a single object rather than a list of objects (single object is very common), the args is allocating an object[] and then wrapping that object[] in a ReadOnlyList. We can instead just allocate a simple read-only IList wrapper for the object directly; that also makes accessing it faster.

Additionally, when constructing an instance using public NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction action, IList? changedItems, int index, int oldIndex), even though the same collection instance is used for both the new and old items, it wraps it twice. We can wrap it once.

Along the way I also simplified the code, enabling the fields to become readonly, removing duplicate assignments of fields, etc.

When initialized with a single object rather than a list of objects (single object is very common), the args is allocating an object[] and then wrapping that object[] in a ReadOnlyList.  We can instead just allocate a simple read-only IList wrapper for the object directly; that also makes accessing it faster.

Additionally, when constructing an instance using `public NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction action, IList? changedItems, int index, int oldIndex)`, even though the same collection instance is used for both the new and old items, it wraps it twice.  We can wrap it once.

Along the way I also simplified the code, enabling the fields to become readonly, removing duplicate assignments of fields, etc.
@ghost
Copy link

ghost commented Jun 29, 2021

Tagging subscribers to this area: @eiriktsarpalis
See info in area-owners.md if you want to be subscribed.

Issue Details

When initialized with a single object rather than a list of objects (single object is very common), the args is allocating an object[] and then wrapping that object[] in a ReadOnlyList. We can instead just allocate a simple read-only IList wrapper for the object directly; that also makes accessing it faster.

Additionally, when constructing an instance using public NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction action, IList? changedItems, int index, int oldIndex), even though the same collection instance is used for both the new and old items, it wraps it twice. We can wrap it once.

Along the way I also simplified the code, enabling the fields to become readonly, removing duplicate assignments of fields, etc.

Author: stephentoub
Assignees: -
Labels:

area-System.Collections

Milestone: -

Copy link
Member

@eiriktsarpalis eiriktsarpalis left a comment

Choose a reason for hiding this comment

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

Thanks

@stephentoub stephentoub merged commit 1ceda71 into dotnet:main Jun 30, 2021
@stephentoub stephentoub deleted the notificationchangealloc branch June 30, 2021 11:56
@ghost ghost locked as resolved and limited conversation to collaborators Jul 30, 2021
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.

2 participants