Skip to content

perf(codegen): speed up printing StringLiterals#10046

Merged
graphite-app[bot] merged 1 commit intomainfrom
03-26-perf_codegen_speed_up_printing_stringliteral_s
Apr 2, 2025
Merged

perf(codegen): speed up printing StringLiterals#10046
graphite-app[bot] merged 1 commit intomainfrom
03-26-perf_codegen_speed_up_printing_stringliteral_s

Conversation

@overlookmotel
Copy link
Member

@overlookmotel overlookmotel commented Mar 25, 2025

Speed up printing StringLiterals by:

  • Iterating over the string byte-by-byte, not char-by-char.
  • Not using a peekable iterator.
  • Printing string in chunks.
  • Using a lookup table for bytes that need escaping.
  • Minimizing the amount of code in the hot loop.
  • Calculating the quote character to use lazily.
  • Faster calculation of quote character when backtick cannot be used as quote.

In common case where string contains no bytes that need escaping, the string will be written to output buffer in one shot, and there's no need for an extra pass through the string's bytes to decide on the quote character to use.

Copy link
Member Author

overlookmotel commented Mar 25, 2025


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@codspeed-hq
Copy link

codspeed-hq bot commented Mar 25, 2025

CodSpeed Instrumentation Performance Report

Merging #10046 will create unknown performance changes

Comparing 03-26-perf_codegen_speed_up_printing_stringliteral_s (775abac) with main (7672620)

Summary

🆕 36 new benchmarks

Benchmarks breakdown

Benchmark BASE HEAD Change
🆕 codegen[checker.ts] N/A 22.5 ms N/A
🆕 codegen_sourcemap[checker.ts] N/A 65.5 ms N/A
🆕 formatter[antd.js] N/A 7.6 ms N/A
🆕 formatter[react.development.js] N/A 41.4 µs N/A
🆕 formatter[typescript.js] N/A 7.6 ms N/A
🆕 isolated-declarations[vue-id.ts] N/A 58.3 ms N/A
🆕 lexer[RadixUIAdoptionSection.jsx] N/A 21.3 µs N/A
🆕 lexer[antd.js] N/A 24.8 ms N/A
🆕 lexer[cal.com.tsx] N/A 5.9 ms N/A
🆕 lexer[checker.ts] N/A 14.8 ms N/A
🆕 lexer[pdf.mjs] N/A 3.9 ms N/A
🆕 linter[RadixUIAdoptionSection.jsx] N/A 2.7 ms N/A
🆕 linter[cal.com.tsx] N/A 1.2 s N/A
🆕 linter[checker.ts] N/A 2.9 s N/A
🆕 mangler[antd.js] N/A 16 ms N/A
🆕 mangler[react.development.js] N/A 294 µs N/A
🆕 mangler[typescript.js] N/A 39.7 ms N/A
🆕 minifier[antd.js] N/A 166.2 ms N/A
🆕 minifier[react.development.js] N/A 1.9 ms N/A
🆕 minifier[typescript.js] N/A 293.1 ms N/A
... ... ... ... ...

ℹ️ Only the first 20 benchmarks are displayed. Go to the app to view all benchmarks.

@overlookmotel overlookmotel force-pushed the 03-26-perf_codegen_speed_up_printing_stringliteral_s branch from 248c62a to 9da8106 Compare March 27, 2025 07:28
@overlookmotel overlookmotel force-pushed the 03-26-refactor_codegen_print_string_literals_containing_lone_surrogates_without_reference_to_raw_ branch from 93d7896 to 687f8bf Compare March 27, 2025 07:28
@overlookmotel overlookmotel marked this pull request as ready for review March 27, 2025 07:38
@overlookmotel overlookmotel force-pushed the 03-26-perf_codegen_speed_up_printing_stringliteral_s branch from 9da8106 to c69b887 Compare March 27, 2025 07:44
@overlookmotel overlookmotel changed the base branch from 03-26-refactor_codegen_print_string_literals_containing_lone_surrogates_without_reference_to_raw_ to graphite-base/10046 March 27, 2025 10:18
@overlookmotel overlookmotel force-pushed the 03-26-perf_codegen_speed_up_printing_stringliteral_s branch from c69b887 to 96a803b Compare March 27, 2025 10:18
@overlookmotel overlookmotel changed the base branch from graphite-base/10046 to 03-27-feat_data_structures_add_codebuffer__unchecked_cap_methods March 27, 2025 10:18
@overlookmotel overlookmotel changed the base branch from 03-27-feat_data_structures_add_codebuffer__unchecked_cap_methods to graphite-base/10046 March 27, 2025 15:46
@overlookmotel overlookmotel force-pushed the 03-26-perf_codegen_speed_up_printing_stringliteral_s branch from 96a803b to ff181cc Compare March 27, 2025 15:47
@overlookmotel overlookmotel changed the base branch from graphite-base/10046 to 03-27-perf_codegen_faster_printing_quotes March 27, 2025 15:47
@Boshen
Copy link
Member

Boshen commented Mar 28, 2025

I don't want you to go down the rabbit hole ... but we can probably avoid looping through the string twice by storing the position of the "unknown" values, and change them at the end of the loop.

