Skip to content

Commit

Permalink
Less forking, more allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Mar 27, 2024
1 parent ccf9838 commit 68e6089
Showing 1 changed file with 24 additions and 38 deletions.
62 changes: 24 additions & 38 deletions packages/react-debug-tools/src/ReactDebugHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@ type HookLogEntry = {
value: mixed,
debugInfo: ReactDebugInfo | null,
dispatcherMethodName: string,
/**
* A list of hook function names that may call this dispatcher method.
* If `null`, we assume that the wrapper name is equal to the dispatcher mthod name.
*/
wrapperNames: Array<string> | null,
wrapperNames: Array<string>,
};

let hookLog: Array<HookLogEntry> = [];
Expand Down Expand Up @@ -222,7 +218,7 @@ function use<T>(usable: Usable<T>): T {
debugInfo:
thenable._debugInfo === undefined ? null : thenable._debugInfo,
dispatcherMethodName: 'use',
wrapperNames: null,
wrapperNames: ['use'],
});
return fulfilledValue;
}
Expand All @@ -241,7 +237,7 @@ function use<T>(usable: Usable<T>): T {
debugInfo:
thenable._debugInfo === undefined ? null : thenable._debugInfo,
dispatcherMethodName: 'use',
wrapperNames: null,
wrapperNames: ['use'],
});
throw SuspenseException;
} else if (usable.$$typeof === REACT_CONTEXT_TYPE) {
Expand All @@ -255,7 +251,7 @@ function use<T>(usable: Usable<T>): T {
value,
debugInfo: null,
dispatcherMethodName: 'use',
wrapperNames: null,
wrapperNames: ['use'],
});

return value;
Expand All @@ -275,7 +271,7 @@ function useContext<T>(context: ReactContext<T>): T {
value: value,
debugInfo: null,
dispatcherMethodName: 'useContext',
wrapperNames: null,
wrapperNames: ['useContext'],
});
return value;
}
Expand All @@ -298,7 +294,7 @@ function useState<S>(
value: state,
debugInfo: null,
dispatcherMethodName: 'useState',
wrapperNames: null,
wrapperNames: ['useState'],
});
return [state, (action: BasicStateAction<S>) => {}];
}
Expand All @@ -322,7 +318,7 @@ function useReducer<S, I, A>(
value: state,
debugInfo: null,
dispatcherMethodName: 'useReducer',
wrapperNames: null,
wrapperNames: ['useReducer'],
});
return [state, (action: A) => {}];
}
Expand All @@ -337,7 +333,7 @@ function useRef<T>(initialValue: T): {current: T} {
value: ref.current,
debugInfo: null,
dispatcherMethodName: 'useRef',
wrapperNames: null,
wrapperNames: ['useRef'],
});
return ref;
}
Expand All @@ -351,7 +347,7 @@ function useCacheRefresh(): () => void {
value: hook !== null ? hook.memoizedState : function refresh() {},
debugInfo: null,
dispatcherMethodName: 'useCacheRefresh',
wrapperNames: null,
wrapperNames: ['useCacheRefresh'],
});
return () => {};
}
Expand All @@ -368,7 +364,7 @@ function useLayoutEffect(
value: create,
debugInfo: null,
dispatcherMethodName: 'useLayoutEffect',
wrapperNames: null,
wrapperNames: ['useLayoutEffect'],
});
}

Expand All @@ -384,7 +380,7 @@ function useInsertionEffect(
value: create,
debugInfo: null,
dispatcherMethodName: 'useInsertionEffect',
wrapperNames: null,
wrapperNames: ['useInsertionEffect'],
});
}

Expand All @@ -400,7 +396,7 @@ function useEffect(
value: create,
debugInfo: null,
dispatcherMethodName: 'useEffect',
wrapperNames: null,
wrapperNames: ['useEffect'],
});
}

Expand All @@ -425,7 +421,7 @@ function useImperativeHandle<T>(
value: instance,
debugInfo: null,
dispatcherMethodName: 'useImperativeHandle',
wrapperNames: null,
wrapperNames: ['useImperativeHandle'],
});
}

Expand All @@ -437,7 +433,7 @@ function useDebugValue(value: any, formatterFn: ?(value: any) => any) {
value: typeof formatterFn === 'function' ? formatterFn(value) : value,
debugInfo: null,
dispatcherMethodName: 'useDebugValue',
wrapperNames: null,
wrapperNames: ['useDebugValue'],
});
}

Expand All @@ -450,7 +446,7 @@ function useCallback<T>(callback: T, inputs: Array<mixed> | void | null): T {
value: hook !== null ? hook.memoizedState[0] : callback,
debugInfo: null,
dispatcherMethodName: 'useCallback',
wrapperNames: null,
wrapperNames: ['useCallback'],
});
return callback;
}
Expand All @@ -468,7 +464,7 @@ function useMemo<T>(
value,
debugInfo: null,
dispatcherMethodName: 'useMemo',
wrapperNames: null,
wrapperNames: ['useMemo'],
});
return value;
}
Expand All @@ -491,7 +487,7 @@ function useSyncExternalStore<T>(
value,
debugInfo: null,
dispatcherMethodName: 'useSyncExternalStore',
wrapperNames: null,
wrapperNames: ['useSyncExternalStore'],
});
return value;
}
Expand All @@ -515,7 +511,7 @@ function useTransition(): [
value: isPending,
debugInfo: null,
dispatcherMethodName: 'useTransition',
wrapperNames: null,
wrapperNames: ['useTransition'],
});
return [isPending, () => {}];
}
Expand All @@ -530,7 +526,7 @@ function useDeferredValue<T>(value: T, initialValue?: T): T {
value: prevValue,
debugInfo: null,
dispatcherMethodName: 'useDeferredValue',
wrapperNames: null,
wrapperNames: ['useDeferredValue'],
});
return prevValue;
}
Expand All @@ -545,7 +541,7 @@ function useId(): string {
value: id,
debugInfo: null,
dispatcherMethodName: 'useId',
wrapperNames: null,
wrapperNames: ['useId'],
});
return id;
}
Expand Down Expand Up @@ -597,7 +593,7 @@ function useOptimistic<S, A>(
value: state,
debugInfo: null,
dispatcherMethodName: 'useOptimistic',
wrapperNames: null,
wrapperNames: ['useOptimistic'],
});
return [state, (action: A) => {}];
}
Expand Down Expand Up @@ -658,7 +654,7 @@ function useFormState<S, P>(
value: value,
debugInfo: debugInfo,
dispatcherMethodName: 'useFormState',
wrapperNames: null,
wrapperNames: ['useFormState'],
});

if (error !== null) {
Expand Down Expand Up @@ -922,18 +918,8 @@ function findPrimitiveIndex(hookStack: any, hook: HookLogEntry) {
) {
i++;
}
if (hook.wrapperNames !== null) {
for (let j = 0; j < hook.wrapperNames.length; j++) {
const wrapperName = hook.wrapperNames[j];
if (
i < hookStack.length - 1 &&
isReactWrapper(hookStack[i].functionName, wrapperName)
) {
i++;
}
}
} else {
const wrapperName = hook.dispatcherMethodName;
for (let j = 0; j < hook.wrapperNames.length; j++) {
const wrapperName = hook.wrapperNames[j];
if (
i < hookStack.length - 1 &&
isReactWrapper(hookStack[i].functionName, wrapperName)
Expand Down

0 comments on commit 68e6089

Please sign in to comment.