| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | * |
| 7 | * @flow |
| 8 | */ |
| 9 | |
| 10 | import ProfilerStore from './ProfilerStore'; |
| 11 | import { |
| 12 | getCommitTree, |
| 13 | invalidateCommitTrees, |
| 14 | } from 'react-devtools-shared/src/devtools/views/Profiler/CommitTreeBuilder'; |
| 15 | import { |
| 16 | getChartData as getFlamegraphChartData, |
| 17 | invalidateChartData as invalidateFlamegraphChartData, |
| 18 | } from 'react-devtools-shared/src/devtools/views/Profiler/FlamegraphChartBuilder'; |
| 19 | import { |
| 20 | getChartData as getRankedChartData, |
| 21 | invalidateChartData as invalidateRankedChartData, |
| 22 | } from 'react-devtools-shared/src/devtools/views/Profiler/RankedChartBuilder'; |
| 23 | |
| 24 | import type {CommitTree} from 'react-devtools-shared/src/devtools/views/Profiler/types'; |
| 25 | import type {ChartData as FlamegraphChartData} from 'react-devtools-shared/src/devtools/views/Profiler/FlamegraphChartBuilder'; |
| 26 | import type {ChartData as RankedChartData} from 'react-devtools-shared/src/devtools/views/Profiler/RankedChartBuilder'; |
| 27 | |
| 28 | export default class ProfilingCache { |
| 29 | _fiberCommits: Map<number, Array<number>> = new Map(); |
| 30 | _profilerStore: ProfilerStore; |
| 31 | |
| 32 | constructor(profilerStore: ProfilerStore) { |
| 33 | this._profilerStore = profilerStore; |
| 34 | } |
| 35 | |
| 36 | getCommitTree: ({commitIndex: number, rootID: number}) => CommitTree = ({ |
| 37 | commitIndex, |
| 38 | rootID, |
| 39 | }) => |
| 40 | getCommitTree({ |
| 41 | commitIndex, |
| 42 | profilerStore: this._profilerStore, |
| 43 | rootID, |
| 44 | }); |
| 45 | |
| 46 | getFiberCommits: ({fiberID: number, rootID: number}) => Array<number> = ({ |
| 47 | fiberID, |
| 48 | rootID, |
| 49 | }) => { |
| 50 | const cachedFiberCommits = this._fiberCommits.get(fiberID); |
| 51 | if (cachedFiberCommits != null) { |
| 52 | return cachedFiberCommits; |
| 53 | } |
| 54 | |
| 55 | const fiberCommits = []; |
| 56 | const dataForRoot = this._profilerStore.getDataForRoot(rootID); |
| 57 | dataForRoot.commitData.forEach((commitDatum, commitIndex) => { |
| 58 | if (commitDatum.fiberActualDurations.has(fiberID)) { |
| 59 | fiberCommits.push(commitIndex); |
| 60 | } |
| 61 | }); |
| 62 | |
| 63 | this._fiberCommits.set(fiberID, fiberCommits); |
| 64 | |
| 65 | return fiberCommits; |
| 66 | }; |
| 67 | |
| 68 | getFlamegraphChartData: ({ |
| 69 | commitIndex: number, |
| 70 | commitTree: CommitTree, |
| 71 | rootID: number, |
| 72 | }) => FlamegraphChartData = ({commitIndex, commitTree, rootID}) => |
| 73 | getFlamegraphChartData({ |
| 74 | commitIndex, |
| 75 | commitTree, |
| 76 | profilerStore: this._profilerStore, |
| 77 | rootID, |
| 78 | }); |
| 79 | |
| 80 | getRankedChartData: ({ |
| 81 | commitIndex: number, |
| 82 | commitTree: CommitTree, |
| 83 | rootID: number, |
| 84 | }) => RankedChartData = ({commitIndex, commitTree, rootID}) => |
| 85 | getRankedChartData({ |
| 86 | commitIndex, |
| 87 | commitTree, |
| 88 | profilerStore: this._profilerStore, |
| 89 | rootID, |
| 90 | }); |
| 91 | |
| 92 | invalidate() { |
| 93 | this._fiberCommits.clear(); |
| 94 | |
| 95 | invalidateCommitTrees(); |
| 96 | invalidateFlamegraphChartData(); |
| 97 | invalidateRankedChartData(); |
| 98 | } |
| 99 | } |