Skip to content

Commit e032fc2

Browse files
Nancy LiDevtools-frontend LUCI CQ
authored andcommitted
[RPP code health] Clean code of main data provider
- Move some variable initialization from constructor to declaration - this will happen before the `reset()` function in the constructor - in `timelineData()` function reorder the check for rebuild flag, also added comments - Restrict `setModel` to not accept null parsedTrace - all the use case will pass a valid trace now, this can avoid extra check - Remove some unused group style, and rename the remaining two. - Rename a test-only function. Bug: none Change-Id: I1c9ec702c1fc53a19fbe79259971c058144a7a9a Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6105491 Reviewed-by: Andres Olivares <[email protected]> Commit-Queue: Nancy Li <[email protected]>
1 parent 9c67e20 commit e032fc2

File tree

4 files changed

+44
-65
lines changed

4 files changed

+44
-65
lines changed

front_end/panels/timeline/TimelineFlameChartDataProvider.ts

Lines changed: 38 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -108,24 +108,17 @@ export class TimelineFlameChartDataProvider extends Common.ObjectWrapper.ObjectW
108108
PerfUI.FlameChart.FlameChartDataProvider {
109109
private droppedFramePatternCanvas: HTMLCanvasElement;
110110
private partialFramePatternCanvas: HTMLCanvasElement;
111-
private timelineDataInternal: PerfUI.FlameChart.FlameChartTimelineData|null;
112-
private currentLevel: number;
113-
114-
// The Performance and the Timeline models are expected to be
115-
// deprecated in favor of using parsedTrace (new RPP engine) only
116-
// as part of the work in crbug.com/1386091. For this reason they
117-
// have the "legacy" prefix on their name.
118-
private compatibilityTracksAppender: CompatibilityTracksAppender|null;
119-
private parsedTrace: Trace.Handlers.Types.ParsedTrace|null;
111+
private timelineDataInternal: PerfUI.FlameChart.FlameChartTimelineData|null = null;
112+
private currentLevel = 0;
113+
114+
private compatibilityTracksAppender: CompatibilityTracksAppender|null = null;
115+
private parsedTrace: Trace.Handlers.Types.ParsedTrace|null = null;
120116
private isCpuProfile = false;
121117

122-
private minimumBoundaryInternal: number;
123-
private timeSpan: number;
124-
private readonly headerLevel1: PerfUI.FlameChart.GroupStyle;
125-
private readonly headerLevel2: PerfUI.FlameChart.GroupStyle;
126-
private readonly staticHeader: PerfUI.FlameChart.GroupStyle;
127-
private framesHeader: PerfUI.FlameChart.GroupStyle;
128-
private readonly screenshotsHeader: PerfUI.FlameChart.GroupStyle;
118+
#minimumBoundary: number = 0;
119+
private timeSpan: number = 0;
120+
private readonly framesGroupStyle: PerfUI.FlameChart.GroupStyle;
121+
private readonly screenshotsGroupStyle: PerfUI.FlameChart.GroupStyle;
129122

130123
// Contains all the entries that are DRAWN onto the track. Entries that have
131124
// been hidden - either by a user action, or because they aren't visible at
@@ -138,44 +131,31 @@ export class TimelineFlameChartDataProvider extends Common.ObjectWrapper.ObjectW
138131
// Trace.Types.Events.Event directly. See crrev.com/c/5973695 for details.
139132
private entryData: Trace.Types.Events.Event[] = [];
140133

141-
private entryTypeByLevel!: EntryType[];
142-
private entryIndexToTitle!: string[];
143-
private lastInitiatorEntry!: number;
134+
private entryTypeByLevel: EntryType[] = [];
135+
private entryIndexToTitle: string[] = [];
136+
private lastInitiatorEntry: number = -1;
144137
private lastInitiatorsData: PerfUI.FlameChart.FlameChartInitiatorData[] = [];
145-
private lastSelection?: Selection;
146-
#font: string;
138+
139+
private lastSelection: Selection|null = null;
140+
readonly #font = `${PerfUI.Font.DEFAULT_FONT_SIZE} ${PerfUI.Font.getFontFamilyForCanvas()}`;
147141
#eventIndexByEvent: WeakMap<Trace.Types.Events.Event, number|null> = new WeakMap();
148142

149143
constructor() {
150144
super();
151145
this.reset();
152-
// This section is in fact covered by `this.reset()` but we still need them in the constructor for compile reason.
153-
this.timelineDataInternal = null;
154-
this.currentLevel = 0;
155-
this.compatibilityTracksAppender = null;
156-
this.parsedTrace = null;
157-
this.minimumBoundaryInternal = 0;
158-
this.timeSpan = 0;
159146

160-
this.#font = `${PerfUI.Font.DEFAULT_FONT_SIZE} ${PerfUI.Font.getFontFamilyForCanvas()}`;
161147
this.droppedFramePatternCanvas = document.createElement('canvas');
162148
this.partialFramePatternCanvas = document.createElement('canvas');
163149
this.preparePatternCanvas();
164150

165-
this.headerLevel1 = this.buildGroupStyle({shareHeaderLine: false});
166-
this.headerLevel2 = this.buildGroupStyle({padding: 2, nestingLevel: 1, collapsible: false});
167-
this.staticHeader = this.buildGroupStyle({collapsible: false});
168-
this.framesHeader = this.buildGroupStyle({useFirstLineForOverview: true});
169-
this.screenshotsHeader =
151+
this.framesGroupStyle = this.buildGroupStyle({useFirstLineForOverview: true});
152+
this.screenshotsGroupStyle =
170153
this.buildGroupStyle({useFirstLineForOverview: true, nestingLevel: 1, collapsible: false, itemsHeight: 150});
171154

172155
ThemeSupport.ThemeSupport.instance().addEventListener(ThemeSupport.ThemeChangeEvent.eventName, () => {
173156
const headers = [
174-
this.headerLevel1,
175-
this.headerLevel2,
176-
this.staticHeader,
177-
this.framesHeader,
178-
this.screenshotsHeader,
157+
this.framesGroupStyle,
158+
this.screenshotsGroupStyle,
179159
];
180160
for (const header of headers) {
181161
header.color = ThemeSupport.ThemeSupport.instance().getComputedValue('--sys-color-on-surface');
@@ -402,17 +382,15 @@ export class TimelineFlameChartDataProvider extends Common.ObjectWrapper.ObjectW
402382
return Object.assign(defaultGroupStyle, extra);
403383
}
404384

405-
setModel(parsedTrace: Trace.Handlers.Types.ParsedTrace|null, isCpuProfile = false): void {
385+
setModel(parsedTrace: Trace.Handlers.Types.ParsedTrace, isCpuProfile = false): void {
406386
this.reset();
407387
this.parsedTrace = parsedTrace;
408388
this.isCpuProfile = isCpuProfile;
409-
if (parsedTrace) {
410-
const {traceBounds} = parsedTrace.Meta;
411-
const minTime = Trace.Helpers.Timing.microSecondsToMilliseconds(traceBounds.min);
412-
const maxTime = Trace.Helpers.Timing.microSecondsToMilliseconds(traceBounds.max);
413-
this.minimumBoundaryInternal = minTime;
414-
this.timeSpan = minTime === maxTime ? 1000 : maxTime - this.minimumBoundaryInternal;
415-
}
389+
const {traceBounds} = parsedTrace.Meta;
390+
const minTime = Trace.Helpers.Timing.microSecondsToMilliseconds(traceBounds.min);
391+
const maxTime = Trace.Helpers.Timing.microSecondsToMilliseconds(traceBounds.max);
392+
this.#minimumBoundary = minTime;
393+
this.timeSpan = minTime === maxTime ? 1000 : maxTime - this.#minimumBoundary;
416394
}
417395

418396
/**
@@ -450,7 +428,8 @@ export class TimelineFlameChartDataProvider extends Common.ObjectWrapper.ObjectW
450428
/**
451429
* Builds the flame chart data using the track appenders
452430
*/
453-
buildFromTrackAppenders(options?: {filterThreadsByName?: string, expandedTracks?: Set<TrackAppenderName>}): void {
431+
buildFromTrackAppendersForTest(options?: {filterThreadsByName?: string, expandedTracks?: Set<TrackAppenderName>}):
432+
void {
454433
if (!this.compatibilityTracksAppender) {
455434
return;
456435
}
@@ -534,7 +513,7 @@ export class TimelineFlameChartDataProvider extends Common.ObjectWrapper.ObjectW
534513
this.entryTypeByLevel = [];
535514
this.entryIndexToTitle = [];
536515
this.#eventIndexByEvent = new Map();
537-
this.minimumBoundaryInternal = 0;
516+
this.#minimumBoundary = 0;
538517
this.timeSpan = 0;
539518

540519
this.compatibilityTracksAppender?.reset();
@@ -549,19 +528,20 @@ export class TimelineFlameChartDataProvider extends Common.ObjectWrapper.ObjectW
549528

550529
/**
551530
* Builds the flame chart data using the tracks appender (which use
552-
* the new trace engine) and the legacy code paths present in this
553-
* file. The result built data is cached and returned.
531+
* the new trace engine). The result built data is cached and returned.
554532
*/
555533
timelineData(rebuild: boolean = false): PerfUI.FlameChart.FlameChartTimelineData {
556-
if (this.timelineDataInternal && this.timelineDataInternal.entryLevels.length !== 0 && !rebuild) {
557-
// The flame chart data is built already, so return the cached
558-
// data.
534+
if (!rebuild && this.timelineDataInternal && this.timelineDataInternal.entryLevels.length !== 0) {
535+
// If the flame chart data is built already and we don't want to rebuild, we can return the cached data.
536+
// |entryLevels.length| is used to check if the cached data is not empty (correctly built),
559537
return this.timelineDataInternal;
560538
}
561539

562540
this.timelineDataInternal = PerfUI.FlameChart.FlameChartTimelineData.createEmpty();
563541

564542
if (rebuild) {
543+
// This function will interact with the |compatibilityTracksAppender|, which needs the reference of
544+
// |timelineDataInternal|, so make sure this is called after the correct |timelineDataInternal|.
565545
this.clearTimelineDataCache();
566546
}
567547

@@ -664,7 +644,7 @@ export class TimelineFlameChartDataProvider extends Common.ObjectWrapper.ObjectW
664644
}
665645

666646
minimumBoundary(): number {
667-
return this.minimumBoundaryInternal;
647+
return this.#minimumBoundary;
668648
}
669649

670650
totalTime(): number {
@@ -718,10 +698,10 @@ export class TimelineFlameChartDataProvider extends Common.ObjectWrapper.ObjectW
718698
const filmStrip = Trace.Extras.FilmStrip.fromParsedTrace(this.parsedTrace);
719699
const hasScreenshots = filmStrip.frames.length > 0;
720700

721-
this.framesHeader.collapsible = hasScreenshots;
701+
this.framesGroupStyle.collapsible = hasScreenshots;
722702
const expanded = Root.Runtime.Runtime.queryParam('flamechart-force-expand') === 'frames';
723703

724-
this.appendHeader(i18nString(UIStrings.frames), this.framesHeader, false /* selectable */, expanded);
704+
this.appendHeader(i18nString(UIStrings.frames), this.framesGroupStyle, false /* selectable */, expanded);
725705

726706
this.entryTypeByLevel[this.currentLevel] = EntryType.FRAME;
727707
for (const frame of this.parsedTrace.Frames.frames) {
@@ -739,7 +719,7 @@ export class TimelineFlameChartDataProvider extends Common.ObjectWrapper.ObjectW
739719
if (!this.timelineDataInternal || !this.parsedTrace) {
740720
return;
741721
}
742-
this.appendHeader('', this.screenshotsHeader, false /* selectable */);
722+
this.appendHeader('', this.screenshotsGroupStyle, false /* selectable */);
743723
this.entryTypeByLevel[this.currentLevel] = EntryType.SCREENSHOT;
744724
let prevTimestamp: Trace.Types.Timing.MilliSeconds|undefined = undefined;
745725

front_end/panels/timeline/TimelineFlameChartNetworkDataProvider.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,12 @@ export class TimelineFlameChartNetworkDataProvider implements PerfUI.FlameChart.
5959
this.#networkTrackAppender = null;
6060
}
6161

62-
setModel(parsedTrace: Trace.Handlers.Types.ParsedTrace|null): void {
62+
setModel(parsedTrace: Trace.Handlers.Types.ParsedTrace): void {
6363
this.reset();
6464
this.#parsedTrace = parsedTrace;
65-
if (this.#parsedTrace) {
66-
this.setEvents(this.#parsedTrace);
67-
this.#setTimingBoundsData(this.#parsedTrace);
68-
}
65+
66+
this.setEvents(this.#parsedTrace);
67+
this.#setTimingBoundsData(this.#parsedTrace);
6968
}
7069

7170
setEvents(parsedTrace: Trace.Handlers.Types.ParsedTrace): void {

front_end/panels/timeline/TimelineFlameChartView.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -988,7 +988,7 @@ export class TimelineFlameChartView extends
988988
this.#updateDetailViews();
989989
}
990990

991-
setModel(newParsedTrace: Trace.Handlers.Types.ParsedTrace|null, isCpuProfile = false): void {
991+
setModel(newParsedTrace: Trace.Handlers.Types.ParsedTrace, isCpuProfile = false): void {
992992
if (newParsedTrace === this.#parsedTrace) {
993993
return;
994994
}

front_end/testing/TraceHelpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export async function getMainFlameChartWithTracks(
5656
dataProvider.setModel(parsedTrace);
5757
const tracksAppender = dataProvider.compatibilityTracksAppenderInstance();
5858
tracksAppender.setVisibleTracks(trackAppenderNames);
59-
dataProvider.buildFromTrackAppenders(
59+
dataProvider.buildFromTrackAppendersForTest(
6060
{filterThreadsByName: trackName, expandedTracks: expanded ? trackAppenderNames : undefined});
6161
const delegate = new MockFlameChartDelegate();
6262
const flameChart = new PerfUI.FlameChart.FlameChart(dataProvider, delegate);

0 commit comments

Comments
 (0)