-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[web] remove obsolete object caches; simplify native object management (
#40894) (this is attempt 3; details below) Remove obsolete object caches and introduce a simpler way to manage native objects: * Remove the unused `SynchronousSkiaObjectCache`. * Introduce new library `native_memory.dart` that's smaller and simpler than `skia_object_cache.dart`. * Introduce two types of native object references: * `UniqueRef` a reference with a unique Dart object owner. * `CountedRef` a ref-counted reference with multiple Dart object owners. * All native references use GC (via `FinalizationRegistry`) as a back-up. * The new library removes everything related to object resurrection that was needed only in browsers that didn't support `FinalizationRegistry`. All browsers support it now. * Remove the ad hoc `SkParagraph` cache that predates the introduction of `Paragraph.dispose`. * Rewrite `CkParagraph` in terms of `UniqueRef`. * Rewrite `CkImage` in terms of `CountedRef`; delete `SkiaObjectBox`. This PR does not migrate all objects from the old `skia_object_cache.dart` to `native_memory.dart`. That would be too big of a change. The migration can be done in multiple smaller PRs. This also removes a few unnecessary relayouts observed in flutter/flutter#120921, but not all of them (more details in flutter/flutter#120921 (comment)) ## About attempt 3 More about [attempt 2 here](#40862). In this attempt 3 I'm replacing the `factory` with a top-level function.
- Loading branch information
Showing
13 changed files
with
559 additions
and
772 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'dart:js_interop'; | ||
import 'package:meta/meta.dart'; | ||
|
||
import '../../engine.dart' show Instrumentation; | ||
import '../util.dart'; | ||
import 'canvaskit_api.dart'; | ||
|
||
/// Collects native objects that weren't explicitly disposed of using | ||
/// [UniqueRef.dispose] or [CountedRef.unref]. | ||
SkObjectFinalizationRegistry _finalizationRegistry = createSkObjectFinalizationRegistry( | ||
(UniqueRef<Object> uniq) { | ||
uniq.collect(); | ||
}.toJS | ||
); | ||
|
||
NativeMemoryFinalizationRegistry nativeMemoryFinalizationRegistry = NativeMemoryFinalizationRegistry(); | ||
|
||
/// An indirection to [SkObjectFinalizationRegistry] to enable tests provide a | ||
/// mock implementation of a finalization registry. | ||
class NativeMemoryFinalizationRegistry { | ||
void register(Object owner, UniqueRef<Object> ref) { | ||
_finalizationRegistry.register(owner, ref); | ||
} | ||
} | ||
|
||
/// Manages the lifecycle of a C++ object referenced by a single Dart object. | ||
/// | ||
/// It is expected that when the C++ object is no longer needed [dispose] is | ||
/// called. | ||
/// | ||
/// To prevent memory leaks, the underlying C++ object is deleted by the GC if | ||
/// it wasn't previously disposed of explicitly. | ||
class UniqueRef<T extends Object> { | ||
UniqueRef(Object owner, T nativeObject, this._debugOwnerLabel) { | ||
_nativeObject = nativeObject; | ||
if (Instrumentation.enabled) { | ||
Instrumentation.instance.incrementCounter('$_debugOwnerLabel Created'); | ||
} | ||
nativeMemoryFinalizationRegistry.register(owner, this); | ||
} | ||
|
||
T? _nativeObject; | ||
final String _debugOwnerLabel; | ||
|
||
/// Returns the underlying native object reference, if it has not been | ||
/// disposed of yet. | ||
/// | ||
/// The returned reference must not be stored. I should only be borrowed | ||
/// temporarily. Storing this reference may result in dangling pointer errors. | ||
T get nativeObject { | ||
assert(!isDisposed, 'Native object was disposed.'); | ||
return _nativeObject!; | ||
} | ||
|
||
/// Returns whether the underlying native object has been disposed and | ||
/// therefore can no longer be used. | ||
bool get isDisposed => _nativeObject == null; | ||
|
||
/// Disposes the underlying native object. | ||
/// | ||
/// The underlying object may be deleted or its ref count may be bumped down. | ||
/// The exact action taken depends on the sharing model of that particular | ||
/// object. For example, an [SkImage] may not be immediately deleted if a | ||
/// [SkPicture] exists that still references it. On the other hand, [SkPaint] | ||
/// is deleted eagerly. | ||
void dispose() { | ||
assert(!isDisposed, 'A native object reference cannot be disposed more than once.'); | ||
if (Instrumentation.enabled) { | ||
Instrumentation.instance.incrementCounter('$_debugOwnerLabel Deleted'); | ||
} | ||
final SkDeletable object = nativeObject as SkDeletable; | ||
if (!object.isDeleted()) { | ||
object.delete(); | ||
} | ||
_nativeObject = null; | ||
} | ||
|
||
/// Called by the garbage [Collector] when the owner of this handle is | ||
/// collected. | ||
/// | ||
/// Garbage collection is used as a back-up for the cases when the handle | ||
/// isn't disposed of explicitly by calling [dispose]. It most likely | ||
/// indicates a memory leak or inefficiency in the framework or application | ||
/// code. | ||
@visibleForTesting | ||
void collect() { | ||
if (!isDisposed) { | ||
if (Instrumentation.enabled) { | ||
Instrumentation.instance.incrementCounter('$_debugOwnerLabel Leaked'); | ||
} | ||
dispose(); | ||
} | ||
} | ||
} | ||
|
||
/// Interface that classes wrapping [UniqueRef] must implement. | ||
/// | ||
/// Used to collect stack traces in debug mode. | ||
abstract class StackTraceDebugger { | ||
/// The stack trace pointing to code location that created or upreffed a | ||
/// [CountedRef]. | ||
StackTrace get debugStackTrace; | ||
} | ||
|
||
/// Manages the lifecycle of a C++ object referenced by multiple Dart objects. | ||
/// | ||
/// Uses reference counting to manage the lifecycle of the C++ object. | ||
/// | ||
/// If the C++ object has a unique owner, use [UniqueRef] instead. | ||
/// | ||
/// The [ref] method can be used to increment the refcount to tell this box to | ||
/// keep the underlying C++ object alive. | ||
/// | ||
/// The [unref] method can be used to decrement the refcount indicating that a | ||
/// referring object no longer needs it. When the refcount drops to zero the | ||
/// underlying C++ object is deleted. | ||
/// | ||
/// In addition to ref counting, this object is also managed by GC. When this | ||
/// reference is garbage collected, the underlying C++ object is automatically | ||
/// deleted. This is mostly done to prevent memory leaks in production. Well | ||
/// behaving framework and app code are expected to rely on [ref] and [unref] | ||
/// for timely collection of resources. | ||
class CountedRef<R extends StackTraceDebugger, T extends Object> { | ||
/// Creates a counted reference. | ||
CountedRef(T nativeObject, R debugReferrer, String debugLabel) { | ||
_ref = UniqueRef<T>(this, nativeObject, debugLabel); | ||
if (assertionsEnabled) { | ||
debugReferrers.add(debugReferrer); | ||
} | ||
assert(refCount == debugReferrers.length); | ||
} | ||
|
||
/// The native object reference whose lifecycle is being managed by this ref | ||
/// count. | ||
/// | ||
/// Do not store this value outside this class. | ||
late final UniqueRef<T> _ref; | ||
|
||
/// Returns the underlying native object reference, if it has not been | ||
/// disposed of yet. | ||
/// | ||
/// The returned reference must not be stored. I should only be borrowed | ||
/// temporarily. Storing this reference may result in dangling pointer errors. | ||
T get nativeObject => _ref.nativeObject; | ||
|
||
/// The number of objects sharing references to this box. | ||
/// | ||
/// When this count reaches zero, the underlying [nativeObject] is scheduled | ||
/// for deletion. | ||
int get refCount => _refCount; | ||
int _refCount = 1; | ||
|
||
/// Whether the underlying [nativeObject] has been disposed and is no longer | ||
/// accessible. | ||
bool get isDisposed => _ref.isDisposed; | ||
|
||
/// When assertions are enabled, stores all objects that share this box. | ||
/// | ||
/// The length of this list is always identical to [refCount]. | ||
/// | ||
/// This list can be used for debugging ref counting issues. | ||
final Set<R> debugReferrers = <R>{}; | ||
|
||
/// If asserts are enabled, the [StackTrace]s representing when a reference | ||
/// was created. | ||
List<StackTrace> debugGetStackTraces() { | ||
if (assertionsEnabled) { | ||
return debugReferrers | ||
.map<StackTrace>((R referrer) => referrer.debugStackTrace) | ||
.toList(); | ||
} | ||
throw UnsupportedError(''); | ||
} | ||
|
||
/// Increases the reference count of this box because a new object began | ||
/// sharing ownership of the underlying [nativeObject]. | ||
void ref(R debugReferrer) { | ||
assert( | ||
!_ref.isDisposed, | ||
'Cannot increment ref count on a deleted handle.', | ||
); | ||
assert(_refCount > 0); | ||
assert( | ||
debugReferrers.add(debugReferrer), | ||
'Attempted to increment ref count by the same referrer more than once.', | ||
); | ||
_refCount += 1; | ||
assert(refCount == debugReferrers.length); | ||
} | ||
|
||
/// Decrements the reference count for the [nativeObject]. | ||
/// | ||
/// Does nothing if the object has already been deleted. | ||
/// | ||
/// If this causes the reference count to drop to zero, deletes the | ||
/// [nativeObject]. | ||
void unref(R debugReferrer) { | ||
assert( | ||
!_ref.isDisposed, | ||
'Attempted to unref an already deleted native object.', | ||
); | ||
assert( | ||
debugReferrers.remove(debugReferrer), | ||
'Attempted to decrement ref count by the same referrer more than once.', | ||
); | ||
_refCount -= 1; | ||
assert(refCount == debugReferrers.length); | ||
if (_refCount == 0) { | ||
_ref.dispose(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.