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

Add a fix for context unmounting, from experimenting with our application #3979

Closed

Conversation

jpnelson
Copy link

In testing for our application, this ended up being a fair bit faster than how it was during client routing transitions (that is, when unmounting lots of context subscribers)

I don't know if we need a benchmark specifically for having many context subscribers unmount, but that would be the use case that we ran into with this one.

@github-actions
Copy link

github-actions bot commented Apr 22, 2023

📊 Tachometer Benchmark Results

Summary

duration

  • 02_replace1k: faster ✔ 0% - 6% (0.15ms - 7.15ms)
    preact-local vs preact-master
  • 03_update10th1k_x16: unsure 🔍 -5% - +1% (-2.28ms - +0.46ms)
    preact-local vs preact-master
  • 07_create10k: unsure 🔍 -1% - +0% (-6.81ms - +4.22ms)
    preact-local vs preact-master
  • filter_list: unsure 🔍 -2% - +1% (-0.38ms - +0.26ms)
    preact-local vs preact-master
  • hydrate1k: unsure 🔍 -2% - +3% (-2.05ms - +3.26ms)
    preact-local vs preact-master
  • many_updates: unsure 🔍 -3% - +3% (-0.90ms - +0.98ms)
    preact-local vs preact-master
  • text_update: unsure 🔍 -1% - +4% (-0.03ms - +0.11ms)
    preact-local vs preact-master
  • todo: unsure 🔍 -4% - +2% (-2.91ms - +1.40ms)
    preact-local vs preact-master

usedJSHeapSize

  • 02_replace1k: unsure 🔍 -1% - +0% (-0.02ms - +0.01ms)
    preact-local vs preact-master
  • 03_update10th1k_x16: unsure 🔍 -0% - +0% (-0.00ms - +0.00ms)
    preact-local vs preact-master
  • 07_create10k: unsure 🔍 -0% - +0% (-0.03ms - +0.03ms)
    preact-local vs preact-master
  • filter_list: faster ✔ 0% - 1% (0.00ms - 0.01ms)
    preact-local vs preact-master
  • hydrate1k: unsure 🔍 -1% - +0% (-0.06ms - +0.03ms)
    preact-local vs preact-master
  • many_updates: unsure 🔍 -1% - +0% (-0.03ms - +0.02ms)
    preact-local vs preact-master
  • text_update: unsure 🔍 -0% - +3% (-0.00ms - +0.02ms)
    preact-local vs preact-master
  • todo: unsure 🔍 -1% - +0% (-0.01ms - +0.00ms)
    preact-local vs preact-master

Results

02_replace1k

duration

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master117.60ms - 122.98ms-slower ❌
0% - 6%
0.15ms - 7.15ms
unsure 🔍
-4% - +2%
-4.97ms - +3.02ms
preact-local114.40ms - 118.87msfaster ✔
0% - 6%
0.15ms - 7.15ms
-faster ✔
1% - 7%
0.93ms - 8.33ms
preact-hooks118.32ms - 124.21msunsure 🔍
-3% - +4%
-3.02ms - +4.97ms
slower ❌
1% - 7%
0.93ms - 8.33ms
-

usedJSHeapSize

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master3.37ms - 3.40ms-unsure 🔍
-0% - +1%
-0.01ms - +0.02ms
unsure 🔍
-1% - +0%
-0.03ms - +0.00ms
preact-local3.36ms - 3.38msunsure 🔍
-1% - +0%
-0.02ms - +0.01ms
-faster ✔
0% - 1%
0.01ms - 0.03ms
preact-hooks3.39ms - 3.40msunsure 🔍
-0% - +1%
-0.00ms - +0.03ms
slower ❌
0% - 1%
0.01ms - 0.03ms
-

run-warmup-0

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master42.33ms - 44.34ms-unsure 🔍
-6% - +2%
-2.49ms - +0.90ms
unsure 🔍
-5% - +1%
-2.44ms - +0.34ms
preact-local42.76ms - 45.49msunsure 🔍
-2% - +6%
-0.90ms - +2.49ms
-unsure 🔍
-4% - +3%
-1.93ms - +1.41ms
preact-hooks43.42ms - 45.34msunsure 🔍
-1% - +6%
-0.34ms - +2.44ms
unsure 🔍
-3% - +4%
-1.41ms - +1.93ms
-

