feat(data_structures): add CodeBuffer::print_strs_array#19760
Conversation
Merging this PR will not alter performance
Comparing Footnotes
|
There was a problem hiding this comment.
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_arrayand an internalprint_strs_array_with_total_lenfast 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.
|
@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. |
4add9b5 to
284b0b2
Compare
Merge activity
|
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.
284b0b2 to
4699498
Compare
…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.

Add a method
CodeBuffer::print_strs_array. This works much likeStringBuilder::from_strs_array_inandAtom::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_strcalls, 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_inthere have to be additional checks when computing the total length to ensure the sum of lengths does not exceedusize::MAXand 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.