An Activity that rerenders in the same mode walks its whole retained subtree

Open
#176 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
30/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
react, typescript
Domain
performance

Research direction

Start with renderActivity in dom/features/activity/full.ts and trace its prepare/syncDom and reconnect walks for an Activity whose mode stays the same. The issue includes a reproducer and reports tests on an existing branch, including a fiber read count test; inspect those tests and verify reveal and hide behavior remains unchanged. Done means same-mode rerenders skip the retained subtree walk while mode changes still process it.

Written by the indexing model from the issue text.

Description

Rerendering an <Activity> whose mode did not change still walks every retained fiber below it, twice. With 2,000 memoized rows inside, a parent rerender takes 28 ms per update on main, against 0.04 ms on React 19.3.0, which only traverses an Activity's subtree when its visibility changes.

import { Activity, memo, useLayoutEffect, useState } from 'react'

const Row = memo(({ id }: { id: number }) => {
  useLayoutEffect(() => () => {}, [])
  return <li>{id}</li>
})
const Rows = memo(() => <ul>{Array.from({ length: 2000 }, (_, id) => <Row key={id} id={id} />)}</ul>)

export function Example() {
  const [tick, setTick] = useState(0)
  return <>
    <button onClick={() => setTick(value => value + 1)}>Rerender {tick}</button>
    <Activity mode="visible"><Rows /></Activity>
  </>
}

renderActivity (dom/features/activity/full.ts) runs its prepare/syncDom walk and its reconnect walk on every render. Walking only when the mode changes is enough: children that render are already prepared and synced as they render, the rest keep the state the last mode change gave them, and an Activity above that changes mode walks the subtree itself.

That is 13 lines, and I have it on a branch with tests, since PRs are limited to collaborators. Happy to open one if you want it, or take the idea as is:
https://github.com/TanStack/redact/compare/main...santiago-ramos-02:redact:perf/activity-skip-activity

  • Speed: same-mode rerender 28.0 to 1.15 ms per update, -95.9% (95% interval -96.3% to -89.8%). Reveal and hide are unchanged.
  • Size: +9 B gzip on the default client (23,411 to 23,420), 0 on nano. Every fixed budget in size-check.mjs passes unchanged.
  • Tests: on the branch, including one that counts fiber reads over the retained subtree during a same-mode rerender and fails on main (301 per Activity, expected 0).
Dominant language
TypeScript
Stars
581
Forks
28
Avg merge
13h 48m
Merged PRs (30d)
45

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from TanStack/redact

All issues in TanStack/redact

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.