Skip to content

perf: Optimized ExpensiMark parser for long text - #937

Merged
JS00001 merged 11 commits into
Expensify:mainfrom
Uzaifm127:fix-95210-optimize-expensimark-parser
Aug 28, 2026
Merged

JS00001 merged 11 commits into
Expensify:mainfrom
Uzaifm127:fix-95210-optimize-expensimark-parser

Conversation

@Uzaifm127

@Uzaifm127 Uzaifm127 commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Explanation of Change

ExpensiMark.replace() runs the autolink, bold, and strikethrough regexes against the complete input.

These regexes contain checks that may scan the same text several times while finding a match. This becomes slow when the composer contains a long message, especially on iOS Safari.

This PR first scans the input for possible URL, bold, and strikethrough parts. It then runs the existing ExpensiMark regex only on those smaller parts.

The existing regex still decides whether each part is valid and performs the replacement. This means we are optimizing the existing parser instead of adding another Markdown or URL parser.

For a 14,501-character input, the local App benchmark showed that ExpensiMark.replace() went from around 350 ms to 0-1 ms. Typing and editing the same long input remained smooth on Web and iOS Safari.

Fixed Issues

$ #95210
PROPOSAL: #95210 (comment)

Tests

  1. Go to staging.new.expensify.com or open Expensify App
  2. Login with any account
  3. Go to any chat
  4. Paste any text containing markdown (bold, italic and autolink) and 14000+ characters in the composer.
  5. Try to write something after pasting.
  6. Verify that the typing experience is smooth and it doesn't lag while typing.
  7. Verify that the markdown is preserved when we type after 14k text in the composer.
  • Verify that no errors appear in the JS console

Offline tests

Same as Test

QA Steps

Same as Test

  • Verify that no errors appear in the JS console

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android: Native
    • Android: mWeb Chrome
    • iOS: Native
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))
  • If new assets were added or existing ones were modified, I verified that:
    • The assets are optimized and compressed (for SVG files, run npm run compress-svg)
    • The assets load correctly across all supported platforms.
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • I added unit tests for any new feature or bug fix in this PR to help automatically prevent regressions in this user flow.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.

Screenshots/Videos

Android: Native
Android-native.mov
Android: mWeb Chrome
Android-chrome.mp4
iOS: Native
ios-native.mp4
iOS: mWeb Safari
ios-safari.mp4
MacOS: Chrome / Safari
macOS.mp4

@github-actions

github-actions Bot commented Aug 3, 2026

Copy link
Copy Markdown

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@Uzaifm127

Copy link
Copy Markdown
Contributor Author

I have read the CLA Document and I hereby sign the CLA

exfy-clabot Bot added a commit to Expensify/CLA that referenced this pull request Aug 3, 2026
@Uzaifm127 Uzaifm127 changed the title Optimized ExpensiMark parser for long text [WIP] Optimized ExpensiMark parser for long text Aug 4, 2026
@Uzaifm127

Copy link
Copy Markdown
Contributor Author

While testing some more parser cases and comparing the result with staging, I found the following two cases and want to confirm whether we should handle them in this PR or separately.

1. Non-ASCII domain

For this input:

https://münich.com

The behavior is already broken on staging. On staging, https://m and nich.com become two separate links while ü remains plain text. With the optimized parser, the text until ü remains plain and only nich.com becomes a link.

The ASCII/punycode version works correctly on both staging and dev:

https://xn--mnich-kva.com/

Correctly supporting the Unicode version requires converting the hostname to punycode for validation while keeping the original Unicode hostname visible in the message. This looks like a different ExpensiMark issue and should not be mixed with this performance PR because staging is already broken for this case too.

@JS00001 I already have the RCA and a possible solution locally for this issue. Can we cover the Unicode domain support in a follow-up issue?

ASCII.mov

2. URL-looking video label

For this input:

