Skip to content

chore: faster sentry + use native polyfills#16025

Merged
Cal-L merged 2 commits into
MetaMask:margelo/faster-sentryfrom
margelo:chore/faster-sentry
Jul 8, 2025
Merged

chore: faster sentry + use native polyfills#16025
Cal-L merged 2 commits into
MetaMask:margelo/faster-sentryfrom
margelo:chore/faster-sentry

Conversation

@Nodonisko

@Nodonisko Nodonisko commented Jun 3, 2025

Copy link
Copy Markdown
Contributor

Description

This PR could speed up Sentry events processing around up to ~15x. I am not able to consistently reproduce slow Sentry (it depends on how much breadcrumbs and extra data are collected), but performance gain helps even when sending relatively small amounts of extra data. For small event time needed dropped from 100ms to under <10ms. For big events I randomly encountered it dropped from 1s to <100ms.

In future this patch for Sentry might not be necessary, because Sentry team is working on fix in library itself, but it might take some time.

There is also second part of PR modifying metro.config.js to use faster native polyfills, this could speed up app in some other places.

Related issues

Fixes: #15560

Manual testing steps

I would recommend to run smoke test across whole app because this polyfilled libraries could be used in many places eveywhere in the app.

I would also recommend to verify that events from Sentry are received in Sentry dashboard.

Screenshots/Recordings

Before

Sending big Sentry event before changes on emulator takes around 380ms. For real slow device it's around 800ms.

Screenshot 2025-06-03 at 16 02 24

For small events it's around 100ms on real device.

After

After changes it takes around 40ms to send big event and around 80ms on real device.

Screenshot 2025-06-03 at 16 00 27

For small events on real device it's around 10ms.

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.

@Nodonisko Nodonisko requested a review from a team June 3, 2025 14:09
@github-actions

github-actions Bot commented Jun 3, 2025

Copy link
Copy Markdown
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.

@socket-security

socket-security Bot commented Jun 3, 2025

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Updatednpm/​@​craftzdog/​react-native-buffer@​6.0.5 ⏵ 6.1.010010010079100
Updatednpm/​react-native-quick-base64@​2.0.8 ⏵ 2.2.0100100100 +1580100
Updatednpm/​react-native-quick-crypto@​0.7.15 ⏵ 0.7.1310010010095100

View full report

Comment thread metro.config.js Outdated
Comment thread patches/@sentry+react-native+6.10.0.patch Outdated
Comment thread metro.config.js Outdated
@Nodonisko Nodonisko force-pushed the chore/faster-sentry branch from 12ce2f0 to 7cdf0c0 Compare June 4, 2025 11:23
@Nodonisko

Copy link
Copy Markdown
Contributor Author

@Cal-L Here is Sentry issue getsentry/sentry-react-native#4884

@darkwing darkwing added the area-performance Issues relating to slowness of app, cpu usage, and/or blank screens. label Jun 24, 2025
@Nodonisko Nodonisko force-pushed the chore/faster-sentry branch from 2ff1155 to 93036e9 Compare July 8, 2025 14:47
cursor[bot]

This comment was marked as outdated.

@Nodonisko

Copy link
Copy Markdown
Contributor Author

I removed some polyfills from metro.config.js that was not relevant for this PR and also rebased branch to latest main.

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bug: UTF-8 Encoding Issues in `utf8ToBytes`

The utf8ToBytes function's rewrite introduces three main issues:

  1. Breaking API Change: The function now returns a Uint8Array instead of a plain JavaScript Array, which can break calling code expecting Array.prototype methods.
  2. UTF-8 Corruption on Truncation: Truncating encoded bytes using slice(0, units) can split multi-byte UTF-8 characters, leading to invalid sequences. The original implementation carefully avoided this.
  3. Incorrect units Parameter Handling: The units parameter's handling for falsy values is inconsistent with the original, causing false and empty string ("") to coerce to 0 instead of Infinity. Additionally, a units value of 0 is still incorrectly treated as Infinity rather than returning an empty result.

patches/@sentry+react-native+6.10.0.patch#L85-L123

- throw new Error('Invalid code point');
+export function utf8ToBytes(string, unitsInput) {
+ let units;
+
+ // Mimic the polyfill's `units = units || Infinity` behavior for common falsy values
+ // that would lead to `Infinity` in the original.
+ if (unitsInput === undefined ||
+ unitsInput === null ||
+ unitsInput === 0 || // Original `|| Infinity` makes 0 effectively Infinity
+ (typeof unitsInput === 'number' && Number.isNaN(unitsInput))) {
+ units = Infinity;
+ } else {
+ units = Number(unitsInput); // Coerce to number (e.g., if it was a string "10")
+ if (Number.isNaN(units)) { // If coercion results in NaN (e.g., from "abc")
+ units = Infinity;
}
}
- return bytes;
+
+ const encoder = new TextEncoder(); // UTF-8 by default
+ const uint8Array = encoder.encode(string);
+
+ let finalUint8Array;
+
+ if (units < 0) {
+ // If units was specified as a negative number not caught by the Infinity conditions.
+ // The polyfill would also likely break immediately or produce no bytes.
+ finalUint8Array = new Uint8Array(0);
+ } else if (units !== Infinity && uint8Array.length > units) {
+ // Truncate if the encoded length is greater than the specified units.
+ // Ensure units is not negative for slice, though above logic should handle most.
+ finalUint8Array = uint8Array.slice(0, Math.max(0, units));
+ } else {
+ // No truncation needed or units is Infinity
+ finalUint8Array = uint8Array;
+ }
+
+ return finalUint8Array
}

Fix in CursorFix in Web


Was this report helpful? Give feedback by reacting with 👍 or 👎

@Cal-L Cal-L changed the base branch from main to margelo/faster-sentry July 8, 2025 17:22
@Cal-L

Cal-L commented Jul 8, 2025

Copy link
Copy Markdown
Contributor

Merging into feature branch for running tests

@Cal-L Cal-L left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@Cal-L Cal-L merged commit 95c4539 into MetaMask:margelo/faster-sentry Jul 8, 2025
5 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 8, 2025
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. external-contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[EPIC]: Margelo platform performance optimizations

5 participants