-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Conversation
📊 Tachometer Benchmark ResultsSummaryduration
usedJSHeapSize
Results02_replace1k
duration
usedJSHeapSize
run-warmup-0
run-warmup-1
run-warmup-2
run-warmup-3
run-warmup-4
run-final
03_update10th1k_x16
duration
usedJSHeapSize
07_create10k
duration
usedJSHeapSize
filter_list
duration
usedJSHeapSize
hydrate1k
duration
usedJSHeapSize
many_updates
duration
usedJSHeapSize
text_update
duration
usedJSHeapSize
todo
duration
usedJSHeapSize
|
@@ -25,7 +25,7 @@ export function createContext(defaultValue, contextId) { | |||
|
|||
this.getChildContext = () => ctx; | |||
|
|||
this.shouldComponentUpdate = function (_props) { | |||
this.shouldComponentUpdate = function(_props) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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]
There was a problem hiding this 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 🙌
There was a problem hiding this 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(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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]
Addressed in #4526 |
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.