Skip to content

adding an item to a list, trigger re - evaluation of all items in the list ( observable by a function call that logs) #846

Closed

Description

@mizulu

logging in tsx

		<ul>
			@for (const item of items; key item.id) {
				<li>
					{item.label as string}
          {console.log("eval: ", item.id)}
					<button onClick={() => setItems(items.filter((x) => x.id !== item.id))}>
						× 
					</button>
				</li>
			} @empty {
				<li class="empty">Empty — add an item above.</li>
			}
		</ul>
Image

playground

this might conflict with rules of react.

Activity

  1. GabbeV commented on Aug 25, 2026

    @GabbeV

    Clarifying what zulu is saying with this issue. Rules of React says that functions used during render are allowed to be assumed only capture a single react commit so can be memoized just like data based on their identity. Given console.log has stable identity, there should be no reason for its usage in the @for to deopt the compilation of the @for from its normal behaviour where it skips rendering items that haven't changed.

    Discussing this with ChatGPT says that the deopt currently is intentionally motivated by tanstack table getIsSorted that doesn't seem to follow rules of React. So what are the rules really here? Is octane trying to somehow invent a weaker ruleset that allow less optimizations but is more compatible with React libraries doing bad things or is the rules of react the target? Are these rules written down somewhere allowing us to reason about what is a bug vs a intentional weak optimization rule for compatibility?

    https://chatgpt.com/c/6a8c7238-c270-83eb-9349-dee1a53ff0ba

  2. GabbeV commented on Aug 25, 2026

    @GabbeV

    Here is a clear example using the react-compiler showing that react them selves consider getIsSorted fair game to optimize. It clearly avoids calling it if table hasn't changed.

    react-compiler playground

  3. jonkwheeler commented on Aug 26, 2026

    @jonkwheeler
    Contributor

    Thanks — I validated this against the current compiler behavior and inspected the GIF frame by frame.

    I’d classify this as a real compiler performance / React Compiler parity gap affecting keyed @for, rather than a keyed-reconciliation correctness bug.

    What the GIF demonstrates

    • After appending item 4, the console logs items 1–4; after appending item 5, it logs items 1–5. Existing row bodies are therefore being reevaluated.
    • The keyed DOM nodes are still preserved. The extra work is render-time JavaScript evaluation, not removal and recreation of every row.
    • The instrumentation matters: console.log(...) is itself a member call, and its presence makes the compiler conservatively classify the whole row body as non-pure. A call-free keyed row remains eligible for the survivor short-circuit.
    • This is not limited to logging. The same policy affects legitimate calls such as header.column.getIsSorted().

    Why Octane currently does this

    Member calls fail closed because a stable receiver can hide mutable state. TanStack Table v8 is the concrete hazard: useReactTable returns a stable table object while updating options/state behind that identity. Skipping getIsSorted() solely because table is unchanged can freeze the displayed value.

    This policy is documented under Automatic memoization and calls in templates, and the compiler has a focused TanStack-shaped regression test for it.

    The React Compiler comparison in the comments is also correct. Compiling that exact playground with React Compiler 1.0.0 caches header.column.getIsSorted() based only on table identity. So the existing test’s “React parity” wording describes ordinary uncompiled React rendering; this behavior is deliberately not React Compiler parity.

    Validation

    The focused production-compiler suite passed: 2 test files, 76 tests, including the member-call/live-receiver cases.

    Recommendation

    I don’t think this issue should be closed as invalid. It is a genuine unnecessary-reevaluation/optimization gap, but changing the global rule is high risk because it can introduce stale UI for live receivers such as TanStack Table and virtualizers.

    I’d keep it open as a performance/design issue—perhaps retitled to “Member calls disable keyed @for survivor memoization”. A safe improvement would need either an explicit strict-purity opt-in or proof that a receiver is an immutable snapshot; it should not special-case console.log.

  4. self-assigned this
    on Aug 26, 2026
  5. GabbeV commented on Aug 26, 2026

    @GabbeV

    Tanstack table just seem fundamentally broken with compiler. https://stackblitz.com/edit/vitejs-vite-skzmbwtj?file=src%2FApp.tsx

    The reasoning octane claim in that linked documentation page isn't at all the reasoning the react compiler uses that happen to make the .map() case work out. It is a confluence of heuristics intended for other things that happen to make getIsSorted to rerun any time table is updated that makes it work out some times.

    Firstly react compiler currently does no memoization within a render callback like the callback passed to .map() at all. They have a option for this called enableJsxOutlining that totally breaks tanstack table. To be fair I think react has stated that all non default options for the compiler are to be considered experimental so not sure what the state of that flag is and if it is correctly implemented.

    If we get rid of map and instead just do table.getHeaderGroups()[0].headers[0].column.getIsSorted() then another rule kicks in where getHeaderGroups() return value is considered a potentially locally mutable object that getIsSorted() is mutating forcing the entire thing to execute within the same caching group even if you chop it up into multiple bindings and depend on different parts from different places etc. However if you pass one of the intermediate values to a hook react compiler suddenly consider it no longer mutable and breaks it spooky action at a distance style.

    const header = table.getHeaderGroups()[0].headers[0]
    // useDebugValue(header) // uncommenting this line breaks getIsSorted below 😱
    return <>{header.column.getIsSorted()}</>

    Tanstack table has some disclaimer about not passing any of the stable values (you have to remember which) to a component and this seemingly includes capturing it in a render function for a custom component. conceptually that is what @for is so saying it should work in octanes @for is weird. octane already wouldn't allow something like the following I would assume:

    @{
      let i = 0
      @for (const _ of Array.from({ length: 10 })) {
        <>{++i}</>
      }
    }

    React compiler consider something captured by a render function no longer mutable and instead consider it frozen, octane should reasonably do the same for @for. This can be fine as it is in react/octane though given it only mutates mutable render temporary state that is never mutated again after it escapes:

    @{
      let i = 0
      const arr = []
      for (const _ of Array.from({ length: 10 })) {
        arr.push(++i)
      }
      <>{arr}</>
    }

    I don't think octane can be claiming to follow react compilers rules and simultaneously support tanstack table. It would have to define some weaker rule set that somehow allows live values while the entire react model octane is built on fundamentally is built on not allowing things like that.

  6. GabbeV commented on Aug 26, 2026

    @GabbeV

    I posted an issue in the tanstack table gh as well to see what they think about it TanStack/table#6577

  7. jonkwheeler commented on Aug 27, 2026

    @jonkwheeler
    Contributor

    Following up with a more concrete recommendation after looking at this as a design problem rather than only a one-off optimization:

    I think the best next step is an explainability layer for keyed @for optimization, starting small in the compiler.

    1. Expose a stable EXPLAIN @for verdict through the compiler's existing inspection output: PURE, DEP_PURE, or NORMAL, plus structured reasons such as render-call: console.log, a changed parent capture, index use, or another unsupported dependency.
    2. Surface that verdict in the Playground with the active build mode. Development can continue executing authored code while saying that a production compile “would have skipped” a survivor; that avoids using console.log as a probe that itself changes optimization eligibility.
    3. If the static explanation proves useful, let DevTools consume the same reason codes and add bounded counters for row-body entries, survivor skips, mounts, moves, and DOM writes.

    That addresses the immediate confusion while preserving the important distinction:

    A key preserves row identity; compiler-proven purity enables evaluation skipping.

    I would not special-case console.log, and I would not make every keyed row an implicit memoized component boundary. Both approaches hide observable behavior or introduce a much broader semantic change.

    I would keep this issue open as a compiler performance/design gap, with the static explanation output as the lowest-risk first tranche. A stricter authoring control—such as a compiler-checked purity assertion—or a separate per-item revision witness could be explored later, but only after issue-shaped tests and benchmarks establish that the additional public semantics are worthwhile.

  8. domgan-oai commented on Sep 19, 2026

    @domgan-oai
    Contributor

    The strict-purity optimization discussed here landed in #862 and was completed by #866.

    Modules opting into "use strong" (or compiler.strong) allow eligible production keyed rows to reuse their output when their witnessed inputs stay unchanged; diagnostic console.log no longer disqualifies those rows. Compatibility mode deliberately keeps member calls live to support receivers with internal mutable state.

    The original sample's remove callback captures the entire items array:

    onClick={() => setItems(items.filter((entry) => entry.id !== item.id))}

    Appending replaces that capture, so surviving rows still need current handlers. Skipping this update could discard appended rows when an original row is removed. Strong preserves that closure behavior. A functional state updater avoids capturing the parent array when that is the intended behavior:

    onClick={() => setItems((current) => current.filter((entry) => entry.id !== item.id))}

    I checked canonical main 47d5d1df: these distinctions have regression coverage in packages/octane/tests/for.test.ts and deterministic controls in the template-call-memo benchmark. The keyed rows and logging documentation explains the exact reported capture case. This follow-up inspected the current source and checked-in coverage; it does not claim a fresh benchmark run.

    Closing as addressed by the delivered Strong purity contract and documented compatibility/capture semantics. The later suggested keyed-list explanation tooling remains a separate optional enhancement and is not implemented by these fixes.

    Issue triage and this comment were assisted by an agent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions