Skip to content

[perf-agent:assets-controller/MulticallClient.ts] Encode/decode Multicall3 aggregate3 directly instead of via ethers Interface - #11100

Closed
Prithpal-Sooriya wants to merge 5 commits into
MetaMask:mainfrom
Prithpal-Sooriya:perf-agent/assets-controller-multicall
Closed

Prithpal-Sooriya wants to merge 5 commits into
MetaMask:mainfrom
Prithpal-Sooriya:perf-agent/assets-controller-multicall

Conversation

@Prithpal-Sooriya

Copy link
Copy Markdown
Contributor

Summary

  • encodeAggregate3, decodeAggregate3Response and decodeUint256 now lay out / read the fixed Multicall3 ABI shapes (aggregate3((address,bool,bytes)[]), its (bool,bytes)[] output, and the balanceOf uint256 word) directly — string word layout + a BigInt word read — instead of going through the ethers Interface / Reader machinery on every batch.
  • The per-account balance-call-data cache also remembers the original-case spelling of each account, so repeated lookups hit without re-lowercasing; constant encode words (0x60 offset, 0x1, address padding) are hoisted out of the per-call loop.
  • No exported behaviour changes: signatures, calldata bytes, decoded values, error/throw semantics on malformed data, retry and fallback paths are all unchanged.

Draft / DO-NOT-MERGE — generated by the assets performance research agent; on-device confirmation still required.

Why

From the MetaMask Mobile Hermes profile (scheduled perf run 39731720623 at commit 9b830f28), this path is the dominant assets-owned cost in the regressed Asset View scenario (tracked in #37813: 20.4s vs 11.0s baseline, 1.86×) and 8 other scenarios (4.2–7.1s each):

batchBalanceOf -> reduceInBatchesSerially (300/batch)
  -> buildAggregate3BalanceCalls   (per-account callData cache, warm)
  -> encodeAggregate3              ~5.6s incl on device
  -> decodeAggregate3Response      ~5.2s incl on device
  -> decodeUint256                 ~1.3s incl on device

Every encode/decode went through ethers Interface parsing and dynamic coder dispatch; the shapes here are fixed, so the same bytes can be produced/read with direct word arithmetic.

Bench

The real MulticallClient source is bundled to a single Hermes script (MetaMask Mobile hermes-engine CLI, -O) that replays a locked parity corpus — 14 batches covering mixed success/failure, native+ERC-20, empty/short/odd/oversized return data, mismatched counts, malformed hex, retry (3 attempts) and fallbackToSingleCalls semantics — then runs a timing workload of balance calls (~15% native) with a warm callData cache. Median of 3 process runs; every run must pass SELF_CHECK=OK (byte-identical provider call sequence + field-identical results) and the full MulticallClient vitest suite, including the existing "identical calldata to regular Ethers implementation" test.

calls before after per call before → after speed-up
600 158 ms 5 ms 263 µs → 8.3 µs 31.6×
2400 601 ms 18.5 ms 250 µs → 7.7 µs 32.5×
4800 1201 ms 36 ms 250 µs → 7.5 µs 33.4×

Per-call cost is flat across the sweep, so the win is not a bench-shape artifact. The .auto/ directory in this branch contains the bench (harness, generator, corpus) for reproducibility.

Testing

  • yarn vitest run packages/assets-controller/src/data-sources/evm-rpc-services/clients/MulticallClient.test.ts --coverage.enabled=false — 38/38 pass, incl. byte-identical calldata vs ethers and count-mismatch error message.
  • Full package suite: yarn vitest run packages/assets-controller --coverage.enabled=false — 1325 passed, 5 pre-existing expected-fails, no type errors.
  • BigInt is standard JS (available on the RN Hermes runtime) and adds no new dependency.

🤖 Generated with Claude Code

encodeAggregate3, decodeAggregate3Response and decodeUint256 now lay out
and read the fixed aggregate3/(bool,bytes)[]/uint256 shapes directly
(string word layout + BigInt word read) instead of going through ethers
Interface/Reader machinery on every call. Byte-identical calldata and
values, same throw behaviour on malformed shapes, enforced by the full
MulticallClient test suite.

Bench (MetaMask Mobile hermes CLI, 2400 balance calls): 601ms -> 20ms.
The balanceCallData cache now also remembers the original-case spelling
of each account, so repeated lookups hit without allocating a lowercase
key; encodeAggregate3 hoists the constant words (allowFailure 1, 0x60
offset, address padding) out of the per-call loop.

Bench: 20ms -> 18ms (2400 balance calls, hermes CLI).
@Prithpal-Sooriya Prithpal-Sooriya added DO-NOT-MERGE area-performance Issues relating to slowness of app, cpu usage, and/or blank screens. assets-performance-agent Opened by the assets performance autoresearch agent labels Oct 26, 2026
@Prithpal-Sooriya

Copy link
Copy Markdown
Contributor Author

thanks for this finding - but we already found insights here: #11010

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-performance Issues relating to slowness of app, cpu usage, and/or blank screens. assets-performance-agent Opened by the assets performance autoresearch agent DO-NOT-MERGE

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant