Skip to content

Commit 0ba0564

Browse files
author
Juan
authored
[DevTools] Add utils for perfomance marks (#22180)
1 parent 2bf2e76 commit 0ba0564

File tree

3 files changed

+240
-292
lines changed

3 files changed

+240
-292
lines changed

packages/react-devtools-extensions/src/astUtils.js

Lines changed: 28 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @flow
88
*/
99

10-
import {__PERFORMANCE_PROFILE__} from 'react-devtools-shared/src/constants';
10+
import {withSyncPerformanceMark} from 'react-devtools-shared/src/PerformanceMarks';
1111
import traverse, {NodePath, Node} from '@babel/traverse';
1212
import {File} from '@babel/types';
1313

@@ -28,15 +28,6 @@ export type SourceFileASTWithHookDetails = {
2828

2929
export const NO_HOOK_NAME = '<no-hook>';
3030

31-
function mark(markName: string): void {
32-
performance.mark(markName + '-start');
33-
}
34-
35-
function measure(markName: string): void {
36-
performance.mark(markName + '-end');
37-
performance.measure(markName, markName + '-start', markName + '-end');
38-
}
39-
4031
const AST_NODE_TYPES = Object.freeze({
4132
PROGRAM: 'Program',
4233
CALL_EXPRESSION: 'CallExpression',
@@ -141,13 +132,10 @@ export function getHookName(
141132
originalSourceLineNumber: number,
142133
originalSourceColumnNumber: number,
143134
): string | null {
144-
if (__PERFORMANCE_PROFILE__) {
145-
mark('getPotentialHookDeclarationsFromAST(originalSourceAST)');
146-
}
147-
const hooksFromAST = getPotentialHookDeclarationsFromAST(originalSourceAST);
148-
if (__PERFORMANCE_PROFILE__) {
149-
measure('getPotentialHookDeclarationsFromAST(originalSourceAST)');
150-
}
135+
const hooksFromAST = withSyncPerformanceMark(
136+
'getPotentialHookDeclarationsFromAST(originalSourceAST)',
137+
() => getPotentialHookDeclarationsFromAST(originalSourceAST),
138+
);
151139

152140
let potentialReactHookASTNode = null;
153141
if (originalSourceColumnNumber === 0) {
@@ -188,29 +176,23 @@ export function getHookName(
188176
// nodesAssociatedWithReactHookASTNode could directly be used to obtain the hook variable name
189177
// depending on the type of potentialReactHookASTNode
190178
try {
191-
if (__PERFORMANCE_PROFILE__) {
192-
mark('getFilteredHookASTNodes()');
193-
}
194-
const nodesAssociatedWithReactHookASTNode = getFilteredHookASTNodes(
195-
potentialReactHookASTNode,
196-
hooksFromAST,
197-
originalSourceCode,
179+
const nodesAssociatedWithReactHookASTNode = withSyncPerformanceMark(
180+
'getFilteredHookASTNodes()',
181+
() =>
182+
getFilteredHookASTNodes(
183+
potentialReactHookASTNode,
184+
hooksFromAST,
185+
originalSourceCode,
186+
),
198187
);
199-
if (__PERFORMANCE_PROFILE__) {
200-
measure('getFilteredHookASTNodes()');
201-
}
202188

203-
if (__PERFORMANCE_PROFILE__) {
204-
mark('getHookNameFromNode()');
205-
}
206-
const name = getHookNameFromNode(
207-
hook,
208-
nodesAssociatedWithReactHookASTNode,
209-
potentialReactHookASTNode,
189+
const name = withSyncPerformanceMark('getHookNameFromNode()', () =>
190+
getHookNameFromNode(
191+
hook,
192+
nodesAssociatedWithReactHookASTNode,
193+
potentialReactHookASTNode,
194+
),
210195
);
211-
if (__PERFORMANCE_PROFILE__) {
212-
measure('getHookNameFromNode()');
213-
}
214196

215197
return name;
216198
} catch (error) {
@@ -315,19 +297,15 @@ function getHookVariableName(
315297

316298
function getPotentialHookDeclarationsFromAST(sourceAST: File): NodePath[] {
317299
const potentialHooksFound: NodePath[] = [];
318-
if (__PERFORMANCE_PROFILE__) {
319-
mark('traverse(sourceAST)');
320-
}
321-
traverse(sourceAST, {
322-
enter(path) {
323-
if (path.isVariableDeclarator() && isPotentialHookDeclaration(path)) {
324-
potentialHooksFound.push(path);
325-
}
326-
},
327-
});
328-
if (__PERFORMANCE_PROFILE__) {
329-
measure('traverse(sourceAST)');
330-
}
300+
withSyncPerformanceMark('traverse(sourceAST)', () =>
301+
traverse(sourceAST, {
302+
enter(path) {
303+
if (path.isVariableDeclarator() && isPotentialHookDeclaration(path)) {
304+
potentialHooksFound.push(path);
305+
}
306+
},
307+
}),
308+
);
331309
return potentialHooksFound;
332310
}
333311

0 commit comments

Comments
 (0)