-
Notifications
You must be signed in to change notification settings - Fork 5.5k
feat(perf): Optimize use{,Multi}Polling by defining useSyncEqualityCheck
#38339
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
feat(perf): Optimize use{,Multi}Polling by defining useSyncEqualityCheck
#38339
Conversation
|
CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes. |
useSyncEqualityCheck and use in polling hooksuseSyncEqualityCheck and use in polling hooks
be2dc85 to
bb0ee4b
Compare
Builds ready [bb0ee4b]
UI Startup Metrics (1328 ± 116 ms)
📊 Page Load Benchmark ResultsCurrent Commit: 📄 Localhost MetaMask Test DappSamples: 100 Summary
📈 Detailed Results
Bundle size diffs [🚨 Warning! Bundle size has increased!]
|
useSyncEqualityCheck and use in polling hooksuseSyncEqualityCheck to fix polling hooks
bb0ee4b to
f577186
Compare
Builds ready [e7f895d]
UI Startup Metrics (1274 ± 125 ms)
📊 Page Load Benchmark ResultsCurrent Commit: 📄 Localhost MetaMask Test DappSamples: 100 Summary
📈 Detailed Results
Bundle size diffs [🚨 Warning! Bundle size has increased!]
|
useSyncEqualityCheck to fix polling hooksuseSyncEqualityCheck to fix polling hooks
ae28b07 to
702c081
Compare
|
@metamaskbot update-policies |
|
Policies updated. 🧠 Learn how: https://lavamoat.github.io/guides/policy-diff/#what-to-look-for-when-reviewing-a-policy-diff |
✨ Files requiring CODEOWNER review ✨📜 @MetaMask/policy-reviewers (7 files, +32 -32)
Tip Follow the policy review process outlined in the LavaMoat Policy Review Process doc before expecting an approval from Policy Reviewers. |
Builds ready [c7904b5]
UI Startup Metrics (1301 ± 112 ms)
📊 Page Load Benchmark ResultsCurrent Commit: 📄 Localhost MetaMask Test DappSamples: 100 Summary
📈 Detailed Results
Bundle size diffs [🚨 Warning! Bundle size has increased!]
|
65055ba to
c26e7e3
Compare
|
@metamaskbot update-policies |
|
Policies updated. 🧠 Learn how: https://lavamoat.github.io/guides/policy-diff/#what-to-look-for-when-reviewing-a-policy-diff |
Builds ready [0ba59c7]
UI Startup Metrics (1269 ± 126 ms)
📊 Page Load Benchmark ResultsCurrent Commit: 📄 Localhost MetaMask Test DappSamples: 100 Summary
📈 Detailed Results
Bundle size diffs [🚨 Warning! Bundle size has increased!]
|
use{,Multi}Polling by defining useSyncEqualityCheckuse{,Multi}Polling by defining useSyncEqualityCheck
Gudahtt
left a comment
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 nit comment left, but otherwise LGTM!
Builds ready [cd2559b]
UI Startup Metrics (1301 ± 108 ms)
📊 Page Load Benchmark ResultsCurrent Commit: 📄 Localhost MetaMask Test DappSamples: 100 Summary
📈 Detailed Results
Bundle size diffs [🚨 Warning! Bundle size has increased!]
|
Gudahtt
left a comment
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.
LGTM!
Builds ready [3551aaa]
UI Startup Metrics (1299 ± 98 ms)
📊 Page Load Benchmark ResultsCurrent Commit: 📄 Localhost MetaMask Test DappSamples: 100 Summary
📈 Detailed Results
Bundle size diffs [🚨 Warning! Bundle size has increased!]
|
Builds ready [460939c]
UI Startup Metrics (1293 ± 111 ms)
📊 Page Load Benchmark ResultsCurrent Commit: 📄 Localhost MetaMask Test DappSamples: 100 Summary
📈 Detailed Results
Bundle size diffs [🚨 Warning! Bundle size has increased!]
|
Motivation
Dependency arrays containing complex expressions e.g.
JSON.stringifyis an anti-pattern and explicitly forbidden by React rules of hooks.useEqualityCheckprovides change detection logic, but it triggers an extra re-render due to its being implemented withuseState. For the purposes of stabilizing hook dependency references, this extra render is often unnecessary.Description
useSyncEqualityCheckOptimizations:
useEqualityCheck, runs without triggering extra re-renders.JSON.stringifywithfast-json-stable-stringify(consistent with@metamask/polling-controller).JSON.parseto handle mutation)Example: Stringify overhead reduction
usePollingFixes:
callIdReftracks effect generations, stops stale promisesisMountedcheck before storing tokenenabled=falsedidn't stop active pollsuseSyncEqualityCheckprovides stable refsuseMultiPollingOptimizations:
inputKeyMap-> Time complexity: Quadratic -> Linear(N = existing polls, M = inputs, k = serialized size)
Fixes (Race Conditions):
pendingPollsSetfinallyclears pending, making inputs available for retriesChangelog
CHANGELOG entry: Optimized network polling requests for tokens, rates, currencies, gas fees etc.
Related issues
useMultiPollingeffect cleanup #38405Manual testing steps
Screenshots/Recordings
Before
After
Pre-merge author checklist
Pre-merge reviewer checklist
Note
Improves polling reliability and performance via reference-stable inputs and race-safe behavior.
useSyncEqualityCheck(usesfast-json-stable-stringify) to stabilize JSON-serializable deps without extra rendersusePollingto cleanly stop stale tokens, handle unmount/disabled states, and prevent orphaned pollsuseMultiPollingwithinputKeyMapandpendingPollsto de-dupe starts, stop removed inputs, and resolve races; linear-time diffsfast-json-stable-stringifydependency; updates LavaMoat policies and yarn.lock accordinglyWritten by Cursor Bugbot for commit 460939c. This will update automatically on new commits. Configure here.