Skip to content

Commit

Permalink
Migrate memory_graph_model.dart to null safety. (#3820)
Browse files Browse the repository at this point in the history
  • Loading branch information
polina-c authored Mar 10, 2022
1 parent e1d45e7 commit 223b649
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1108,7 +1108,7 @@ class MemoryController extends DisposableController
for (final liveExternal
in heapGraph?.externals ?? <HeapGraphExternalLive>[]) {
final HeapGraphClassLive? classLive =
liveExternal.live?.theClass as HeapGraphClassLive?;
liveExternal.live.theClass as HeapGraphClassLive?;

ExternalReference? externalReference;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// @dart=2.9

import 'package:vm_service/vm_service.dart';

// import '../config_specific/logger/logger.dart';
Expand Down Expand Up @@ -92,7 +90,7 @@ bool isBuiltInList(HeapGraphClassLive live) =>
final Map<int, String> _monitorClasses = {};

/// Ensure the classId is zero based ()
bool monitorClass({int classId, String className, String message = ''}) {
bool monitorClass({int? classId, String? className, String message = ''}) {
if (classId != null) {
if (_monitorClasses.containsKey(classId)) {
final className = _monitorClasses[classId];
Expand All @@ -113,7 +111,7 @@ bool monitorClass({int classId, String className, String message = ''}) {
HeapGraph convertHeapGraph(
MemoryController controller,
HeapSnapshotGraph graph, [
List<String> classNamesToMonitor,
List<String>? classNamesToMonitor,
]) {
final Map<LibraryClass, int> builtInClasses = {};

Expand All @@ -123,8 +121,7 @@ HeapGraph convertHeapGraph(
}

// Construct all the classes in the snapshot.
final List<HeapGraphClassLive> classes =
List<HeapGraphClassLive>.filled(graph.classes.length, null);
final classes = List<HeapGraphClassLive?>.filled(graph.classes.length, null);
for (int i = 0; i < graph.classes.length; i++) {
final HeapSnapshotClass c = graph.classes[i];

Expand All @@ -151,8 +148,8 @@ HeapGraph convertHeapGraph(
}

// Pre-allocate the number of objects in the snapshot.
final List<HeapGraphElementLive> elements =
List<HeapGraphElementLive>.filled(graph.objects.length, null);
final elements =
List<HeapGraphElementLive?>.filled(graph.objects.length, null);

// Construct all objects.
for (int i = 0; i < graph.objects.length; i++) {
Expand All @@ -163,7 +160,7 @@ HeapGraph convertHeapGraph(
// Associate each object with a Class.
for (int i = 0; i < graph.objects.length; i++) {
final HeapSnapshotObject o = graph.objects[i];
final HeapGraphElementLive converted = elements[i];
final HeapGraphElementLive converted = elements[i]!;

// New style snapshot class index is zero based.
if (o.classId == 0) {
Expand All @@ -185,9 +182,9 @@ HeapGraph convertHeapGraph(
if (refId == 0) {
ref = HeapGraph.elementSentinel;
} else {
ref = elements[controller.newSnapshotSemantics ? refId : refId - 1];
ref = elements[controller.newSnapshotSemantics ? refId : refId - 1]!;
}
converted.references.add(ref);
converted.references!.add(ref);
}
};
}
Expand All @@ -196,21 +193,21 @@ HeapGraph convertHeapGraph(

// Pre-allocate the number of external objects in the snapshot.
final externals =
List<HeapGraphExternalLive>.filled(snapshotExternals.length, null);
List<HeapGraphExternalLive?>.filled(snapshotExternals.length, null);

// Construct all external objects and link to its live element.
for (int index = 0; index < snapshotExternals.length; index++) {
final snapshotObject = snapshotExternals[index];
final liveElement = elements[snapshotObject.object];
final liveElement = elements[snapshotObject.object]!;
externals[index] = HeapGraphExternalLive(snapshotObject, liveElement);
}

return HeapGraph(
controller,
builtInClasses,
classes,
elements,
externals,
classes as List<HeapGraphClassLive>,
elements as List<HeapGraphElementLive>,
externals as List<HeapGraphExternalLive>,
);
}

Expand Down Expand Up @@ -258,7 +255,7 @@ class HeapGraph {

/// Group all instances by class (key is class name, value are class
/// instances). This is the entire set of objects (no filter applied).
final Map<String, Set<HeapGraphElementLive>> rawGroupByClass = {};
final Map<String, Set<HeapGraphElementLive?>> rawGroupByClass = {};

/// Group all instances by class (key is class name, value are class
/// instances). Filtering out objects that match a given filter. This
Expand Down Expand Up @@ -308,16 +305,16 @@ class HeapGraph {
sb.write(libraryKey);
final librarySbToString = sb.toString();
rawGroupByLibrary[librarySbToString] ??= <HeapGraphClassLive>{};
rawGroupByLibrary[librarySbToString].add(c);
rawGroupByLibrary[librarySbToString]!.add(c);
sb.clear();

// Collect instances for each class (group by class)
for (final instance in c.getInstances(this)) {
sb.write(c.name);
c.instancesTotalShallowSizes += instance.origin.shallowSize;
final classSbToString = sb.toString();
rawGroupByClass[classSbToString] ??= <HeapGraphElementLive>{};
rawGroupByClass[classSbToString].add(instance);
rawGroupByClass[classSbToString] ??= <HeapGraphElementLive?>{};
rawGroupByClass[classSbToString]!.add(instance);
sb.clear();
}
}
Expand Down Expand Up @@ -378,7 +375,7 @@ class HeapGraph {
void computeInstancesForClasses() {
if (!instancesComputed) {
for (final instance in elements) {
instance.theClass.addInstance(instance);
instance.theClass!.addInstance(instance);
}

_instancesComputed = true;
Expand All @@ -388,14 +385,14 @@ class HeapGraph {

abstract class HeapGraphElement {
/// Outbound references, i.e. this element points to elements in this list.
List<HeapGraphElement> _references;
List<HeapGraphElement>? _references;

void Function() referencesFiller;
void Function()? referencesFiller;

List<HeapGraphElement> get references {
List<HeapGraphElement>? get references {
if (_references == null && referencesFiller != null) {
_references = [];
referencesFiller();
referencesFiller!();
}
return _references;
}
Expand All @@ -417,17 +414,17 @@ class HeapGraphElementLive extends HeapGraphElement {
HeapGraphElementLive(this.origin);

final HeapSnapshotObject origin;
HeapGraphClass theClass;
HeapGraphClass? theClass;

@override
bool get isSentinel => false;

HeapGraphElement getField(String name) {
HeapGraphElement? getField(String name) {
if (theClass is HeapGraphClassLive) {
final HeapGraphClassLive c = theClass;
final HeapGraphClassLive c = theClass as HeapGraphClassLive;
for (HeapSnapshotField field in c.origin.fields) {
if (field.name == name) {
return references[field.index];
return references![field.index];
}
}
}
Expand All @@ -437,10 +434,10 @@ class HeapGraphElementLive extends HeapGraphElement {
List<MapEntry<String, HeapGraphElement>> getFields() {
final List<MapEntry<String, HeapGraphElement>> result = [];
if (theClass is HeapGraphClassLive) {
final HeapGraphClassLive c = theClass;
final HeapGraphClassLive c = theClass as HeapGraphClassLive;
for (final field in c.origin.fields) {
if (field.index < references.length) {
result.add(MapEntry(field.name, references[field.index]));
if (field.index < references!.length) {
result.add(MapEntry(field.name, references![field.index]));
} else {
// TODO(polinach): figure out what to do if index is out of range.
// See https://github.com/flutter/devtools/issues/3629 for details.
Expand Down Expand Up @@ -500,6 +497,9 @@ abstract class HeapGraphClass {
}

List<HeapGraphElementLive> getInstances(HeapGraph graph) {
// TODO(polinach): this code never executes and should be reviewed and
// fixed.
// ignore: unnecessary_null_comparison
if (_instances == null) {
for (var i = 0; i < graph.elements.length; i++) {
final HeapGraphElementLive converted = graph.elements[i];
Expand All @@ -513,7 +513,10 @@ abstract class HeapGraphClass {

/// Quick short-circuit to return real size of null implies '--' yet to be
/// computed or N/A.
int get instancesCount => _instances == null ? null : _instances.length;
// TODO(polinach): _instances is never null. So, this code
// should be reviewed and fixed.
// ignore: unnecessary_null_comparison
int? get instancesCount => _instances == null ? null : _instances.length;
}

class HeapGraphClassSentinel extends HeapGraphClass {
Expand All @@ -522,13 +525,7 @@ class HeapGraphClassSentinel extends HeapGraphClass {
}

class HeapGraphClassLive extends HeapGraphClass {
HeapGraphClassLive(this.origin) {
_check();
}

void _check() {
assert(origin != null);
}
HeapGraphClassLive(this.origin);

final HeapSnapshotClass origin;

Expand Down

0 comments on commit 223b649

Please sign in to comment.