Skip to content

feat(data_structures): add CodeBuffer::print_strs_array#19760

Merged
graphite-app[bot] merged 1 commit intomainfrom
om/02-26-feat_data_structures_add_codebuffer_print_strs_array_
Feb 26, 2026
Merged

feat(data_structures): add CodeBuffer::print_strs_array#19760
graphite-app[bot] merged 1 commit intomainfrom
om/02-26-feat_data_structures_add_codebuffer_print_strs_array_

Conversation

@overlookmotel
Copy link
Member

@overlookmotel overlookmotel commented Feb 26, 2026

Add a method CodeBuffer::print_strs_array. This works much like StringBuilder::from_strs_array_in and Atom::from_strs_array_in. It takes an array of &strs, computes the total length required to print them all, reserves space for the total number of bytes for all together, and then writes all the strings one after another.

The result is that it's much faster than a series of print_str calls, which incur the cost of a bounds check and branch for each string. Instead you only do 1 bounds check for the whole batch.

Like StringBuilder::from_strs_array_in there have to be additional checks when computing the total length to ensure the sum of lengths does not exceed usize::MAX and wrap around. But when some of the strings are statically known (e.g. buffer.print_strs_array(["foo", dynamic, "bar"])) and so it's impossible for overflow to occur, these checks can usually be removed by compiler.

@github-actions github-actions bot added the C-enhancement Category - New feature or request label Feb 26, 2026
Copy link
Member Author

overlookmotel commented Feb 26, 2026


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 Feb 26, 2026

Merging this PR will not alter performance

✅ 52 untouched benchmarks
⏩ 3 skipped benchmarks1


Comparing om/02-26-feat_data_structures_add_codebuffer_print_strs_array_ (284b0b2) with main (fd938d3)2

Open in CodSpeed

Footnotes

  1. 3 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

  2. No successful run was found on graphite-base/19760 (da9445a) during the generation of this report, so main (fd938d3) was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a new optimized batching API to CodeBuffer for printing multiple &str segments with a single reserve/bounds check, aligning with similar *_from_strs_array_in patterns elsewhere in the codebase.

Changes:

  • Introduce CodeBuffer::print_strs_array and an internal print_strs_array_with_total_len fast path using precomputed total length + raw copies.
  • Update imports to support the new pointer-based implementation.
  • Add a unit test covering a basic multi-segment concatenation.

Copy link
Contributor

Copilot AI commented Feb 26, 2026

@overlookmotel I've opened a new pull request, #19798, to work on those changes. Once the pull request is ready, I'll request review from you.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

@overlookmotel overlookmotel force-pushed the om/02-26-feat_data_structures_add_codebuffer_print_strs_array_ branch from 4add9b5 to 284b0b2 Compare February 26, 2026 23:41
@graphite-app graphite-app bot added the 0-merge Merge with Graphite Merge Queue label Feb 26, 2026
@graphite-app graphite-app bot changed the base branch from om/02-22-perf_estree_tokens_use_ident_s_from_ast_for_identifier_tokens to graphite-base/19760 February 26, 2026 23:44
@graphite-app
Copy link
Contributor

graphite-app bot commented Feb 26, 2026

Merge activity

@graphite-app graphite-app bot changed the base branch from graphite-base/19760 to main February 26, 2026 23:52
Add a method `CodeBuffer::print_strs_array`. This works much like `StringBuilder::from_strs_array_in` and `Atom::from_strs_array_in`. It takes an array of `&str`s, computes the total length required to print them all, reserves space for the total number of bytes for all together, and then writes all the strings one after another.

The result is that it's much faster than a series of `print_str` calls, which incur the cost of a bounds check and branch for each string. Instead you only do 1 bounds check for the whole batch.

Like `StringBuilder::from_strs_array_in` there have to be additional checks when computing the total length to ensure the sum of lengths does not exceed `usize::MAX` and wrap around. But when some of the strings are statically known (e.g. `buffer.print_strs_array(["foo", dynamic, "bar"])`) and so it's impossible for overflow to occur, these checks can usually be removed by compiler.
@graphite-app graphite-app bot force-pushed the om/02-26-feat_data_structures_add_codebuffer_print_strs_array_ branch from 284b0b2 to 4699498 Compare February 26, 2026 23:53
graphite-app bot pushed a commit that referenced this pull request Feb 26, 2026
…cks (#19766)

Use `CodeBuffer::print_strs_array` (introduced in #19760) to optimize serializing to JSON in `ESTree` trait. When printing the start of an object property `"key":`, instead of printing `"`, then `key`, then `":`, print them all in one batch.

As this pushing strings into the serializer's buffer is the core of what JSON serialization involves, this simple optimization produces a sizeable speed-up - +10% on ESTree benchmark, +7% on ESTree tokens benchmark.
@graphite-app graphite-app bot merged commit 4699498 into main Feb 26, 2026
22 checks passed
@graphite-app graphite-app bot deleted the om/02-26-feat_data_structures_add_codebuffer_print_strs_array_ branch February 26, 2026 23:59
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Feb 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-enhancement Category - New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants