Create class MemoryAllocations.#110230
Conversation
dnfield
left a comment
There was a problem hiding this comment.
I'm not quite clear on how this will be used or why it belongs in the framework at this point.
goderbauer
left a comment
There was a problem hiding this comment.
It's kinda difficult reviewing this skeleton without docs filled in and seeing how this would be used by other parts of the framework.
| object: this, | ||
| )); | ||
| } | ||
| _creationDispatched = true; |
There was a problem hiding this comment.
Why is the inherited implementation from ChangeNotifier not enough? Also, it is odd that this is reaching out to a private in ChangeNotifier. Does that mean, I couldn't implement my own ValueNotifier-like class based on ChangeNotifier outside of the framework?
There was a problem hiding this comment.
Yes, the implementation is not super-elegant. However, as there is no constructor in ChangeNotifier (because it is used as mixin), I did not find a better way.
You can implement ValueNotifier-like class based on ChangeNotifier though. It will dispatch the events if only a listener is added.
| @override | ||
| void addListener(VoidCallback listener) { | ||
| assert(ChangeNotifier.debugAssertNotDisposed(this)); | ||
| if (!_creationDispatched) { |
There was a problem hiding this comment.
One thought that occurred to me: In release builds that ship to customers you probably want this entire code tree-shaken out. That's currently no possible?
There was a problem hiding this comment.
Thank you for bringing it. Introduced kFlutterMemoryAllocationsEnabled and verified tree-shaking happens (see screen shot in description)
| assert(ChangeNotifier.debugAssertNotDisposed(this)); | ||
| if (!_creationDispatched) { | ||
| if (MemoryAllocations.instance.hasListeners) { | ||
| MemoryAllocations.instance.dispatchObjectEvent(ObjectCreated( |
There was a problem hiding this comment.
I am not sure how these events will be consumed, but will it not be confusing if an object creation notification is received somewhat randomly long after the object has been created?
There was a problem hiding this comment.
Yes, it is not perfect, but I do not see other way to inform about ChangeNotifier creation.
|
|
||
| /// List of the Flutter Framework and SDK libraries with instrumented | ||
| /// classes. | ||
| class FlutterLibraries { |
There was a problem hiding this comment.
Does this have to be public API?
| try { | ||
| listeners[i]?.call(objectEvent); | ||
| } catch (exception, stack) { | ||
| final String type = objectEvent.object.runtimeType.toString(); |
There was a problem hiding this comment.
There should be a no_runtimeType_toString lint on this line, no?
There was a problem hiding this comment.
nope
And if i remove the type, I get lint about return type
| return; | ||
| } | ||
|
|
||
| if (_activeDispatchLoops > 0) { |
There was a problem hiding this comment.
For my own understanding, why is the removal/notification logic so different from ChangeNotifier? I wish we could just reuse that logic instead of reinventing everything.
There was a problem hiding this comment.
ChangeNotifier is different, because it is not-singletone and is optimized for often adding/removing listeners.
One of comments in ChangeNotifier is 'The list holding the listeners is not growable for performances reasons.'.
We are ok with growable list for MemoryAllocations, because it is singletone.
There was a problem hiding this comment.
We still care about performance for event dispatching, as the number of events can be huge.
|
SkiaPerf is reporting regressions in some microbenchmarks on this change: Does the flag to disable this feature need to be passed in more places? |
|
Following up. |
|
Regression is triggered by: Guidance: https://github.com/flutter/flutter/tree/master/dev/devicelab Command: https://github.com/polina-c/spikes/tree/master/memory_allocation_investigation |
|
From offline conversation:
@polina-c is going to follow up with a patch to remove the |
|
fix: #111615 |
|
(to get this link i opened the regression link and scrolled right by pressing 'd') |
Design: go/dart-leaktracker-productization
Peer PR in engine: flutter/engine#35274
Verified memory_allocations.dart is not included into release build if the flag
--dart-define=flutter.memory_allocations=trueis not passed: