Unverified Commit 38604989 authored by Polina Cherkasova's avatar Polina Cherkasova Committed by GitHub

Fix performance regression. (#111615)

parent 96b49aae
......@@ -215,13 +215,11 @@ class ChangeNotifier implements Listenable {
void addListener(VoidCallback listener) {
assert(ChangeNotifier.debugAssertNotDisposed(this));
if (kFlutterMemoryAllocationsEnabled && !_creationDispatched) {
MemoryAllocations.instance.dispatchObjectEvent(() {
return ObjectCreated(
MemoryAllocations.instance.dispatchObjectEvent(ObjectCreated(
library: _flutterFoundationLibrary,
className: 'ChangeNotifier',
object: this,
);
});
));
_creationDispatched = true;
}
if (_count == _listeners.length) {
......@@ -328,7 +326,7 @@ class ChangeNotifier implements Listenable {
return true;
}());
if (kFlutterMemoryAllocationsEnabled && _creationDispatched) {
MemoryAllocations.instance.dispatchObjectEvent(() => ObjectDisposed(object: this));
MemoryAllocations.instance.dispatchObjectEvent(ObjectDisposed(object: this));
}
_listeners = _emptyListeners;
_count = 0;
......@@ -466,13 +464,11 @@ class ValueNotifier<T> extends ChangeNotifier implements ValueListenable<T> {
/// Creates a [ChangeNotifier] that wraps this value.
ValueNotifier(this._value) {
if (kFlutterMemoryAllocationsEnabled) {
MemoryAllocations.instance.dispatchObjectEvent(() {
return ObjectCreated(
MemoryAllocations.instance.dispatchObjectEvent(ObjectCreated(
library: _flutterFoundationLibrary,
className: 'ValueNotifier',
object: this,
);
});
));
}
_creationDispatched = true;
}
......
......@@ -12,11 +12,11 @@ const bool _kMemoryAllocations = bool.fromEnvironment('flutter.memory_allocation
/// If true, Flutter objects dispatch the memory allocation events.
///
/// By default, the constant is true for profile and debug mode and false
/// for release mode, for app size optimization goals.
/// By default, the constant is true for debug mode and false
/// for profile and release modes.
/// To enable the dispatching for release mode, pass the compilation flag
/// `--dart-define=flutter.memory_allocations=true`.
const bool kFlutterMemoryAllocationsEnabled = _kMemoryAllocations || kProfileMode || kDebugMode;
const bool kFlutterMemoryAllocationsEnabled = _kMemoryAllocations || kDebugMode;
const String _dartUiLibrary = 'dart:ui';
......@@ -54,9 +54,6 @@ abstract class ObjectEvent{
/// A listener of [ObjectEvent].
typedef ObjectEventListener = void Function(ObjectEvent);
/// A builder of [ObjectEvent].
typedef ObjectEventBuilder = ObjectEvent Function();
/// An event that describes creation of an object.
class ObjectCreated extends ObjectEvent {
/// Creates an instance of [ObjectCreated].
......@@ -225,7 +222,7 @@ class MemoryAllocations {
/// after the removal.
///
/// Only call this when [kFlutterMemoryAllocationsEnabled] is true.
void dispatchObjectEvent(ObjectEventBuilder objectEventBuilder) {
void dispatchObjectEvent(ObjectEvent event) {
if (!kFlutterMemoryAllocationsEnabled) {
return;
}
......@@ -234,7 +231,6 @@ class MemoryAllocations {
return;
}
final ObjectEvent event = objectEventBuilder();
_activeDispatchLoops++;
final int end = listeners.length;
for (int i = 0; i < end; i++) {
......@@ -285,38 +281,30 @@ class MemoryAllocations {
}
void _imageOnCreate(ui.Image image) {
dispatchObjectEvent(() {
return ObjectCreated(
dispatchObjectEvent(ObjectCreated(
library: _dartUiLibrary,
className: 'Image',
object: image,
);
});
));
}
void _pictureOnCreate(ui.Picture picture) {
dispatchObjectEvent(() {
return ObjectCreated(
dispatchObjectEvent(ObjectCreated(
library: _dartUiLibrary,
className: 'Picture',
object: picture,
);
});
));
}
void _imageOnDispose(ui.Image image) {
dispatchObjectEvent(() {
return ObjectDisposed(
dispatchObjectEvent(ObjectDisposed(
object: image,
);
});
));
}
void _pictureOnDispose(ui.Picture picture) {
dispatchObjectEvent(() {
return ObjectDisposed(
dispatchObjectEvent(ObjectDisposed(
object: picture,
);
});
));
}
}
......@@ -24,13 +24,13 @@ void main() {
ma.addListener(listener);
_checkSdkHandlersSet();
ma.dispatchObjectEvent(() => event);
ma.dispatchObjectEvent(event);
expect(recievedEvent, equals(event));
expect(ma.hasListeners, isTrue);
recievedEvent = null;
ma.removeListener(listener);
ma.dispatchObjectEvent(() => event);
ma.dispatchObjectEvent(event);
expect(recievedEvent, isNull);
expect(ma.hasListeners, isFalse);
_checkSdkHandlersNotSet();
......@@ -56,7 +56,7 @@ void main() {
ma.addListener(badListener2);
ma.addListener(listener2);
ma.dispatchObjectEvent(() => event);
ma.dispatchObjectEvent(event);
expect(log, <String>['badListener1', 'listener1', 'badListener2','listener2']);
expect(tester.takeException(), contains('Multiple exceptions (2)'));
......@@ -69,7 +69,7 @@ void main() {
log.clear();
expect(ma.hasListeners, isFalse);
ma.dispatchObjectEvent(() => event);
ma.dispatchObjectEvent(event);
expect(log, <String>[]);
});
......@@ -86,11 +86,11 @@ void main() {
ma.addListener(listener1);
_checkSdkHandlersSet();
ma.dispatchObjectEvent(() => event);
ma.dispatchObjectEvent(event);
expect(log, <String>['listener1']);
log.clear();
ma.dispatchObjectEvent(() => event);
ma.dispatchObjectEvent(event);
expect(log, <String>['listener1','listener2']);
log.clear();
......@@ -99,7 +99,7 @@ void main() {
_checkSdkHandlersNotSet();
expect(ma.hasListeners, isFalse);
ma.dispatchObjectEvent(() => event);
ma.dispatchObjectEvent(event);
expect(log, <String>[]);
});
......@@ -117,7 +117,7 @@ void main() {
ma.addListener(listener1);
ma.addListener(listener2);
ma.dispatchObjectEvent(() => event);
ma.dispatchObjectEvent(event);
expect(log, <String>['listener1']);
log.clear();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment