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(lib): reduce backtrack when injecting esbuild helpers (fixes #8099) #8110



Copy link

@sapphi-red sapphi-red commented May 11, 2022


This line was consuming a lot of time.

res.code = res.code.replace(
(_, helpers, header, rest) => header + helpers + rest

This PR changes regex to reduce backtracks.

I confirmed #8099 works with this PR.
#8099 is about umd but it happens with iife in theory and this PR fixes it too.

fixes #8099

refs #7948

Additional context

rollup repl (named exports + iife)
rollup repl (named exports + umd)

What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@sapphi-red sapphi-red added p3-minor-bug 🔨 An edge case that only affects very specific usage (priority) feat: library mode labels May 11, 2022
bluwy approved these changes May 11, 2022
Copy link

@bluwy bluwy left a comment

Choose a reason for hiding this comment

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

Interesting. Didn't know this technique has perf implications too.

@antfu antfu merged commit b993c5f into vitejs:main May 11, 2022
9 checks passed
@antfu antfu changed the title fix(lib): reduce backtrack when injecting esbuild helpers (fixes #8099) perf(lib): reduce backtrack when injecting esbuild helpers (fixes #8099) May 11, 2022
Copy link

Thanks for the quick fix @sapphi-red! I also didn't know there was so much difference... wow.
Maybe we have some bottlenecks in other regexes that we have neglected so far 🤔
I'm preparing a list of PRs to backport to v2 so we can release 2.9.9 now

Copy link
Member Author

FYI, the following list is the step counts for some regexp.

target string: aa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)bb / aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(a)bbbbbbbbbbbbbbbbbbbbbbbbbbbb

This step count is by PCRE (not the one used in V8) so it is different from Node.js(V8) but it would be simillar.

If you change the target string to a(a)bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, \(.*\) takes 158 steps. I think this is what the issue faced to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
feat: library mode p3-minor-bug 🔨 An edge case that only affects very specific usage (priority)
None yet

Successfully merging this pull request may close these issues.

vite 2.6+ produces 50% larger output file for ESM
4 participants