@Boshen Boshen added the 0-merge Merge with Graphite Merge Queue label Mar 28, 2025
Copy link
Member

Boshen commented Mar 28, 2025

Merge activity

  • Mar 28, 12:29 AM EDT: The merge label '0-merge' was detected. This PR will be added to the Graphite merge queue once it meets the requirements.
  • Mar 28, 9:16 AM EDT: The merge label '0-merge' was detected. This PR will be added to the Graphite merge queue once it meets the requirements.
  • Mar 28, 9:18 AM EDT: The merge label '0-merge' was detected. This PR will be added to the Graphite merge queue once it meets the requirements.
  • Apr 2, 8:57 AM EDT: Boshen added this pull request to the Graphite merge queue.
  • Apr 2, 9:11 AM EDT: Merged by the Graphite merge queue.

@graphite-app graphite-app bot force-pushed the 03-27-perf_codegen_faster_printing_quotes branch from 699ade4 to e44b613 Compare March 28, 2025 05:09
@graphite-app graphite-app bot force-pushed the 03-26-perf_codegen_speed_up_printing_stringliteral_s branch from ff181cc to 84c33ba Compare March 28, 2025 05:10
@overlookmotel
Copy link
Member Author

overlookmotel commented Mar 28, 2025

As a test, I tried the shortcut of just printing raw - the fastest possible way to print a StringLiteral, as it involves no processing at all. That only gave a 0.15% perf improvement vs this PR.

So basically, I think there's very little gain in trying to optimize it any further. We might get 0.1% perf improvement at best. It was probably a waste of time even going as far as I have in this PR!

@overlookmotel overlookmotel changed the base branch from 03-28-fix_codegen_do_not_escape_in_strings_unless_using_backtick_as_quote to graphite-base/10046 March 28, 2025 14:57
@overlookmotel overlookmotel force-pushed the 03-26-perf_codegen_speed_up_printing_stringliteral_s branch from fd663ec to 4984b06 Compare March 28, 2025 14:57
@overlookmotel overlookmotel changed the base branch from graphite-base/10046 to 03-28-ci_codegen_add_benchmark_for_minified_printing March 28, 2025 14:57
@graphite-app graphite-app bot force-pushed the 03-28-ci_codegen_add_benchmark_for_minified_printing branch 2 times, most recently from a898510 to 021986e Compare March 29, 2025 13:12
@graphite-app graphite-app bot force-pushed the 03-26-perf_codegen_speed_up_printing_stringliteral_s branch from bc8214f to 08b18d4 Compare March 29, 2025 13:12
@overlookmotel overlookmotel changed the base branch from 03-28-ci_codegen_add_benchmark_for_minified_printing to graphite-base/10046 April 2, 2025 06:49
@overlookmotel overlookmotel force-pushed the 03-26-perf_codegen_speed_up_printing_stringliteral_s branch from 08b18d4 to 84a22c2 Compare April 2, 2025 06:49
@overlookmotel overlookmotel changed the base branch from graphite-base/10046 to 04-02-test_parser_tests_for_lone_surrogates_and_lossy_escape_characters April 2, 2025 06:49
@overlookmotel overlookmotel marked this pull request as ready for review April 2, 2025 06:51
@overlookmotel
Copy link
Member Author

This is finally ready to merge.

@overlookmotel overlookmotel requested a review from Boshen April 2, 2025 06:52
@overlookmotel
Copy link
Member Author

@Boshen I assume the effect on formatter benchmarks is just noise?

@overlookmotel overlookmotel force-pushed the 03-26-perf_codegen_speed_up_printing_stringliteral_s branch from 84a22c2 to d94d261 Compare April 2, 2025 10:50
@overlookmotel overlookmotel force-pushed the 04-02-test_parser_tests_for_lone_surrogates_and_lossy_escape_characters branch from daee98a to 2f58955 Compare April 2, 2025 10:50
Speed up printing `StringLiteral`s by:

* Iterating over the string byte-by-byte, not char-by-char.
* Not using a peekable iterator.
* Printing string in chunks.
* Using a lookup table for bytes that need escaping.
* Minimizing the amount of code in the hot loop.
* Calculating the quote character to use lazily.
* Faster calculation of quote character when backtick cannot be used as quote.

In common case where string contains no bytes that need escaping, the string will be written to output buffer in one shot, and there's no need for an extra pass through the string's bytes to decide on the quote character to use.
@graphite-app graphite-app bot force-pushed the 04-02-test_parser_tests_for_lone_surrogates_and_lossy_escape_characters branch from 2f58955 to 7672620 Compare April 2, 2025 12:57
@graphite-app graphite-app bot force-pushed the 03-26-perf_codegen_speed_up_printing_stringliteral_s branch from d94d261 to 775abac Compare April 2, 2025 12:58
Base automatically changed from 04-02-test_parser_tests_for_lone_surrogates_and_lossy_escape_characters to main April 2, 2025 13:03
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Apr 2, 2025
@graphite-app graphite-app bot merged commit 775abac into main Apr 2, 2025
27 checks passed
@graphite-app graphite-app bot deleted the 03-26-perf_codegen_speed_up_printing_stringliteral_s branch April 2, 2025 13:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-codegen Area - Code Generation C-performance Category - Solution not expected to change functional behavior, only performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants