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: improve regexp performance by using non-capturing groups #58551

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

antfu
Copy link

@antfu antfu commented May 16, 2024

This PR is a performance improvement to TS cli/compiler for RegExps of file path matching. Currently, it uses dynamically constructed regex with groups to match multiple path patterns. While all RegExp groups are capturing, but we only use regex.test(file) function, which doesn't consume those groups at all.

I am building a RegExp monitor regex-doctor and found those regexes are taking quite some time to do the matches, even in a very small project. I imagine the cost of running those regexes could scale up with the package size.

For example, in my testing repo, this regex cost a total 8.48ms to match against 17 paths (and crates a RegexMatchArray with 233 unconsumed groups in this case)

Screenshot 2024-05-16 at 17 03 09

After this PR, the time cost to match with the exact same inputs becomes 3.36ms (60% faster on regex matching).

Screenshot 2024-05-16 at 17 01 48

Can you help to initiate the bot to run a proper benchmark and see how it would help the overall performance? Thanks.

@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label May 16, 2024
@typescript-bot
Copy link
Collaborator

This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise.

@jakebailey
Copy link
Member

Funnily I was already about to send a PR similar to this one using the findings from https://www.npmjs.com/package/eslint-plugin-regexp.

@typescript-bot test it

@typescript-bot
Copy link
Collaborator

typescript-bot commented May 16, 2024

Starting jobs; this comment will be updated as builds start and complete.

Command Status Results
test top400 ✅ Started ✅ Results
user test this ✅ Started ✅ Results
run dt ✅ Started ✅ Results
perf test this faster ✅ Started 👀 Results

@jakebailey
Copy link
Member

Actually, this PR is perpendicular as it affects dynamically created regexes. Very nice!

@typescript-bot
Copy link
Collaborator

@jakebailey Here are the results of running the user tests comparing main and refs/pull/58551/merge:

Everything looks good!

@typescript-bot
Copy link
Collaborator

Hey @jakebailey, the results of running the DT tests are ready.

Everything looks the same!

You can check the log here.

@typescript-bot
Copy link
Collaborator

@jakebailey
The results of the perf run you requested are in!

Here they are:

tsc

Comparison Report - baseline..pr
Metric baseline pr Delta Best Worst p-value
Compiler-Unions - node (v18.15.0, x64)
Errors 30 30 ~ ~ ~ p=1.000 n=6
Symbols 62,154 62,154 ~ ~ ~ p=1.000 n=6
Types 50,248 50,248 ~ ~ ~ p=1.000 n=6
Memory used 193,485k (± 0.96%) 193,449k (± 0.97%) ~ 192,199k 195,880k p=0.378 n=6
Parse Time 1.55s (± 1.62%) 1.55s (± 1.98%) ~ 1.51s 1.58s p=0.745 n=6
Bind Time 0.86s (± 0.63%) 0.87s (± 0.94%) ~ 0.86s 0.88s p=0.859 n=6
Check Time 11.31s (± 0.36%) 11.34s (± 0.24%) ~ 11.32s 11.39s p=0.172 n=6
Emit Time 3.15s (± 0.43%) 3.14s (± 0.79%) ~ 3.09s 3.16s p=0.934 n=6
Total Time 16.87s (± 0.24%) 16.90s (± 0.17%) ~ 16.86s 16.94s p=0.198 n=6
angular-1 - node (v18.15.0, x64)
Errors 5 5 ~ ~ ~ p=1.000 n=6
Symbols 944,110 944,110 ~ ~ ~ p=1.000 n=6
Types 407,140 407,140 ~ ~ ~ p=1.000 n=6
Memory used 1,222,113k (± 0.00%) 1,222,105k (± 0.00%) ~ 1,222,016k 1,222,148k p=0.936 n=6
Parse Time 6.77s (± 0.40%) 6.79s (± 0.65%) ~ 6.73s 6.84s p=0.467 n=6
Bind Time 1.88s (± 0.58%) 1.87s (± 0.52%) ~ 1.86s 1.89s p=0.143 n=6
Check Time 31.24s (± 0.52%) 31.31s (± 0.28%) ~ 31.23s 31.43s p=0.423 n=6
Emit Time 14.71s (± 0.31%) 14.71s (± 0.68%) ~ 14.58s 14.84s p=0.748 n=6
Total Time 54.59s (± 0.32%) 54.69s (± 0.29%) ~ 54.43s 54.92s p=0.748 n=6
mui-docs - node (v18.15.0, x64)
Errors 5 5 ~ ~ ~ p=1.000 n=6
Symbols 1,964,178 1,964,178 ~ ~ ~ p=1.000 n=6
Types 819,287 819,287 ~ ~ ~ p=1.000 n=6
Memory used 1,849,617k (± 0.00%) 1,849,640k (± 0.00%) ~ 1,849,605k 1,849,691k p=0.378 n=6
Parse Time 6.78s (± 0.49%) 6.78s (± 0.28%) ~ 6.76s 6.81s p=0.808 n=6
Bind Time 2.29s (± 0.55%) 2.30s (± 1.39%) ~ 2.27s 2.36s p=0.858 n=6
Check Time 58.56s (± 0.37%) 58.92s (± 0.40%) +0.36s (+ 0.62%) 58.72s 59.37s p=0.031 n=6
Emit Time 0.14s 0.14s (± 2.88%) ~ 0.14s 0.15s p=0.405 n=6
Total Time 67.77s (± 0.29%) 68.13s (± 0.34%) +0.37s (+ 0.54%) 67.96s 68.57s p=0.031 n=6
self-build-src - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 1,221,231 1,221,231 ~ ~ ~ p=1.000 n=6
Types 259,523 259,525 +2 (+ 0.00%) ~ ~ p=0.001 n=6
Memory used 2,337,530k (± 0.02%) 2,362,787k (± 2.59%) ~ 2,336,844k 2,487,867k p=0.378 n=6
Parse Time 5.00s (± 1.14%) 5.00s (± 0.90%) ~ 4.95s 5.05s p=0.810 n=6
Bind Time 1.88s (± 0.75%) 1.88s (± 0.43%) ~ 1.87s 1.89s p=0.677 n=6
Check Time 33.78s (± 0.24%) 33.80s (± 0.29%) ~ 33.66s 33.91s p=0.630 n=6
Emit Time 2.65s (± 1.60%) 2.62s (± 2.99%) ~ 2.53s 2.71s p=0.748 n=6
Total Time 43.31s (± 0.34%) 43.33s (± 0.37%) ~ 43.06s 43.50s p=0.748 n=6
self-build-src-public-api - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 1,221,231 1,221,231 ~ ~ ~ p=1.000 n=6
Types 259,523 259,525 +2 (+ 0.00%) ~ ~ p=0.001 n=6
Memory used 2,413,565k (± 0.02%) 2,413,646k (± 0.04%) ~ 2,412,411k 2,414,535k p=0.689 n=6
Parse Time 6.19s (± 0.79%) 6.18s (± 1.14%) ~ 6.12s 6.28s p=0.575 n=6
Bind Time 2.00s (± 0.93%) 2.03s (± 0.52%) +0.02s (+ 1.00%) 2.01s 2.04s p=0.040 n=6
Check Time 40.16s (± 0.25%) 40.11s (± 0.34%) ~ 39.88s 40.25s p=0.521 n=6
Emit Time 3.11s (± 3.37%) 3.12s (± 2.92%) ~ 3.00s 3.26s p=0.748 n=6
Total Time 51.48s (± 0.34%) 51.46s (± 0.36%) ~ 51.22s 51.73s p=0.810 n=6
self-compiler - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 256,768 256,768 ~ ~ ~ p=1.000 n=6
Types 104,587 104,589 +2 (+ 0.00%) ~ ~ p=0.001 n=6
Memory used 426,072k (± 0.01%) 426,066k (± 0.01%) ~ 426,037k 426,122k p=0.810 n=6
Parse Time 2.79s (± 0.42%) 2.79s (± 0.43%) ~ 2.78s 2.81s p=0.388 n=6
Bind Time 1.11s (± 0.74%) 1.10s (± 0.74%) ~ 1.09s 1.11s p=0.389 n=6
Check Time 15.20s (± 0.49%) 15.18s (± 0.44%) ~ 15.07s 15.26s p=0.747 n=6
Emit Time 1.15s (± 0.85%) 1.15s (± 1.28%) ~ 1.13s 1.17s p=0.615 n=6
Total Time 20.25s (± 0.37%) 20.22s (± 0.35%) ~ 20.11s 20.30s p=0.747 n=6
ts-pre-modules - node (v18.15.0, x64)
Errors 35 35 ~ ~ ~ p=1.000 n=6
Symbols 224,575 224,575 ~ ~ ~ p=1.000 n=6
Types 93,785 93,785 ~ ~ ~ p=1.000 n=6
Memory used 369,862k (± 0.03%) 369,852k (± 0.03%) ~ 369,696k 369,972k p=0.810 n=6
Parse Time 3.53s (± 1.00%) 3.51s (± 0.68%) ~ 3.48s 3.55s p=0.466 n=6
Bind Time 1.93s (± 0.71%) 1.92s (± 1.52%) ~ 1.87s 1.95s p=0.560 n=6
Check Time 19.40s (± 0.26%) 19.34s (± 0.19%) ~ 19.28s 19.39s p=0.053 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 24.86s (± 0.28%) 24.77s (± 0.26%) -0.09s (- 0.38%) 24.66s 24.84s p=0.045 n=6
vscode - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 2,823,415 2,823,415 ~ ~ ~ p=1.000 n=6
Types 957,881 957,881 ~ ~ ~ p=1.000 n=6
Memory used 2,996,465k (± 0.00%) 2,996,475k (± 0.00%) ~ 2,996,422k 2,996,537k p=0.936 n=6
Parse Time 17.05s (± 0.27%) 17.03s (± 0.43%) ~ 16.94s 17.16s p=0.470 n=6
Bind Time 5.05s (± 0.35%) 5.05s (± 0.41%) ~ 5.02s 5.08s p=0.681 n=6
Check Time 89.23s (± 0.19%) 89.99s (± 2.25%) ~ 88.90s 94.11s p=1.000 n=6
Emit Time 28.86s (± 0.59%) 28.03s (± 6.48%) ~ 24.32s 28.88s p=0.336 n=6
Total Time 140.19s (± 0.12%) 140.09s (± 0.24%) ~ 139.65s 140.50s p=0.575 n=6
webpack - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 265,866 265,866 ~ ~ ~ p=1.000 n=6
Types 108,401 108,401 ~ ~ ~ p=1.000 n=6
Memory used 410,578k (± 0.03%) 410,488k (± 0.01%) ~ 410,426k 410,528k p=0.128 n=6
Parse Time 3.84s (± 1.07%) 3.83s (± 0.96%) ~ 3.79s 3.87s p=1.000 n=6
Bind Time 1.67s (± 0.90%) 1.66s (± 1.28%) ~ 1.63s 1.69s p=0.326 n=6
Check Time 17.00s (± 0.55%) 16.91s (± 0.23%) ~ 16.86s 16.97s p=0.109 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 22.51s (± 0.39%) 22.41s (± 0.30%) ~ 22.34s 22.49s p=0.108 n=6
xstate-main - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 524,639 524,639 ~ ~ ~ p=1.000 n=6
Types 178,906 178,906 ~ ~ ~ p=1.000 n=6
Memory used 462,703k (± 0.02%) 462,732k (± 0.01%) ~ 462,667k 462,837k p=0.689 n=6
Parse Time 3.88s (± 0.55%) 3.88s (± 0.47%) ~ 3.87s 3.91s p=0.676 n=6
Bind Time 1.44s (± 0.57%) 1.45s (± 1.94%) ~ 1.41s 1.47s p=1.000 n=6
Check Time 22.44s (± 0.81%) 22.47s (± 0.70%) ~ 22.26s 22.73s p=0.936 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 27.76s (± 0.66%) 27.80s (± 0.48%) ~ 27.60s 28.02s p=0.520 n=6
System info unknown
Hosts
  • node (v18.15.0, x64)
Scenarios
  • Compiler-Unions - node (v18.15.0, x64)
  • angular-1 - node (v18.15.0, x64)
  • mui-docs - node (v18.15.0, x64)
  • self-build-src - node (v18.15.0, x64)
  • self-build-src-public-api - node (v18.15.0, x64)
  • self-compiler - node (v18.15.0, x64)
  • ts-pre-modules - node (v18.15.0, x64)
  • vscode - node (v18.15.0, x64)
  • webpack - node (v18.15.0, x64)
  • xstate-main - node (v18.15.0, x64)
Benchmark Name Iterations
Current pr 6
Baseline baseline 6

Developer Information:

Download Benchmarks

@jakebailey
Copy link
Member

Perf looks unaffected, I'm afraid.

@typescript-bot
Copy link
Collaborator

@jakebailey Here are the results of running the top 400 repos comparing main and refs/pull/58551/merge:

Everything looks good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants