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 20, 2024
1 parent 22079a5 commit 127b116
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 @@ -218,7 +214,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 @@ -237,7 +233,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 @@ -251,7 +247,7 @@ function use<T>(usable: Usable<T>): T {
value,
debugInfo: null,
dispatcherMethodName: 'use',
wrapperNames: null,
wrapperNames: ['use'],
});

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

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

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

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

Expand All @@ -433,7 +429,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 @@ -446,7 +442,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 @@ -464,7 +460,7 @@ function useMemo<T>(
value,
debugInfo: null,
dispatcherMethodName: 'useMemo',
wrapperNames: null,
wrapperNames: ['useMemo'],
});
return value;
}
Expand All @@ -487,7 +483,7 @@ function useSyncExternalStore<T>(
value,
debugInfo: null,
dispatcherMethodName: 'useSyncExternalStore',
wrapperNames: null,
wrapperNames: ['useSyncExternalStore'],
});
return value;
}
Expand All @@ -511,7 +507,7 @@ function useTransition(): [
value: isPending,
debugInfo: null,
dispatcherMethodName: 'useTransition',
wrapperNames: null,
wrapperNames: ['useTransition'],
});
return [isPending, () => {}];
}
Expand All @@ -526,7 +522,7 @@ function useDeferredValue<T>(value: T, initialValue?: T): T {
value: prevValue,
debugInfo: null,
dispatcherMethodName: 'useDeferredValue',
wrapperNames: null,
wrapperNames: ['useDeferredValue'],
});
return prevValue;
}
Expand All @@ -541,7 +537,7 @@ function useId(): string {
value: id,
debugInfo: null,
dispatcherMethodName: 'useId',
wrapperNames: null,
wrapperNames: ['useId'],
});
return id;
}
Expand Down Expand Up @@ -593,7 +589,7 @@ function useOptimistic<S, A>(
value: state,
debugInfo: null,
dispatcherMethodName: 'useOptimistic',
wrapperNames: null,
wrapperNames: ['useOptimistic'],
});
return [state, (action: A) => {}];
}
Expand Down Expand Up @@ -654,7 +650,7 @@ function useFormState<S, P>(
value: value,
debugInfo: debugInfo,
dispatcherMethodName: 'useFormState',
wrapperNames: null,
wrapperNames: ['useFormState'],
});

if (error !== null) {
Expand Down Expand Up @@ -847,18 +843,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 127b116

Please sign in to comment.