run-warmup-1

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master59.01ms - 62.11ms-unsure 🔍
-3% - +4%
-1.71ms - +2.33ms
unsure 🔍
-5% - +1%
-3.22ms - +0.65ms
preact-local58.95ms - 61.56msunsure 🔍
-4% - +3%
-2.33ms - +1.71ms
-unsure 🔍
-5% - +0%
-3.34ms - +0.16ms
preact-hooks60.68ms - 63.01msunsure 🔍
-1% - +5%
-0.65ms - +3.22ms
unsure 🔍
-0% - +6%
-0.16ms - +3.34ms
-

run-warmup-2

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master39.31ms - 43.61ms-unsure 🔍
-5% - +9%
-2.15ms - +3.70ms
unsure 🔍
-6% - +7%
-2.61ms - +3.07ms
preact-local38.70ms - 42.67msunsure 🔍
-9% - +5%
-3.70ms - +2.15ms
-unsure 🔍
-8% - +5%
-3.26ms - +2.17ms
preact-hooks39.38ms - 43.09msunsure 🔍
-7% - +6%
-3.07ms - +2.61ms
unsure 🔍
-5% - +8%
-2.17ms - +3.26ms
-

run-warmup-3

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master49.40ms - 53.24ms-unsure 🔍
-7% - +4%
-3.63ms - +2.11ms
faster ✔
1% - 11%
0.66ms - 6.15ms
preact-local49.95ms - 54.22msunsure 🔍
-4% - +7%
-2.11ms - +3.63ms
-unsure 🔍
-10% - +0%
-5.54ms - +0.26ms
preact-hooks52.77ms - 56.68msslower ❌
1% - 12%
0.66ms - 6.15ms
unsure 🔍
-1% - +11%
-0.26ms - +5.54ms
-

run-warmup-4

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master49.59ms - 52.52ms-unsure 🔍
-5% - +3%
-2.48ms - +1.74ms
unsure 🔍
-6% - +4%
-3.05ms - +2.01ms
preact-local49.91ms - 52.95msunsure 🔍
-3% - +5%
-1.74ms - +2.48ms
-unsure 🔍
-5% - +5%
-2.71ms - +2.41ms
preact-hooks49.52ms - 53.64msunsure 🔍
-4% - +6%
-2.01ms - +3.05ms
unsure 🔍
-5% - +5%
-2.41ms - +2.71ms
-

run-final

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master32.59ms - 36.58ms-unsure 🔍
-1% - +15%
-0.17ms - +4.86ms
unsure 🔍
-13% - +3%
-4.98ms - +1.04ms
preact-local30.71ms - 33.77msunsure 🔍
-14% - +0%
-4.86ms - +0.17ms
-faster ✔
5% - 19%
1.58ms - 7.03ms
preact-hooks34.30ms - 38.80msunsure 🔍
-3% - +15%
-1.04ms - +4.98ms
slower ❌
5% - 22%
1.58ms - 7.03ms
-
03_update10th1k_x16

duration

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master45.74ms - 47.71ms-unsure 🔍
-1% - +5%
-0.46ms - +2.28ms
unsure 🔍
-1% - +5%
-0.62ms - +2.10ms
preact-local44.86ms - 46.76msunsure 🔍
-5% - +1%
-2.28ms - +0.46ms
-unsure 🔍
-3% - +3%
-1.51ms - +1.16ms
preact-hooks45.05ms - 46.93msunsure 🔍
-4% - +1%
-2.10ms - +0.62ms
unsure 🔍
-3% - +3%
-1.16ms - +1.51ms
-

usedJSHeapSize

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master3.30ms - 3.30ms-unsure 🔍
-0% - +0%
-0.00ms - +0.00ms
faster ✔
0% - 1%
0.01ms - 0.02ms
preact-local3.30ms - 3.30msunsure 🔍
-0% - +0%
-0.00ms - +0.00ms
-faster ✔
0% - 1%
0.02ms - 0.02ms
preact-hooks3.32ms - 3.32msslower ❌
0% - 1%
0.01ms - 0.02ms
slower ❌
0% - 1%
0.02ms - 0.02ms
-
07_create10k

duration

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master1314.45ms - 1322.89ms-unsure 🔍
-0% - +1%
-4.22ms - +6.81ms
unsure 🔍
-1% - +0%
-9.19ms - +6.14ms
preact-local1313.82ms - 1320.93msunsure 🔍
-1% - +0%
-6.81ms - +4.22ms
-unsure 🔍
-1% - +0%
-10.14ms - +4.50ms
preact-hooks1313.80ms - 1326.59msunsure 🔍
-0% - +1%
-6.14ms - +9.19ms
unsure 🔍
-0% - +1%
-4.50ms - +10.14ms
-