![example.com](https://example.com/video.mp4)

I wasn't able to see any broken behavior on real devices because the composer looks correct and the sent message still renders as a video. However, direct parser testing through terminal shows that the output is different.

Existing parser output:

<video ...>example.com</video>

Optimized parser output:

Note: I only got the broken parser output in terminal, in Web or native I got the correct output.

<video ...><a ...>example.com</a></video>

The fix is small. The URL scanner can skip everything between <video> and </video>, just like it already skips links and code blocks.

Since this parser output difference is introduced by the optimization, I'm not sure whether we should fix this case in the current PR or not because I wasn't get any regression behaviour while testing manually, but can you confirm? @JS00001 @linhvovan29546

I've used this to technically reproduce this case:

npm run build

node - <<'NODE'
const ExpensiMark = require('./dist/ExpensiMark').default;
const parser = new ExpensiMark();

console.log(
    parser.replace('![example.com](https://example.com/video.mp4)'),
);
NODE
video-label-on-ios-native.mov
video-label-on-web.mov
video-label-reproduced-technically.mov

@Uzaifm127
Uzaifm127 marked this pull request as ready for review August 7, 2026 07:15
@Uzaifm127
Uzaifm127 requested a review from a team as a code owner August 7, 2026 07:15
@Uzaifm127 Uzaifm127 changed the title [WIP] Optimized ExpensiMark parser for long text fix: Optimized ExpensiMark parser for long text Aug 7, 2026
@melvin-bot
melvin-bot Bot requested review from Valforte and removed request for a team August 7, 2026 07:15
@Uzaifm127

Copy link
Copy Markdown
Contributor Author

@linhvovan29546

I'm getting a wierd behaviour only on Android native (as per my findings, Its not related to ExpensiMark)

When we paste a long text with having *Bold* then we get a delay in typing (Not a lag) or latency but when we do similar thing with strikethrough or autolink then we don't get any delay.

Also if we paste long text (either plain text or having markdown such as autolink or strikethrough) and then we type Bold, italic, inline code then we don't get this typing delay and the typing seems to be smoother.

We are getting the typing delay when we paste the long text with:

  • Bold
  • italic
  • inline code

We don't get this delay with autolink or strikethrough

I found the root cause for this behaviour and I found StyleSpan in Android was causing this behaviour but I'm not really sure.

wierd-behaviour-not-related-to-ExpensiMark.mp4

@JS00001

JS00001 commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Yeah if the issue exists on staging, lets do a follow up

Comment thread lib/ExpensiMark.ts Outdated
Comment thread lib/ExpensiMark.ts Outdated
Comment thread lib/ExpensiMark.ts Outdated
Comment thread lib/ExpensiMark.ts Outdated
@Uzaifm127

Copy link
Copy Markdown
Contributor Author

I'll look into the review comments on tomorrow. Thanks

@Uzaifm127

Copy link
Copy Markdown
Contributor Author

@JS00001 Resolved the review comments:

  1. Added constants for the ASCII ranges and reused the same function in isWordCharacter() and the URL scanner.
  2. Added one list for the supported protocols and used its length instead of hard-coded numbers.

@JS00001
JS00001 removed the request for review from Valforte August 10, 2026 17:44
@linhvovan29546

Copy link
Copy Markdown

Reviewing...

@linhvovan29546

Copy link
Copy Markdown

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

To use Codex here, create a Codex account and connect to github.

@linhvovan29546

Copy link
Copy Markdown

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1cf4123014

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread lib/ExpensiMark.ts Outdated
Comment thread lib/ExpensiMark.ts Outdated
Comment thread lib/ExpensiMark.ts Outdated
Comment thread lib/ExpensiMark.ts
Comment thread lib/ExpensiMark.ts Outdated
Comment thread lib/ExpensiMark.ts Outdated
@Uzaifm127

Copy link
Copy Markdown
Contributor Author

I'll go through the suggestions today. Thanks

@quinthar quinthar removed the #quality label Aug 13, 2026
Include @, *, _, and ~ before a domain when passing URL candidates to
the existing parser.

Keep markers inside code and links in the Markdown scan, but mark them
as protected so they cannot create bold or strikethrough formatting.
@Uzaifm127

Copy link
Copy Markdown
Contributor Author

@linhvovan29546

While reviewing this PR locally, I found and fixed two regressions:

  1. A formatted domain after @, such as @*example.com*, was incorrectly autolinked because the scanner moved across the Markdown marker before checking for @. The URL candidate now keeps @, *, _, and ~ together, so the existing URL logic receives the same context as before.

    1. Open any chat.
    2. Paste @*example.com* into the composer.
    3. Check that example.com is bold and is not shown as a blue link.
    4. Send the message and check the sent message too.
    issue-1.mov
  2. Markers inside protected content, such as `~` ~strike~ or [~](https://example.com) ~strike~, were removed from the marker sequence. This could make the scanner select a different Markdown pair than the original parser. The scanner now keeps those markers in order and records whether they are protected, so they can affect pairing without being formatted incorrectly.

    1. Open any chat.
    2. Paste `~` ~strike~ into the composer.
    3. Check that the first ~ stays inside the code block and only strike is strikethrough.
    4. Send the message and check the sent message.
    5. Repeat the same steps with [~](https://example.com) ~strike~.
    6. Check that ~ remains the link text and only strike is strikethrough.
    issue-2.mov

These two suggestions have been fixed in this commit.

@linhvovan29546

Copy link
Copy Markdown

Reviewing again

@linhvovan29546 linhvovan29546 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could you please check and update the comment formatting for all the new functions? Thanks!

Comment thread lib/ExpensiMark.ts Outdated
Comment thread lib/ExpensiMark.ts
Comment thread lib/ExpensiMark.ts
Comment thread lib/ExpensiMark.ts Outdated
Comment thread lib/ExpensiMark.ts Outdated
@linhvovan29546

linhvovan29546 commented Aug 18, 2026

Copy link
Copy Markdown

Reviewer Checklist

  • I have verified the author checklist is complete (all boxes are checked off).
  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the steps for local testing are in the Tests section
    • I verified the steps for Staging and/or Production testing are in the QA steps section
    • I verified the steps cover any possible failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
  • I checked that screenshots or videos are included for tests on all platforms
  • I included screenshots or videos for tests on all platforms
  • I verified that the composer does not automatically focus or open the keyboard on mobile unless explicitly intended. This includes checking that returning the app from the background does not unexpectedly open the keyboard.
  • I verified tests pass on all platforms & I tested again on:
    • Android: HybridApp
    • Android: mWeb Chrome
    • iOS: HybridApp
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • If there are any errors in the console that are unrelated to this PR, I either fixed them (preferred) or linked to where I reported them in Slack
  • I verified proper code patterns were followed (see Reviewing the code)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I verified that this PR follows the guidelines as stated in the Review Guidelines
  • I verified other components that can be impacted by these changes have been tested, and I retested again (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar have been tested & I retested again)
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG)
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • For any bug fix or new feature in this PR, I verified that sufficient unit tests are included to prevent regressions in this flow.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR reviewer checklist, including those that don't apply to this PR.

Screenshots/Videos

Android: HybridApp
telegram-cloud-document-5-6061894507744471517.mp4
Android: mWeb Chrome
telegram-cloud-document-5-6061894507744471518.mp4
iOS: HybridApp
Screen.Recording.2026-08-18.at.18.20.34.mov
iOS: mWeb Safari
Screen.Recording.2026-08-18.at.18.44.15.mov
MacOS: Chrome / Safari
Screen.Recording.2026-08-18.at.18.40.33.mov

@Uzaifm127

Copy link
Copy Markdown
Contributor Author

I'll look into the new suggestion/comments tomorrow, thanks.

@Uzaifm127

Copy link
Copy Markdown
Contributor Author

@linhvovan29546 I've added and refined the comments in the added functions, moved onClose outside from replaceMarkdownCandidates.

Could you please review, thanks.

Comment thread lib/ExpensiMark.ts
Comment thread lib/ExpensiMark.ts Outdated
Comment thread lib/ExpensiMark.ts
Comment thread lib/ExpensiMark.ts
@Uzaifm127

Uzaifm127 commented Aug 20, 2026

Copy link
Copy Markdown
Contributor Author

I'll go through each suggestion on Friday (today).

Looking into the suggestions...

@Uzaifm127

Copy link
Copy Markdown
Contributor Author

@JS00001 I've replied to all the changes requested by you.

Please don't merge this PR as I'm doing the testing regarding this Android slowdown (I'm 100% sure this is a different issue and should also be covered in a follow up but I'll provide the conclusion) soon.

TY!

@JS00001

JS00001 commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

lmk when this is ready please

@JS00001

JS00001 commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

Any updates?

@Uzaifm127

Uzaifm127 commented Aug 26, 2026

Copy link
Copy Markdown
Contributor Author

Will finalize by EOD

@Uzaifm127

Copy link
Copy Markdown
Contributor Author

@JS00001

All clear from my side now because the Android typing delay seems to be caused by other reason (which we will find out, though I tried to find the reason for that but it needs deeper investigation). It isn't related to ExpensiMark because I also got that delay when I pasted almost 14400 characters text without markdown.

We will figure it out in a follow PR/issue, for now I don't think it should block the parser optimization for other platforms.

Sorry for the delay, since I was also doing the codex review locally for this PR, codex gave some regressions but when I tested manually then they weren't reproducible.

TY!

@JS00001

JS00001 commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

@Uzaifm127 can you take a look at @linhvovan29546 's comments please

@Uzaifm127

Copy link
Copy Markdown
Contributor Author

@JS00001 We already addressed all comments by @linhvovan29546

I forgot to resolve those comments, my bad

@JS00001 JS00001 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.

one more question

Comment thread lib/ExpensiMark.ts
Comment thread lib/ExpensiMark.ts
@JS00001
JS00001 merged commit e3f827c into Expensify:main Aug 28, 2026
8 of 13 checks passed
@JS00001

JS00001 commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

@Uzaifm127 lets create a PR to bump the version in e/app please

@os-botify

os-botify Bot commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

🚀 Published to npm in 2.0.201 🎉

@Uzaifm127

Copy link
Copy Markdown
Contributor Author

I'll raise a bump PR in App soon. Thanks

@Uzaifm127 Uzaifm127 changed the title fix: Optimized ExpensiMark parser for long text perf: Optimized ExpensiMark parser for long text Aug 31, 2026
@Uzaifm127
Uzaifm127 deleted the fix-95210-optimize-expensimark-parser branch September 15, 2026 06:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants