Skip to content

Performance: remove memory allocations in useReducer #33165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions packages/react-server/src/ReactFizzHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ export function useReducer<S, I, A>(
// $FlowFixMe[incompatible-use] found when upgrading Flow
renderPhaseUpdates.delete(queue);
// $FlowFixMe[incompatible-use] found when upgrading Flow
let newState = workInProgressHook.memoizedState;
let newState = workInProgressHook.memoizedState[0];
let update: Update<any> = firstRenderPhaseUpdate;
do {
// Process this render phase update. We don't have to check the
Expand All @@ -398,13 +398,14 @@ export function useReducer<S, I, A>(
} while (update !== null);

// $FlowFixMe[incompatible-use] found when upgrading Flow
workInProgressHook.memoizedState = newState;

return [newState, dispatch];
workInProgressHook.memoizedState = [newState, dispatch];
Copy link
Contributor Author

@romgrk romgrk May 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about .memoizedState[0] = newState, but that would make the return value of useState stable across renders, even when there are changes, and that would be conceptually wrong for this API.


Tbh my true aim was to have a state hook with a stable value (and absolutely no memory allocations beyond initialization), but it would have an API like this:

const enabled = useStableState(false);
// => { get: () => boolean, set: (v: boolean) => void }

return (
  <button onClick=(() => enabled.set(!enabled.get())>
    {enabled.get() ? 'enabled' : 'disabled'}
  </button>
)

However, I fear it won't be possible without adding it directly in React as I need access to the internals to implement it properly.

if (__DEV__) {
Object.freeze(workInProgressHook.memoizedState);
}
}
}
// $FlowFixMe[incompatible-use] found when upgrading Flow
return [workInProgressHook.memoizedState, dispatch];
return workInProgressHook.memoizedState;
} else {
if (__DEV__) {
isInHookUserCodeInDev = true;
Expand All @@ -424,8 +425,6 @@ export function useReducer<S, I, A>(
isInHookUserCodeInDev = false;
}
// $FlowFixMe[incompatible-use] found when upgrading Flow
workInProgressHook.memoizedState = initialState;
// $FlowFixMe[incompatible-use] found when upgrading Flow
const queue: UpdateQueue<A> = (workInProgressHook.queue = {
last: null,
dispatch: null,
Expand All @@ -436,7 +435,12 @@ export function useReducer<S, I, A>(
queue,
): any));
// $FlowFixMe[incompatible-use] found when upgrading Flow
return [workInProgressHook.memoizedState, dispatch];
workInProgressHook.memoizedState = [initialState, dispatch];
if (__DEV__) {
Object.freeze(workInProgressHook.memoizedState);
}
// $FlowFixMe[incompatible-use] found when upgrading Flow
return workInProgressHook.memoizedState;
}
}

Expand Down
Loading