Skip to content
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

Re-sort rerender queue if modified while we are processing rerenders #3871

Merged
merged 4 commits into from
Feb 3, 2023

Conversation

andrewiggins
Copy link
Member

By re-sorting the rerender queue while flushing it, we ensure we always rerender from the top-down of the vdom tree. This behavior is especially important for flushing context updates which trigger updates of nodes further down the tree. We should always process these top-down so the context updates propagate before other rerenders which may depend on the context update rerender. See the commit message in 672782a for a more detailed description of the bug and the test that this PR adds.

@github-actions
Copy link

github-actions bot commented Jan 27, 2023

📊 Tachometer Benchmark Results

Summary

duration

  • 02_replace1k: unsure 🔍 -10% - +4% (-21.70ms - +9.96ms)
    preact-local vs preact-master
  • 03_update10th1k_x16: unsure 🔍 -1% - +7% (-0.37ms - +3.38ms)
    preact-local vs preact-master
  • 07_create10k: unsure 🔍 -0% - +0% (-9.19ms - +6.91ms)
    preact-local vs preact-master
  • filter_list: unsure 🔍 -1% - +2% (-0.21ms - +0.56ms)
    preact-local vs preact-master
  • hydrate1k: unsure 🔍 -2% - +3% (-2.21ms - +4.22ms)
    preact-local vs preact-master
  • many_updates: unsure 🔍 -0% - +4% (-0.04ms - +0.95ms)
    preact-local vs preact-master
  • text_update: unsure 🔍 -7% - +1% (-0.28ms - +0.05ms)
    preact-local vs preact-master
  • todo: unsure 🔍 -2% - +0% (-1.12ms - +0.22ms)
    preact-local vs preact-master

usedJSHeapSize

  • 02_replace1k: unsure 🔍 -4% - +10% (-0.12ms - +0.35ms)
    preact-local vs preact-master
  • 03_update10th1k_x16: slower ❌ 1% - 2% (0.04ms - 0.07ms)
    preact-local vs preact-master
  • 07_create10k: unsure 🔍 +0% - +0% (+0.00ms - +0.00ms)
    preact-local vs preact-master
  • filter_list: unsure 🔍 -0% - +0% (-0.00ms - +0.00ms)
    preact-local vs preact-master
  • hydrate1k: unsure 🔍 -7% - +3% (-0.78ms - +0.30ms)
    preact-local vs preact-master
  • many_updates: unsure 🔍 -0% - +1% (-0.02ms - +0.03ms)
    preact-local vs preact-master
  • text_update: unsure 🔍 +0% - +0% (+0.00ms - +0.00ms)
    preact-local vs preact-master
  • todo: unsure 🔍 -0% - +0% (-0.00ms - +0.00ms)
    preact-local vs preact-master

Results

02_replace1k

duration

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master208.76ms - 229.00ms-unsure 🔍
-5% - +10%
-9.96ms - +21.70ms
unsure 🔍
-8% - +2%
-17.54ms - +5.07ms
preact-local200.84ms - 225.18msunsure 🔍
-10% - +4%
-21.70ms - +9.96ms
-unsure 🔍
-11% - +0%
-25.28ms - +1.07ms
preact-hooks220.07ms - 230.15msunsure 🔍
-2% - +8%
-5.07ms - +17.54ms
unsure 🔍
-1% - +12%
-1.07ms - +25.28ms
-

usedJSHeapSize

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master3.43ms - 3.71ms-unsure 🔍
-9% - +3%
-0.35ms - +0.12ms
unsure 🔍
-6% - +7%
-0.20ms - +0.23ms
preact-local3.49ms - 3.87msunsure 🔍
-4% - +10%
-0.12ms - +0.35ms
-unsure 🔍
-3% - +11%
-0.12ms - +0.38ms
preact-hooks3.39ms - 3.71msunsure 🔍
-6% - +5%
-0.23ms - +0.20ms
unsure 🔍
-10% - +3%
-0.38ms - +0.12ms
-

run-warmup-0

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master1.23ms - 1.32ms-unsure 🔍
-5% - +7%
-0.06ms - +0.09ms
unsure 🔍
-4% - +7%
-0.05ms - +0.09ms
preact-local1.20ms - 1.32msunsure 🔍
-7% - +4%
-0.09ms - +0.06ms
-unsure 🔍
-6% - +6%
-0.07ms - +0.08ms
preact-hooks1.21ms - 1.31msunsure 🔍
-7% - +4%
-0.09ms - +0.05ms
unsure 🔍
-6% - +6%
-0.08ms - +0.07ms
-

run-warmup-1

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master0.81ms - 1.16ms-unsure 🔍
-18% - +31%
-0.16ms - +0.28ms
unsure 🔍
-31% - +21%
-0.32ms - +0.23ms
preact-local0.79ms - 1.05msunsure 🔍
-28% - +15%
-0.28ms - +0.16ms
-unsure 🔍
-33% - +12%
-0.36ms - +0.14ms
preact-hooks0.82ms - 1.25msunsure 🔍
-24% - +33%
-0.23ms - +0.32ms
unsure 🔍
-16% - +40%
-0.14ms - +0.36ms
-

run-warmup-2

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master0.44ms - 0.59ms-unsure 🔍
-49% - +6%
-0.36ms - +0.08ms
unsure 🔍
-8% - +36%
-0.03ms - +0.16ms
preact-local0.45ms - 0.86msunsure 🔍
-17% - +71%
-0.08ms - +0.36ms
-unsure 🔍
-4% - +93%
-0.01ms - +0.42ms
preact-hooks0.40ms - 0.51msunsure 🔍
-29% - +5%
-0.16ms - +0.03ms
unsure 🔍
-54% - -8%
-0.42ms - +0.01ms
-

run-warmup-3

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master0.58ms - 1.04ms-unsure 🔍
-57% - +37%
-0.55ms - +0.37ms
unsure 🔍
-39% - +43%
-0.30ms - +0.34ms
preact-local0.50ms - 1.30msunsure 🔍
-47% - +69%
-0.37ms - +0.55ms
-unsure 🔍
-46% - +73%
-0.35ms - +0.57ms
preact-hooks0.57ms - 1.02msunsure 🔍
-41% - +37%
-0.34ms - +0.30ms
unsure 🔍
-58% - +35%
-0.57ms - +0.35ms
-

run-warmup-4

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master0.50ms - 0.56ms-unsure 🔍
-20% - +3%
-0.12ms - +0.02ms
unsure 🔍
-10% - +9%
-0.05ms - +0.05ms
preact-local0.51ms - 0.64msunsure 🔍
-5% - +22%
-0.02ms - +0.12ms
-unsure 🔍
-6% - +23%
-0.03ms - +0.12ms
preact-hooks0.49ms - 0.57msunsure 🔍
-9% - +10%
-0.05ms - +0.05ms
unsure 🔍
-20% - +4%
-0.12ms - +0.03ms
-

run-final

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master0.37ms - 0.83ms-unsure 🔍
-54% - +49%
-0.34ms - +0.31ms
unsure 🔍
-40% - +59%
-0.21ms - +0.32ms
preact-local0.39ms - 0.84msunsure 🔍
-51% - +56%
-0.31ms - +0.34ms
-unsure 🔍
-37% - +61%
-0.19ms - +0.33ms
preact-hooks0.42ms - 0.69msunsure 🔍
-50% - +33%
-0.32ms - +0.21ms
unsure 🔍
-50% - +28%
-0.33ms - +0.19ms
-
03_update10th1k_x16

duration

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master51.16ms - 53.59ms-unsure 🔍
-6% - +1%
-3.38ms - +0.37ms
unsure 🔍
-5% - +2%
-2.57ms - +0.86ms
preact-local52.45ms - 55.31msunsure 🔍
-1% - +7%
-0.37ms - +3.38ms
-unsure 🔍
-2% - +5%
-1.22ms - +2.53ms
preact-hooks52.01ms - 54.44msunsure 🔍
-2% - +5%
-0.86ms - +2.57ms
unsure 🔍
-5% - +2%
-2.53ms - +1.22ms
-

usedJSHeapSize

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master3.30ms - 3.31ms-faster ✔
1% - 2%
0.04ms - 0.07ms
faster ✔
1% - 2%
0.05ms - 0.08ms
preact-local3.34ms - 3.37msslower ❌
1% - 2%
0.04ms - 0.07ms
-unsure 🔍
-1% - +0%
-0.03ms - +0.01ms
preact-hooks3.35ms - 3.38msslower ❌
1% - 2%
0.05ms - 0.08ms
unsure 🔍
-0% - +1%
-0.01ms - +0.03ms
-
07_create10k

duration

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master1921.11ms - 1932.38ms-unsure 🔍
-0% - +0%
-6.91ms - +9.19ms
unsure 🔍
-1% - +0%
-14.26ms - +1.01ms
preact-local1919.86ms - 1931.36msunsure 🔍
-0% - +0%
-9.19ms - +6.91ms
-faster ✔
0% - 1%
0.04ms - 15.48ms
preact-hooks1928.21ms - 1938.52msunsure 🔍
-0% - +1%
-1.01ms - +14.26ms
slower ❌
0% - 1%
0.04ms - 15.48ms
-

usedJSHeapSize

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master25.36ms - 25.36ms-unsure 🔍
-0% - -0%
-0.00ms - -0.00ms
unsure 🔍
-0% - -0%
-0.02ms - -0.02ms
preact-local25.36ms - 25.36msunsure 🔍
+0% - +0%
+0.00ms - +0.00ms
-unsure 🔍
-0% - -0%
-0.02ms - -0.02ms
preact-hooks25.38ms - 25.38msunsure 🔍
+0% - +0%
+0.02ms - +0.02ms
unsure 🔍
+0% - +0%
+0.02ms - +0.02ms
-
filter_list

duration

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master24.67ms - 25.13ms-unsure 🔍
-2% - +1%
-0.56ms - +0.21ms
unsure 🔍
-3% - +0%
-0.64ms - +0.06ms
preact-local24.77ms - 25.39msunsure 🔍
-1% - +2%
-0.21ms - +0.56ms
-unsure 🔍
-2% - +1%
-0.52ms - +0.29ms
preact-hooks24.93ms - 25.46msunsure 🔍
-0% - +3%
-0.06ms - +0.64ms
unsure 🔍
-1% - +2%
-0.29ms - +0.52ms
-

usedJSHeapSize

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master1.59ms - 1.59ms-unsure 🔍
-0% - +0%
-0.00ms - +0.00ms
faster ✔
1% - 2%
0.02ms - 0.03ms
preact-local1.59ms - 1.60msunsure 🔍
-0% - +0%
-0.00ms - +0.00ms
-faster ✔
1% - 2%
0.02ms - 0.02ms
preact-hooks1.61ms - 1.62msslower ❌
1% - 2%
0.02ms - 0.03ms
slower ❌
1% - 2%
0.02ms - 0.02ms
-
hydrate1k

duration

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master122.36ms - 126.93ms-unsure 🔍
-3% - +2%
-4.22ms - +2.21ms
unsure 🔍
-5% - +0%
-6.29ms - +0.59ms
preact-local123.38ms - 127.91msunsure 🔍
-2% - +3%
-2.21ms - +4.22ms
-unsure 🔍
-4% - +1%
-5.28ms - +1.59ms
preact-hooks124.92ms - 130.07msunsure 🔍
-1% - +5%
-0.59ms - +6.29ms
unsure 🔍
-1% - +4%
-1.59ms - +5.28ms
-

usedJSHeapSize

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master10.73ms - 11.53ms-unsure 🔍
-3% - +7%
-0.30ms - +0.78ms
unsure 🔍
-4% - +7%
-0.43ms - +0.78ms
preact-local10.52ms - 11.26msunsure 🔍
-7% - +3%
-0.78ms - +0.30ms
-unsure 🔍
-6% - +5%
-0.65ms - +0.52ms
preact-hooks10.49ms - 11.41msunsure 🔍
-7% - +4%
-0.78ms - +0.43ms
unsure 🔍
-5% - +6%
-0.52ms - +0.65ms
-
many_updates

duration

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master24.06ms - 24.59ms-unsure 🔍
-4% - +0%
-0.95ms - +0.04ms
faster ✔
1% - 3%
0.15ms - 0.86ms
preact-local24.37ms - 25.20msunsure 🔍
-0% - +4%
-0.04ms - +0.95ms
-unsure 🔍
-2% - +2%
-0.53ms - +0.43ms
preact-hooks24.60ms - 25.06msslower ❌
1% - 4%
0.15ms - 0.86ms
unsure 🔍
-2% - +2%
-0.43ms - +0.53ms
-

usedJSHeapSize

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master4.60ms - 4.63ms-unsure 🔍
-1% - +0%
-0.03ms - +0.02ms
faster ✔
1% - 1%
0.03ms - 0.06ms
preact-local4.60ms - 4.64msunsure 🔍
-0% - +1%
-0.02ms - +0.03ms
-faster ✔
1% - 1%
0.03ms - 0.06ms
preact-hooks4.66ms - 4.66msslower ❌
1% - 1%
0.03ms - 0.06ms
slower ❌
1% - 1%
0.03ms - 0.06ms
-
text_update

