Skip to content

Commit 324c04a

Browse files
committed
Less forking, more allocations
1 parent ad2b928 commit 324c04a

File tree

1 file changed

+24
-38
lines changed

1 file changed

+24
-38
lines changed

packages/react-debug-tools/src/ReactDebugHooks.js

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,7 @@ type HookLogEntry = {
4949
value: mixed,
5050
debugInfo: ReactDebugInfo | null,
5151
dispatcherMethodName: string,
52-
/**
53-
* A list of hook function names that may call this dispatcher method.
54-
* If `null`, we assume that the wrapper name is equal to the dispatcher mthod name.
55-
*/
56-
wrapperNames: Array<string> | null,
52+
wrapperNames: Array<string>,
5753
};
5854

5955
let hookLog: Array<HookLogEntry> = [];
@@ -218,7 +214,7 @@ function use<T>(usable: Usable<T>): T {
218214
debugInfo:
219215
thenable._debugInfo === undefined ? null : thenable._debugInfo,
220216
dispatcherMethodName: 'use',
221-
wrapperNames: null,
217+
wrapperNames: ['use'],
222218
});
223219
return fulfilledValue;
224220
}
@@ -237,7 +233,7 @@ function use<T>(usable: Usable<T>): T {
237233
debugInfo:
238234
thenable._debugInfo === undefined ? null : thenable._debugInfo,
239235
dispatcherMethodName: 'use',
240-
wrapperNames: null,
236+
wrapperNames: ['use'],
241237
});
242238
throw SuspenseException;
243239
} else if (usable.$$typeof === REACT_CONTEXT_TYPE) {
@@ -251,7 +247,7 @@ function use<T>(usable: Usable<T>): T {
251247
value,
252248
debugInfo: null,
253249
dispatcherMethodName: 'use',
254-
wrapperNames: null,
250+
wrapperNames: ['use'],
255251
});
256252

257253
return value;
@@ -271,7 +267,7 @@ function useContext<T>(context: ReactContext<T>): T {
271267
value: value,
272268
debugInfo: null,
273269
dispatcherMethodName: 'useContext',
274-
wrapperNames: null,
270+
wrapperNames: ['useContext'],
275271
});
276272
return value;
277273
}
@@ -294,7 +290,7 @@ function useState<S>(
294290
value: state,
295291
debugInfo: null,
296292
dispatcherMethodName: 'useState',
297-
wrapperNames: null,
293+
wrapperNames: ['useState'],
298294
});
299295
return [state, (action: BasicStateAction<S>) => {}];
300296
}
@@ -318,7 +314,7 @@ function useReducer<S, I, A>(
318314
value: state,
319315
debugInfo: null,
320316
dispatcherMethodName: 'useReducer',
321-
wrapperNames: null,
317+
wrapperNames: ['useReducer'],
322318
});
323319
return [state, (action: A) => {}];
324320
}
@@ -333,7 +329,7 @@ function useRef<T>(initialValue: T): {current: T} {
333329
value: ref.current,
334330
debugInfo: null,
335331
dispatcherMethodName: 'useRef',
336-
wrapperNames: null,
332+
wrapperNames: ['useRef'],
337333
});
338334
return ref;
339335
}
@@ -347,7 +343,7 @@ function useCacheRefresh(): () => void {
347343
value: hook !== null ? hook.memoizedState : function refresh() {},
348344
debugInfo: null,
349345
dispatcherMethodName: 'useCacheRefresh',
350-
wrapperNames: null,
346+
wrapperNames: ['useCacheRefresh'],
351347
});
352348
return () => {};
353349
}
@@ -364,7 +360,7 @@ function useLayoutEffect(
364360
value: create,
365361
debugInfo: null,
366362
dispatcherMethodName: 'useLayoutEffect',
367-
wrapperNames: null,
363+
wrapperNames: ['useLayoutEffect'],
368364
});
369365
}
370366

@@ -380,7 +376,7 @@ function useInsertionEffect(
380376
value: create,
381377
debugInfo: null,
382378
dispatcherMethodName: 'useInsertionEffect',
383-
wrapperNames: null,
379+
wrapperNames: ['useInsertionEffect'],
384380
});
385381
}
386382

@@ -396,7 +392,7 @@ function useEffect(
396392
value: create,
397393
debugInfo: null,
398394
dispatcherMethodName: 'useEffect',
399-
wrapperNames: null,
395+
wrapperNames: ['useEffect'],
400396
});
401397
}
402398

