Skip to content

Commit

Permalink
Performance improvements for the CPU profiler (#3274)
Browse files Browse the repository at this point in the history
* Performance improvements for the CPU profiler

* review comments
  • Loading branch information
kenzieschmoll authored Aug 13, 2021
1 parent aede736 commit 3e8dbe3
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,17 @@ class CpuProfilerController
List<ToggleFilter<CpuStackFrame>> get toggleFilters => _toggleFilters ??= [
ToggleFilter<CpuStackFrame>(
name: 'Hide Native code',
includeCallback: (stackFrame) => stackFrame.processedUrl.isNotEmpty,
includeCallback: (stackFrame) => !stackFrame.isNative,
enabledByDefault: true,
),
ToggleFilter<CpuStackFrame>(
name: 'Hide core Dart libraries',
includeCallback: (stackFrame) =>
!stackFrame.processedUrl.startsWith('dart:'),
includeCallback: (stackFrame) => !stackFrame.isDartCore,
),
if (serviceManager.connectedApp?.isFlutterAppNow ?? true)
ToggleFilter<CpuStackFrame>(
name: 'Hide core Flutter libraries',
includeCallback: (stackFrame) =>
!(stackFrame.processedUrl.startsWith('package:flutter/') ||
stackFrame.name.startsWith('flutter::') ||
stackFrame.processedUrl.startsWith('dart:ui')),
includeCallback: (stackFrame) => !stackFrame.isFlutterCore,
),
];

Expand Down
63 changes: 58 additions & 5 deletions packages/devtools_app/lib/src/profiler/cpu_profile_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ class CpuProfileData {
@required this.cpuSamples,
@required this.profileMetaData,
}) {
_cpuProfileRoot = CpuStackFrame(
_cpuProfileRoot = CpuStackFrame._(
id: rootId,
name: rootName,
verboseName: rootName,
category: 'Dart',
rawUrl: '',
processedUrl: '',
profileMetaData: profileMetaData,
parentId: null,
);
Expand Down Expand Up @@ -407,16 +408,50 @@ class CpuStackFrame extends TreeNode<CpuStackFrame>
DataSearchStateMixin,
TreeDataSearchStateMixin<CpuStackFrame>,
FlameChartDataMixin {
CpuStackFrame({
factory CpuStackFrame({
@required String id,
@required String name,
@required String verboseName,
@required String category,
@required String rawUrl,
@required String parentId,
@required CpuProfileMetaData profileMetaData,
}) {
assert(rawUrl != null);
return CpuStackFrame._(
id: id,
name: name,
verboseName: verboseName,
category: category,
rawUrl: rawUrl,
processedUrl: getSimplePackageUrl(rawUrl),
parentId: parentId,
profileMetaData: profileMetaData,
);
}

CpuStackFrame._({
@required this.id,
@required this.name,
@required this.verboseName,
@required this.category,
@required this.rawUrl,
@required this.processedUrl,
@required this.parentId,
@required this.profileMetaData,
}) : processedUrl = getSimplePackageUrl(rawUrl),
assert(rawUrl != null);
});

/// Prefix for packages from the core Dart libraries.
static const dartPackagePrefix = 'dart:';

/// Prefix for packages from the core Flutter libraries.
static const flutterPackagePrefix = 'package:flutter/';

/// The Flutter namespace in C++ that is part of the Flutter Engine code.
static const flutterEnginePrefix = 'flutter::';

/// dart:ui is the library for the Dart part of the Flutter Engine code.
static const dartUiPrefix = 'dart:ui';

final String id;

Expand All @@ -434,6 +469,23 @@ class CpuStackFrame extends TreeNode<CpuStackFrame>

final CpuProfileMetaData profileMetaData;

bool get isNative =>
_isNative ??= id != CpuProfileData.rootId && processedUrl.isEmpty;

bool _isNative;

bool get isDartCore =>
_isDartCore ??= processedUrl.startsWith(dartPackagePrefix);

bool _isDartCore;

bool get isFlutterCore =>
_isFlutterCore ??= processedUrl.startsWith(flutterPackagePrefix) ||
name.startsWith(flutterEnginePrefix) ||
processedUrl.startsWith(dartUiPrefix);

bool _isFlutterCore;

Iterable<String> get userTags => _userTagSampleCount.keys;

/// Maps a user tag to the number of CPU samples associated with it.
Expand Down Expand Up @@ -517,12 +569,13 @@ class CpuStackFrame extends TreeNode<CpuStackFrame>
bool copySampleCountsAndTags = true,
bool resetInclusiveSampleCount = true,
}) {
final copy = CpuStackFrame(
final copy = CpuStackFrame._(
id: id ?? this.id,
name: name ?? this.name,
verboseName: verboseName ?? this.verboseName,
category: category ?? this.category,
rawUrl: url ?? rawUrl,
processedUrl: url != null ? getSimplePackageUrl(url) : processedUrl,
parentId: parentId ?? this.parentId,
profileMetaData: profileMetaData ?? this.profileMetaData,
);
Expand Down
30 changes: 30 additions & 0 deletions packages/devtools_app/test/cpu_profile_model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,36 @@ void main() {
});

group('CpuStackFrame', () {
test('isNative', () {
expect(stackFrameA.isNative, isTrue);
expect(stackFrameB.isNative, isFalse);
expect(stackFrameC.isNative, isFalse);
expect(
CpuStackFrame(
id: CpuProfileData.rootId,
name: CpuProfileData.rootName,
verboseName: 'all',
category: 'Dart',
rawUrl: '',
parentId: null,
profileMetaData: profileMetaData,
).isNative,
isFalse,
);
});

test('isDartCore', () {
expect(stackFrameA.isDartCore, isFalse);
expect(stackFrameB.isDartCore, isTrue);
expect(stackFrameC.isDartCore, isFalse);
});

test('isFlutterCore', () {
expect(stackFrameA.isFlutterCore, isFalse);
expect(stackFrameB.isFlutterCore, isFalse);
expect(stackFrameC.isFlutterCore, isTrue);
});

test('sampleCount', () {
expect(testStackFrame.inclusiveSampleCount, equals(10));
});
Expand Down

0 comments on commit 3e8dbe3

Please sign in to comment.