duration

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master3.92ms - 4.21ms-unsure 🔍
-1% - +7%
-0.05ms - +0.28ms
unsure 🔍
-8% - +0%
-0.33ms - +0.02ms
preact-local3.86ms - 4.03msunsure 🔍
-7% - +1%
-0.28ms - +0.05ms
-faster ✔
4% - 9%
0.15ms - 0.40ms
preact-hooks4.12ms - 4.32msunsure 🔍
-1% - +8%
-0.02ms - +0.33ms
slower ❌
4% - 10%
0.15ms - 0.40ms
-

usedJSHeapSize

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master0.82ms - 0.82ms-unsure 🔍
-0% - -0%
-0.00ms - -0.00ms
faster ✔
2% - 2%
0.01ms - 0.01ms
preact-local0.82ms - 0.82msunsure 🔍
+0% - +0%
+0.00ms - +0.00ms
-faster ✔
2% - 2%
0.01ms - 0.01ms
preact-hooks0.83ms - 0.83msslower ❌
2% - 2%
0.01ms - 0.01ms
slower ❌
2% - 2%
0.01ms - 0.01ms
-
todo

duration

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master54.51ms - 55.53ms-unsure 🔍
-0% - +2%
-0.22ms - +1.12ms
unsure 🔍
-2% - +0%
-0.93ms - +0.24ms
preact-local54.13ms - 55.00msunsure 🔍
-2% - +0%
-1.12ms - +0.22ms
-faster ✔
0% - 2%
0.27ms - 1.32ms
preact-hooks55.08ms - 55.65msunsure 🔍
-0% - +2%
-0.24ms - +0.93ms
slower ❌
0% - 2%
0.27ms - 1.32ms
-

usedJSHeapSize

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master1.10ms - 1.11ms-unsure 🔍
-0% - +0%
-0.00ms - +0.00ms
faster ✔
2% - 3%
0.02ms - 0.03ms
preact-local1.10ms - 1.11msunsure 🔍
-0% - +0%
-0.00ms - +0.00ms
-faster ✔
2% - 2%
0.02ms - 0.03ms
preact-hooks1.13ms - 1.13msslower ❌
2% - 3%
0.02ms - 0.03ms
slower ❌
2% - 3%
0.02ms - 0.03ms
-

tachometer-reporter-action v2 for Benchmarks

@coveralls
Copy link

coveralls commented Jan 27, 2023

Coverage Status

Coverage: 99.533% (+0.001%) from 99.531% when pulling 4e55f55 on context-fix into 5eecaf1 on master.

@github-actions
Copy link

github-actions bot commented Jan 27, 2023

Size Change: +176 B (0%)

Total Size: 53.3 kB

Filename Size Change
dist/preact.js 4.11 kB +32 B (0%)
dist/preact.min.js 4.13 kB +28 B (0%)
dist/preact.min.module.js 4.13 kB +31 B (0%)
dist/preact.min.umd.js 4.16 kB +26 B (0%)
dist/preact.module.js 4.13 kB +29 B (0%)
dist/preact.umd.js 4.17 kB +30 B (0%)
ℹ️ View Unchanged
Filename Size Change
compat/dist/compat.js 3.81 kB 0 B
compat/dist/compat.module.js 3.75 kB 0 B
compat/dist/compat.umd.js 3.87 kB 0 B
debug/dist/debug.js 3 kB 0 B
debug/dist/debug.module.js 3.01 kB 0 B
debug/dist/debug.umd.js 3.08 kB 0 B
devtools/dist/devtools.js 231 B 0 B
devtools/dist/devtools.module.js 240 B 0 B
devtools/dist/devtools.umd.js 315 B 0 B
hooks/dist/hooks.js 1.49 kB 0 B
hooks/dist/hooks.module.js 1.52 kB 0 B
hooks/dist/hooks.umd.js 1.57 kB 0 B
jsx-runtime/dist/jsxRuntime.js 360 B 0 B
jsx-runtime/dist/jsxRuntime.module.js 326 B 0 B
jsx-runtime/dist/jsxRuntime.umd.js 441 B 0 B
test-utils/dist/testUtils.js 442 B 0 B
test-utils/dist/testUtils.module.js 444 B 0 B
test-utils/dist/testUtils.umd.js 526 B 0 B

compressed-size-action

andrewiggins and others added 4 commits February 3, 2023 11:45
Found a difference between how React and Preact propagate context. It shows up when using react-router and a component triggers a location update (e.g. `history.push`) and at the same time triggers a local state update.

## The setup

First, how React Router works. The `Router` component watches the pages current location and provides it on a context. Let's call this context `RouterContext`. The `Route` component consumes this `RouterContext`, but also modifies it and re-provides the new value on a new instance of `RouterContext.Provider`.

So if we have a `Page` component that lives under a `Router` and `Route` components, the virtual DOM tree looks something the following. Let's say the initial value of the provided location in `RouterContext` is `/page/1`

```jsx
<App>
  <Router>
    <RouterContext.Provider> {/* location: /page/1 */}
      <Route>
        <RouterContext.Consumer>
          <RouterContext.Provider> {/* location: /page/1 */}
            <Page>
              <button>
```

Let's say the `button` under `Page` has a click handler that triggers a location update on `Router` as well as updates some local state:

```jsx
function Page() {
	const history = useHistory(); // Magical hook that triggers a state update in the Router with a new location entry
	const [value, setValue] = useState(1);

	// let's pretend to read the current location and value for some purpose
	const { location } = useContext(RouterContext);
	console.log({ location, value });

	return (
		<button
			onClick={() => {
				history.push('/page/2'); // Triggers an state update in the Router component
				setValue(2); // Triggers a state update in Page
			}}
		>
			Update
		</button>
	);
}
```

Some things to note about this virtual tree:

1. The `RouteContext.Consumer` under `Route` is subscribed to its parent `RouteContext.Provider` (the one owned by `Router`)
2. The `Page` component is subscribed to it's nearest parent `RouteContext.Provider`, which in this case is the one owned by `Route`, not `Router`.

On initial render, `Page` would log `{ location: /page/1, value: 1 }`.

## The bug

So what happens in Preact when we click this button? Two state updates are triggered: one on `Router` and one `Page`. So our rerender queue looks like [`Router`, `Page`]. Upon rerendering Router, we update the location value we pass to the first `RouterContext.Provider` to `/page/2` and trigger updates to all of the `RouterContext` consumers. Let's re-examine our render queue and virtual DOM tree:

```jsx
// renderQueue: [Page, RouterContext.Consumer]
<App>
  <Router>
    <RouterContext.Provider> {/* location: /page/2 */}
      <Route>
        <RouterContext.Consumer>
          <RouterContext.Provider> {/* location: /page/1 */}
            <Page>
              <button>
```

A couple things to note. Our rerendering stops after the first `RouterContext.Provider` runs. Context providers just return `props.children` so our `vnodeID` optimization will kick in, see the children of the `Provider` didn't get no VNodes and stop rerendering there. Because of this, the `RouterContext.Consumer` under `Route` has NOT rerendered yet, so the `RouterContext.Provider` that it owns has not received the updated context. And since the `Page` component is subscribed to the `RouterContext.Provider` that `Route` owns, it still would see the old context.

So, since rerendering stopped after the first `RouterContext.Provider`, the next component that rerenders is `Page` (remember, it triggered it's own local state update). At this point `Page` will log `{ location: /page/1, value: 2 }`. This log represents the developer-observable bug.

The `onClick` handler of the button contains both the `history.push()` and `setValue()` calls and so the developer expects that the next time `Page` renders will be both a new location and a new state value. And logic that assumes this will fail.

## Completing the flow

If we continue with Preact's rendering (for completeness), once the `RouterContext.Consumer` rerenders, it'll rerender the second `RouterContext.Provider` which trigger rerenders to its subscribers, namely `Page`. A this point `Page` will rerender again but with the both the updated location context and the new state and so will log `{ location: /page/2, value: 2 }`.

## React's behavior

In this case, React handles the state propagation and local state update in one pass down the render tree. Presumably, changes to context synchronously mark consumers as needing updates (I haven't personally validated this yet). So as React walks down the tree, it rerenders the `Router`, the first `RouterContext.Provider`, sees the `RouterContext.Consumer` as needing a rerender, which causes a render of the second `RouterContext.Provider`, and ultimately rerenders the `Page` component with both it's local state update and the location update from its parent `RouterContext.Provider`. In this case, `Page` only rerenders once with both updates.
…l the rerenderQueue is empty and length == 0
@andrewiggins andrewiggins merged commit 1de5866 into master Feb 3, 2023
@andrewiggins andrewiggins deleted the context-fix branch February 3, 2023 19:57
JoviDeCroock added a commit that referenced this pull request Jan 12, 2024
JoviDeCroock added a commit that referenced this pull request Jan 12, 2024
* backport #3871

* port test from #3884 functionality seems to work in v11

* backport #3880

* backport #3875

* backport #3862

* add todo for #3801

* backport #3868

* backport #3867

* backport #3863

* add todo for #3856

* backport #3844

* backport #3816

* backport #3888

* backport #3889
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants