Remove List<IObserver<T>>.ToArray()
allocations in LightweightObservableBase
#18316
+40
−13
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hello folks,☺️
What does the pull request do?
This PR is removing the
List<IObserver<T>>.ToArray()
allocations happening inLightweightObservableBase
when Routing events are fired (e.g. whenever you move the mouse)When profiling the memory, I noticed that when generating lots of routing events (e.g. just moving the mouse over a window) several MB of
IObserver<ValueTuple<Object, RoutedEventArgs>>[]
were created.See for example here with the
ControlDialog
application by just moving the mouse on the Window for a few seconds:What is the current behavior?
I noticed 2 problems:
For example, here are some statistics about these array allocations:
Array size to count:
Total array count: 347807
Total array size in bytes: 13246224 bytes
What is the updated/expected behavior with this PR?
This PR is introducing the case for 0/2/3 elements and pooling the array for other cases with ArrayPool.
After this PR, the allocation is removed:
A direct side effect is less GC collections:
Before this PR, a trigger of the GC happens after 4.6s
After this PR, a trigger of the GC happens after 7s.
How was the solution implemented (if it's not obvious)?
Cases of 0/2/3 and ArrayPool.
I have left the code that can print some statistics commented. If it is a problem, I can remove it from the PR.
Checklist
Breaking changes
None
Obsoletions / Deprecations
None
Fixed issues
None