usedJSHeapSize

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master25.39ms - 25.44ms-unsure 🔍
-0% - +0%
-0.03ms - +0.03ms
unsure 🔍
+0% - +0%
+0.01ms - +0.06ms
preact-local25.39ms - 25.44msunsure 🔍
-0% - +0%
-0.03ms - +0.03ms
-unsure 🔍
+0% - +0%
+0.01ms - +0.06ms
preact-hooks25.38ms - 25.38msunsure 🔍
-0% - -0%
-0.06ms - -0.01ms
unsure 🔍
-0% - -0%
-0.06ms - -0.01ms
-
filter_list

duration

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master21.19ms - 21.61ms-unsure 🔍
-1% - +2%
-0.26ms - +0.38ms
unsure 🔍
-1% - +2%
-0.23ms - +0.37ms
preact-local21.10ms - 21.57msunsure 🔍
-2% - +1%
-0.38ms - +0.26ms
-unsure 🔍
-1% - +2%
-0.31ms - +0.33ms
preact-hooks21.11ms - 21.54msunsure 🔍
-2% - +1%
-0.37ms - +0.23ms
unsure 🔍
-2% - +1%
-0.33ms - +0.31ms
-

usedJSHeapSize

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master1.44ms - 1.45ms-slower ❌
0% - 1%
0.00ms - 0.01ms
faster ✔
1% - 1%
0.01ms - 0.02ms
preact-local1.44ms - 1.44msfaster ✔
0% - 1%
0.00ms - 0.01ms
-faster ✔
1% - 2%
0.02ms - 0.03ms
preact-hooks1.46ms - 1.46msslower ❌
1% - 1%
0.01ms - 0.02ms
slower ❌
1% - 2%
0.02ms - 0.03ms
-
hydrate1k

duration

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master114.94ms - 118.64ms-unsure 🔍
-3% - +2%
-3.26ms - +2.05ms
unsure 🔍
-1% - +4%
-1.23ms - +4.02ms
preact-local115.49ms - 119.31msunsure 🔍
-2% - +3%
-2.05ms - +3.26ms
-unsure 🔍
-1% - +4%
-0.66ms - +4.67ms
preact-hooks113.53ms - 117.26msunsure 🔍
-3% - +1%
-4.02ms - +1.23ms
unsure 🔍
-4% - +1%
-4.67ms - +0.66ms
-

usedJSHeapSize

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master5.99ms - 6.06ms-unsure 🔍
-0% - +1%
-0.03ms - +0.06ms
faster ✔
0% - 2%
0.02ms - 0.11ms
preact-local5.98ms - 6.04msunsure 🔍
-1% - +0%
-0.06ms - +0.03ms
-faster ✔
1% - 2%
0.04ms - 0.13ms
preact-hooks6.06ms - 6.12msslower ❌
0% - 2%
0.02ms - 0.11ms
slower ❌
1% - 2%
0.04ms - 0.13ms
-
many_updates

duration

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master33.22ms - 34.54ms-unsure 🔍
-3% - +3%
-0.98ms - +0.90ms
unsure 🔍
-5% - +0%
-1.79ms - +0.02ms
preact-local33.26ms - 34.59msunsure 🔍
-3% - +3%
-0.90ms - +0.98ms
-unsure 🔍
-5% - +0%
-1.75ms - +0.07ms
preact-hooks34.15ms - 35.38msunsure 🔍
-0% - +5%
-0.02ms - +1.79ms
unsure 🔍
-0% - +5%
-0.07ms - +1.75ms
-

usedJSHeapSize

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master4.50ms - 4.54ms-unsure 🔍
-0% - +1%
-0.02ms - +0.03ms
unsure 🔍
-1% - +0%
-0.04ms - +0.01ms
preact-local4.50ms - 4.53msunsure 🔍
-1% - +0%
-0.03ms - +0.02ms
-unsure 🔍
-1% - +0%
-0.04ms - +0.01ms
preact-hooks4.51ms - 4.55msunsure 🔍
-0% - +1%
-0.01ms - +0.04ms
unsure 🔍
-0% - +1%
-0.01ms - +0.04ms
-
text_update

duration

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master3.07ms - 3.18ms-unsure 🔍
-3% - +1%
-0.11ms - +0.03ms
faster ✔
3% - 7%
0.08ms - 0.23ms
preact-local3.11ms - 3.21msunsure 🔍
-1% - +4%
-0.03ms - +0.11ms
-faster ✔
2% - 6%
0.05ms - 0.19ms
preact-hooks3.23ms - 3.34msslower ❌
3% - 8%
0.08ms - 0.23ms
slower ❌
1% - 6%
0.05ms - 0.19ms
-

usedJSHeapSize

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master0.70ms - 0.71ms-unsure 🔍
-3% - +0%
-0.02ms - +0.00ms
slower ❌
3% - 5%
0.02ms - 0.03ms
preact-local0.71ms - 0.72msunsure 🔍
-0% - +3%
-0.00ms - +0.02ms
-slower ❌
4% - 6%
0.03ms - 0.04ms
preact-hooks0.68ms - 0.68msfaster ✔
2% - 4%
0.02ms - 0.03ms
faster ✔
4% - 6%
0.03ms - 0.04ms
-
todo

duration

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master73.72ms - 77.56ms-unsure 🔍
-2% - +4%
-1.40ms - +2.91ms
unsure 🔍
-4% - +2%
-3.10ms - +1.54ms
preact-local73.90ms - 75.87msunsure 🔍
-4% - +2%
-2.91ms - +1.40ms
-unsure 🔍
-4% - +0%
-3.17ms - +0.10ms
preact-hooks75.12ms - 77.73msunsure 🔍
-2% - +4%
-1.54ms - +3.10ms
unsure 🔍
-0% - +4%
-0.10ms - +3.17ms
-

usedJSHeapSize

VersionAvg timevs preact-mastervs preact-localvs preact-hooks
preact-master0.92ms - 0.93ms-unsure 🔍
-0% - +1%
-0.00ms - +0.01ms
faster ✔
2% - 3%
0.02ms - 0.03ms
preact-local0.92ms - 0.92msunsure 🔍
-1% - +0%
-0.01ms - +0.00ms
-faster ✔
3% - 3%
0.02ms - 0.03ms
preact-hooks0.95ms - 0.95msslower ❌
2% - 3%
0.02ms - 0.03ms
slower ❌
3% - 3%
0.02ms - 0.03ms
-

tachometer-reporter-action v2 for Benchmarks

@@ -25,7 +25,7 @@ export function createContext(defaultValue, contextId) {

this.getChildContext = () => ctx;

this.shouldComponentUpdate = function (_props) {
this.shouldComponentUpdate = function(_props) {
Copy link
Author

Choose a reason for hiding this comment

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

I didn't intend to change the formatting here but it seems as though the precommit hooks are doing that for me. I might have my setup incorrect, or maybe this file just needs these fixes with the config that exists?

Copy link
Member

Choose a reason for hiding this comment

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

All good, the same happens on my end. I guess somehow a commit sneaked through one PR that didn't apply the prettier formatting from the pre-commit hook.

Copy link
Member

Choose a reason for hiding this comment

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

Try reinstalling node_modules. We recently upgraded our prettier version so maybe that's the reason for the discrepancy? Either way, no biggie.

> npm ls prettier
[email protected] C:\code\github\preactjs\preact
└── [email protected]

@jpnelson jpnelson mentioned this pull request Apr 22, 2023
Copy link
Member

@marvinhagemeister marvinhagemeister left a comment

Choose a reason for hiding this comment

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

Love this! It's a clever trick and one of those optimizations where one wonders why nobody thought of that before. I think nobody would say no to a benchmark, but it's totally fine if that's too much work. I'm more than happy to merge the PR as is.

Thank you for filing a PR and for helping to make Preact even better 🙌

Copy link
Member

@andrewiggins andrewiggins left a comment

Choose a reason for hiding this comment

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

One additional thought

subs.splice(subs.indexOf(c), 1);
// This is a hot path so we want to be careful about how we're removing items from the subscriber array to keep this fast.
// This is faster than splicing an item out in testing.
subs[subs.indexOf(c)] = subs.pop();
Copy link
Member

Choose a reason for hiding this comment

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

I think this doesn't work if c is the last element in the array, since it'll reassign the last element back to it's original index. I think we have to do this in two lines.

Copy link
Member

Choose a reason for hiding this comment

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

oh right, I missed that in my review. Also what happens if there is only one entry in the array?

Copy link
Member

Choose a reason for hiding this comment

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

We could consider using Set for this if the Array is a pain, or switching to a sparse array (would require a guard on L45).

@@ -25,7 +25,7 @@ export function createContext(defaultValue, contextId) {

this.getChildContext = () => ctx;

this.shouldComponentUpdate = function (_props) {
this.shouldComponentUpdate = function(_props) {
Copy link
Member

Choose a reason for hiding this comment

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

Try reinstalling node_modules. We recently upgraded our prettier version so maybe that's the reason for the discrepancy? Either way, no biggie.

> npm ls prettier
[email protected] C:\code\github\preactjs\preact
└── [email protected]

developit added a commit that referenced this pull request Apr 25, 2023
@JoviDeCroock
Copy link
Member

Addressed in #4526

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.

6 participants