Repository navigation
🐛 fix(js): fold string concatenation chains in linear time - #2264
Conversation
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
Merging this PR will degrade performance by 6.67%2 warnings
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing Footnotes
|
Folding a
+chain of string literals inminify_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 onupstream/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.