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: Remove costly reduce operation for generating Engine context #12374

Merged

Conversation

MajorLift
Copy link
Contributor

@MajorLift MajorLift commented Nov 21, 2024

Overview

Optimizes an expensive operation from the Engine initialization process.

A custom span for measuring this process was added in #11579.

Motivation

Currently, the Engine's context object is created using a Array.prototype.reduce() operation on a large controllers array. This reduce call is also written to create a temporary object on every iteration.

Benchmarks show that this particular implementation of reduce runs significantly slower than other means of creating an object, and compared to reduce calls that are written to re-use the output object the difference is in four orders of magnitude (1555.6 Ops/sec vs. 0.2 Ops/sec).

https://www.measurethat.net/Benchmarks/Show/21554/0/objectfromentries-vs-reduce-vs-assign

A simple fix would be to refactor the reduce operation so it doesn't rely on temporary objects and spreading:

diff --git a/app/core/Engine.ts b/app/core/Engine.ts
index decf28da51..1ba63ed106 100644
--- a/app/core/Engine.ts
+++ b/app/core/Engine.ts
@@ -1738,10 +1738,10 @@ export class Engine {
       this.controllerMessenger,
     );
     this.context = controllers.reduce<Partial<typeof this.context>>(
-      (context, controller) => ({
-        ...context,
-        [controller.name]: controller,
-      }),
+      (context, controller) => {
+        context[controller.name] = controller;
+        return context;
+      },
       {},
     ) as typeof this.context;

However, we can also avoid the cost of running reduce altogether simply by creating a controller name-keyed object to assign to the Engine's context field. This is also the approach taken in the Extension MetamaskController.

Description

  • Replaces controllers array in the Engine class with a corresponding object that is directly assigned to context.
  • Optimize Object.values for deriving ComposableController input with Object.keys and reduce (re-uses object).

    https://www.measurethat.net/Benchmarks/Show/7173/0/objectvalues-vs-reduce

  • Removes for-loop that sets initial state for V1 controllers now that all mobile controllers have been updated to V2.

Related issues

Manual testing steps

Screenshots/Recordings

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.

…s an object

Adds `Object.values` operation for deriving ComposableController input array
@MajorLift MajorLift added area-performance Issues relating to slowness of app, cpu usage, and/or blank screens. team-tiger Tiger team (for tech debt reduction + performance improvements) labels Nov 21, 2024
@MajorLift MajorLift self-assigned this Nov 21, 2024
Copy link
Contributor

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.

@MajorLift MajorLift added the Run Smoke E2E Triggers smoke e2e on Bitrise label Nov 21, 2024
Copy link
Contributor

https://bitrise.io/ Bitrise

🔄🔄🔄 pr_smoke_e2e_pipeline started on Bitrise...🔄🔄🔄

Commit hash: 2c34547
Build link: https://app.bitrise.io/app/be69d4368ee7e86d/pipelines/d2c35a05-5104-4eb1-860d-0ff113c32b79

Note

  • This comment will auto-update when build completes
  • You can kick off another pr_smoke_e2e_pipeline on Bitrise by removing and re-applying the Run Smoke E2E label on the pull request

@MajorLift MajorLift force-pushed the jongsun/perf/engine/241121-remove-costly-reduce-operation branch from 2c34547 to 8774483 Compare November 21, 2024 15:57
@MajorLift MajorLift added Run Smoke E2E Triggers smoke e2e on Bitrise and removed Run Smoke E2E Triggers smoke e2e on Bitrise labels Nov 21, 2024
Copy link
Contributor

https://bitrise.io/ Bitrise

🔄🔄🔄 pr_smoke_e2e_pipeline started on Bitrise...🔄🔄🔄

Commit hash: 8774483
Build link: https://app.bitrise.io/app/be69d4368ee7e86d/pipelines/8fe73c04-76bb-4248-9834-03d19340af7a

Note

  • This comment will auto-update when build completes
  • You can kick off another pr_smoke_e2e_pipeline on Bitrise by removing and re-applying the Run Smoke E2E label on the pull request

@MajorLift MajorLift added Run Smoke E2E Triggers smoke e2e on Bitrise and removed Run Smoke E2E Triggers smoke e2e on Bitrise labels Nov 21, 2024
Copy link
Contributor

github-actions bot commented Nov 21, 2024

https://bitrise.io/ Bitrise

✅✅✅ pr_smoke_e2e_pipeline passed on Bitrise! ✅✅✅

Commit hash: 0c662ae
Build link: https://app.bitrise.io/app/be69d4368ee7e86d/pipelines/11d7cf96-828d-4136-8582-ed7a120b0624

Note

  • You can kick off another pr_smoke_e2e_pipeline on Bitrise by removing and re-applying the Run Smoke E2E label on the pull request

@codecov-commenter
Copy link

Codecov Report

Attention: Patch coverage is 83.33333% with 1 line in your changes missing coverage. Please review.

Project coverage is 56.46%. Comparing base (22a4989) to head (0c662ae).
Report is 29 commits behind head on main.

Files with missing lines Patch % Lines
app/core/Engine.ts 83.33% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #12374      +/-   ##
==========================================
+ Coverage   56.41%   56.46%   +0.04%     
==========================================
  Files        1797     1808      +11     
  Lines       40586    40724     +138     
  Branches     5097     5131      +34     
==========================================
+ Hits        22896    22993      +97     
- Misses      16134    16159      +25     
- Partials     1556     1572      +16     

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


🚨 Try these New Features:

@MajorLift MajorLift marked this pull request as ready for review November 21, 2024 16:42
@MajorLift MajorLift requested a review from a team as a code owner November 21, 2024 16:42
@MajorLift MajorLift requested a review from Cal-L November 21, 2024 16:42
Copy link
Contributor

@tommasini tommasini left a comment

Choose a reason for hiding this comment

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

LOVE IT! LGTM! So cool that we now only have base controllers v2 controllers!

Copy link
Contributor

@tommasini tommasini left a comment

Choose a reason for hiding this comment

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

LOVE IT! LGTM! So cool that we now only have base controllers v2 controllers!

Copy link
Contributor

@Cal-L Cal-L 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 comment

Copy link
Contributor

@Cal-L Cal-L left a comment

Choose a reason for hiding this comment

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

LGTM

@MajorLift MajorLift added this pull request to the merge queue Nov 22, 2024
@MajorLift MajorLift removed this pull request from the merge queue due to a manual request Nov 22, 2024
@MajorLift MajorLift added this pull request to the merge queue Nov 22, 2024
Merged via the queue into main with commit 30d8a30 Nov 22, 2024
57 checks passed
@MajorLift MajorLift deleted the jongsun/perf/engine/241121-remove-costly-reduce-operation branch November 22, 2024 10:03
@github-actions github-actions bot locked and limited conversation to collaborators Nov 22, 2024
@metamaskbot metamaskbot added the release-7.37.0 Issue or pull request that will be included in release 7.37.0 label Nov 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-performance Issues relating to slowness of app, cpu usage, and/or blank screens. release-7.37.0 Issue or pull request that will be included in release 7.37.0 Run Smoke E2E Triggers smoke e2e on Bitrise team-tiger Tiger team (for tech debt reduction + performance improvements)
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

5 participants