@@ -421,7 +417,7 @@ function useImperativeHandle<T>(
421417
value: instance,
422418
debugInfo: null,
423419
dispatcherMethodName: 'useImperativeHandle',
424-
wrapperNames: null,
420+
wrapperNames: ['useImperativeHandle'],
425421
});
426422
}
427423

@@ -433,7 +429,7 @@ function useDebugValue(value: any, formatterFn: ?(value: any) => any) {
433429
value: typeof formatterFn === 'function' ? formatterFn(value) : value,
434430
debugInfo: null,
435431
dispatcherMethodName: 'useDebugValue',
436-
wrapperNames: null,
432+
wrapperNames: ['useDebugValue'],
437433
});
438434
}
439435

@@ -446,7 +442,7 @@ function useCallback<T>(callback: T, inputs: Array<mixed> | void | null): T {
446442
value: hook !== null ? hook.memoizedState[0] : callback,
447443
debugInfo: null,
448444
dispatcherMethodName: 'useCallback',
449-
wrapperNames: null,
445+
wrapperNames: ['useCallback'],
450446
});
451447
return callback;
452448
}
@@ -464,7 +460,7 @@ function useMemo<T>(
464460
value,
465461
debugInfo: null,
466462
dispatcherMethodName: 'useMemo',
467-
wrapperNames: null,
463+
wrapperNames: ['useMemo'],
468464
});
469465
return value;
470466
}
@@ -487,7 +483,7 @@ function useSyncExternalStore<T>(
487483
value,
488484
debugInfo: null,
489485
dispatcherMethodName: 'useSyncExternalStore',
490-
wrapperNames: null,
486+
wrapperNames: ['useSyncExternalStore'],
491487
});
492488
return value;
493489
}
@@ -511,7 +507,7 @@ function useTransition(): [
511507
value: isPending,
512508
debugInfo: null,
513509
dispatcherMethodName: 'useTransition',
514-
wrapperNames: null,
510+
wrapperNames: ['useTransition'],
515511
});
516512
return [isPending, () => {}];
517513
}
@@ -526,7 +522,7 @@ function useDeferredValue<T>(value: T, initialValue?: T): T {
526522
value: prevValue,
527523
debugInfo: null,
528524
dispatcherMethodName: 'useDeferredValue',
529-
wrapperNames: null,
525+
wrapperNames: ['useDeferredValue'],
530526
});
531527
return prevValue;
532528
}
@@ -541,7 +537,7 @@ function useId(): string {
541537
value: id,
542538
debugInfo: null,
543539
dispatcherMethodName: 'useId',
544-
wrapperNames: null,
540+
wrapperNames: ['useId'],
545541
});
546542
return id;
547543
}
@@ -593,7 +589,7 @@ function useOptimistic<S, A>(
593589
value: state,
594590
debugInfo: null,
595591
dispatcherMethodName: 'useOptimistic',
596-
wrapperNames: null,
592+
wrapperNames: ['useOptimistic'],
597593
});
598594
return [state, (action: A) => {}];
599595
}
@@ -654,7 +650,7 @@ function useFormState<S, P>(
654650
value: value,
655651
debugInfo: debugInfo,
656652
dispatcherMethodName: 'useFormState',
657-
wrapperNames: null,
653+
wrapperNames: ['useFormState'],
658654
});
659655

660656
if (error !== null) {
@@ -847,18 +843,8 @@ function findPrimitiveIndex(hookStack: any, hook: HookLogEntry) {
847843
) {
848844
i++;
849845
}
850-
if (hook.wrapperNames !== null) {
851-
for (let j = 0; j < hook.wrapperNames.length; j++) {
852-
const wrapperName = hook.wrapperNames[j];
853-
if (
854-
i < hookStack.length - 1 &&
855-
isReactWrapper(hookStack[i].functionName, wrapperName)
856-
) {
857-
i++;
858-
}
859-
}
860-
} else {
861-
const wrapperName = hook.dispatcherMethodName;
846+
for (let j = 0; j < hook.wrapperNames.length; j++) {
847+
const wrapperName = hook.wrapperNames[j];
862848
if (
863849
i < hookStack.length - 1 &&
864850
isReactWrapper(hookStack[i].functionName, wrapperName)

0 commit comments

Comments
 (0)