Skip to content

🐛 fix(js): fold string concatenation chains in linear time - #2264

Merged
gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:fix/js-concat-chain-linear
Nov 16, 2026
Merged

gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:fix/js-concat-chain-linear

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Folding a + chain of string literals in minify_js() took time that grew with the square of the chain length. A 3.2 MB script made of 800 literals joined by + took about 2.4 seconds on upstream/main, so a minifier pass over a large generated bundle could stall for that long on one statement. After this change the same input folds in a few milliseconds and the time is proportional to its length. Fixes #2257.

The fold of a + node decoded both operands and re-encoded their joined value, and a left-deep chain makes each + join the whole prefix built so far. The prefix was therefore decoded and encoded again at every operator, which is quadratic in the chain length.

The chain is now joined in one pass. The operands are decoded once into a single buffer as the chain is walked, and the joined value is encoded once, where the last joined + stands. The owned buffers of the joined operands are released at that point, and the buffers of operands after the joined prefix are kept, so memory use does not grow with the number of operators either.

Behavior changes

The output is the same for every chain. Only the time spent folding changes.

Folding a `+` chain of string literals joined each prefix again at every
operator. The prefix was decoded and re-encoded in full each time, so a
chain cost the number of operators times the length of its value: 800
literals of 4000 characters took about 2.4 seconds.

The fold now decodes each operand once into one buffer as the chain is
walked and encodes the joined value once, where the last joined operator
stands. The owned buffers of the joined operands are released at that
point, and the buffers of operands past the joined prefix are kept.

Fixes tox-dev#2257
@gaborbernat gaborbernat added the bug Something isn't working label Nov 16, 2026
@gaborbernat
gaborbernat merged commit 3269e33 into tox-dev:main Nov 16, 2026
19 of 48 checks passed
@codspeed

codspeed Bot commented Nov 16, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 6.67%

2 warnings

⚠️ 1 benchmark with a significant change ran on different hardware

Its base and head results were measured on machines with a different CPU, core count or memory, so the change might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚠️ 1 benchmark with a significant change ran in a different software environment

Its base and head results were measured with a different OS or toolchain, such as compiler, runtime or library versions, so the change might come from this difference rather than from the code. Open the benchmark to see what changed.

See affected benchmarks

❌ 1 regressed benchmark
✅ 581 untouched benchmarks
⏩ 89 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_feature[query-closest] 256.1 µs 274.4 µs -6.67%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing gaborbernat:fix/js-concat-chain-linear (4d325f7) with main (4236996)2

Open in CodSpeed

Footnotes

  1. 89 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 main (38a1c5a) during the generation of this report, so 4236996 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 fix(js): string concatenation chains fold quadratically

1 participant