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

perf: use redux state patches #26738

Merged
merged 28 commits into from
Sep 19, 2024
Merged

perf: use redux state patches #26738

merged 28 commits into from
Sep 19, 2024

Conversation

matthewwalsh0
Copy link
Member

@matthewwalsh0 matthewwalsh0 commented Aug 29, 2024

Description

Currently, the background page / service worker subscribes to state changes from every controller and on each state change (debounced to 200ms), sends the full flattened state to the UI where it is stored in the metamask duck.

The primary drawback of this is that Redux and React default to shallow equality comparisons meaning any object and array state is always detected as new, resulting in frequent unnecessary renders unless deep equality is explicitly used through mechanisms such as createDeepEqualSelector.

This PR aims to resolve this by instead sending state patches to the UI, and only for the top level state properties that no longer have shallow equality. These patches are then applied to the MetaMask duck in the UI, meaning it is now incrementally updated and any object and array references are only updated when the data itself has changed.

This logic is encapsulated within the new PatchStore class, with an alternate instance being created for each UI connection.

The patches are currently intentionally generated and applied without immer given this has the side effect of freezing the associated state, which triggers multiple errors due to pre-existing instances of direct state mutation in the background and UI code.

Open in GitHub Codespaces

Related issues

Fixes: #2987

Manual testing steps

No functional changes, but high level sanity checks would be beneficial given risk.

Screenshots/Recordings

Before

After

Pre-merge author checklist

Pre-merge reviewer checklist

  • I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed).
  • I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.

Add patch store.
@github-actions github-actions bot added the team-confirmations Push issues to confirmations team label Aug 29, 2024
@metamaskbot
Copy link
Collaborator

Builds ready [1e27752]
Page Load Metrics (1675 ± 82 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint40622291617327157
domContentLoaded15182217165817283
load15312228167517182
domInteractive147934167
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 2.7 KiB (0.08%)
  • ui: 26 Bytes (0.00%)
  • common: 213 Bytes (0.00%)

@metamaskbot
Copy link
Collaborator

Builds ready [3c4e8ae]
Page Load Metrics (1767 ± 80 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint22621791698376180
domContentLoaded15502170174316579
load15632179176716680
domInteractive13144423617
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 2.7 KiB (0.08%)
  • ui: 47 Bytes (0.00%)
  • common: -57 Bytes (-0.00%)

@matthewwalsh0 matthewwalsh0 added the team-tiger Tiger team (for tech debt reduction + performance improvements) label Sep 11, 2024
@matthewwalsh0 matthewwalsh0 marked this pull request as ready for review September 11, 2024 13:28
@matthewwalsh0 matthewwalsh0 requested review from a team as code owners September 11, 2024 13:28
Copy link

codecov bot commented Sep 11, 2024

Codecov Report

Attention: Patch coverage is 69.76744% with 39 lines in your changes missing coverage. Please review.

Project coverage is 69.99%. Comparing base (eadc707) to head (54bbdf5).

Files with missing lines Patch % Lines
ui/store/actions.ts 25.64% 29 Missing ⚠️
app/scripts/metamask-controller.js 57.14% 6 Missing ⚠️
app/scripts/controllers/preferences-controller.ts 66.67% 3 Missing ⚠️
ui/index.js 0.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop   #26738      +/-   ##
===========================================
- Coverage    70.02%   69.99%   -0.04%     
===========================================
  Files         1443     1445       +2     
  Lines        50164    50226      +62     
  Branches     14039    14045       +6     
===========================================
+ Hits         35126    35151      +25     
- Misses       15038    15075      +37     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@metamaskbot
Copy link
Collaborator

Builds ready [b5f22e8]
Page Load Metrics (1741 ± 99 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint22822951605450216
domContentLoaded14652287172420699
load14722293174120599
domInteractive236531105
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 3.28 KiB (0.10%)
  • ui: 47 Bytes (0.00%)
  • common: -135 Bytes (-0.00%)

@metamaskbot
Copy link
Collaborator

Builds ready [b5968fe]
Page Load Metrics (1700 ± 74 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint40721391631319153
domContentLoaded15032084168414570
load15152143170015474
domInteractive148032157
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 3.28 KiB (0.09%)
  • ui: 47 Bytes (0.00%)
  • common: -135 Bytes (-0.00%)

Copy link

sonarcloud bot commented Sep 18, 2024

@metamaskbot
Copy link
Collaborator

Builds ready [54bbdf5]
Page Load Metrics (1848 ± 148 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint19626961676560269
domContentLoaded153725241820285137
load154627181848308148
domInteractive14109372412
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 3.28 KiB (0.09%)
  • ui: 47 Bytes (0.00%)
  • common: -135 Bytes (-0.00%)

cryptotavares
cryptotavares previously approved these changes Sep 18, 2024
Copy link
Contributor

@cryptotavares cryptotavares left a comment

Choose a reason for hiding this comment

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

Left a minor comment but it is looking great 🚀 🔥

Comment on lines +5613 to +5614
* Intentionally not using immer as a temporary measure to avoid
* freezing the resulting state and requiring further fixes
Copy link
Contributor

Choose a reason for hiding this comment

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

was just thinking about why are we not using immer for applying the patches... I guess this explains it 😅

ui/store/actions.ts Outdated Show resolved Hide resolved
Copy link

sonarcloud bot commented Sep 19, 2024

@metamaskbot
Copy link
Collaborator

Builds ready [86ab71d]
Page Load Metrics (1935 ± 116 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint33425491864426205
domContentLoaded153525281893235113
load154225461935242116
domInteractive16110432512
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 3.28 KiB (0.08%)
  • ui: 47 Bytes (0.00%)
  • common: -153 Bytes (-0.00%)

@matthewwalsh0 matthewwalsh0 merged commit eecff31 into develop Sep 19, 2024
77 checks passed
@matthewwalsh0 matthewwalsh0 deleted the perf/redux-state-patches branch September 19, 2024 13:28
@github-actions github-actions bot locked and limited conversation to collaborators Sep 19, 2024
@metamaskbot metamaskbot added the release-12.6.0 Issue or pull request that will be included in release 12.6.0 label Sep 19, 2024
@metamaskbot metamaskbot added release-12.5.0 Issue or pull request that will be included in release 12.5.0 and removed release-12.6.0 Issue or pull request that will be included in release 12.6.0 labels Sep 29, 2024
@metamaskbot
Copy link
Collaborator

Missing release label release-12.5.0 on PR. Adding release label release-12.5.0 on PR and removing other release labels(release-12.6.0), as PR was added to branch 12.5.0 when release was cut.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
release-12.5.0 Issue or pull request that will be included in release 12.5.0 team-confirmations Push issues to confirmations team team-tiger Tiger team (for tech debt reduction + performance improvements)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants