Strings: a short + chain no longer pays for a deferred representation it never takes - #3571
Conversation
…on it never takes sebastienros#3386 gave a long `+` a deferred copy, and SunSpider's date-format-xparb row paid 3.9% for it. The cutoff that was supposed to keep short concatenations out of that trade was already there and already working — `JsString.MinDeferredConcatenationLength`, 512 characters, honoured by both the pairwise `+` and the flattened chain — and xparb's chains are ten to thirty-five characters, so they were on the eager side of it the whole time. What they paid for was not the deferral. It was the shape of the eager path. Two things had moved onto it. The chain lane coerced every operand with `TypeConverter.ToJsString` before the cutoff was read, which allocates a `JsString` wrapper for every operand that is not already a string — a number, which is most of what a formatted date is made of — and the eager join then unwrapped every one of them again. And `ConcatMany` built a `string[]` to hand to `string.Concat` on top of the `JsString[]` the operands already lived in, so a chain allocated two arrays where the pre-sebastienros#3386 lane allocated one. For xparb's fifteen-operand long-format chain that is a second 144-byte array plus a wrapper per numeric operand, all of it dead before the result was copied out: the doubled variable-size allocations and the +23% allocation events the profile in sebastienros#3527 measured under the chain node. An operand is now carried in whichever form its coercion already produced — the `JsString` it was, whose representation has to survive because flattening it is the copy the deferred lane exists to avoid, or the plain text a non-string primitive coerced to. Both answer their length without materializing anything, which is all the cutoff is decided on, so nothing is given up by holding the cheaper form until the decision is made. Below the cutoff the result is then joined through a `ValueStringBuilder` over a cutoff-sized stack buffer, which the assembly's `SkipLocalsInit` leaves unzeroed, so there is no second array and no wrapper. Above it the fold is unchanged. The pairwise `+` gets the same treatment from the other end: two operands that are already strings — a `+` between two string expressions — now take a branch that coerces nothing at all, and the mixed pair (`n + "x"`) coerces to text and builds the wrapper only if it goes on to defer. `+=` and `String.prototype.concat` are untouched and exempt: both build `JsString.ConcatenatedString`, the mutable builder, and never enter the deferred representation at all. Bytes per evaluation of an eleven-operand short chain, as a delta of a thread-local allocation counter: 576 to 272 on net10.0 and net8.0, 712 to 296 on net472. `AShortChainDoesNotPayForTheDeferredRepresentation` pins it at 400, between the two with a third of the distance on either side; the asymptotic guarantee sebastienros#3386 exists for is pinned as before by `AccumulatingWithPlusIsNoLongerQuadratic`, now including the five-operand chain shapes that reach `ConcatMany`. Fixes sebastienros#3527. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014W5mbjGhyvgAS4pivXoc4S
|
Gate verdict: PASS on all three measurements (paired, quiet box, baseline = merge-base SunSpider, 26 rows, 8 rounds: zero regressions. Dromaeo, 24 rows, 8 rounds: 23 no-change, and one borderline flag —
The flag does not reproduce under clean conditions — #3386's deferred-copy wins hold, exactly as the deferring branch being byte-identical predicts. With the allocation evidence in the PR body (−52.8%/−58.4% bytes per short-chain evaluation) and every suite at its control, this merges. |
date-format-xparbpays +3.91% since #3386 (#3527), and the profile in that issue put the cost underAdditionChainExpression:RhpNewVariableSizeObjectunder the chain ×2.0,RhpNewPtrArrayFast×3.3, allocation events +23%, whileString.Concat— the cost the deferred copy was meant to displace — did not move at all. The workload allocates more under the new lane, not less.The cutoff was never the problem
The issue proposed adding a length cutoff to the chain lane. There already is one, and it was already doing its job:
JsString.MinDeferredConcatenationLengthis 512 characters, and both the pairwise+(insideJsString.Concat) and the flattened chain (ConcatThree/ConcatFour/ConcatMany) test the summed operand lengths against it. xparb's chains are"2007-01-01"and"Monday, January 01, 2007 1:11:11 AM"— ten and thirty-five characters. They were on the eager side of the cutoff the entire time, and no threshold value can change that. Moving the line would only have pushed more chains onto the deferred path, which is the expensive one here.What they paid for was the shape of the path below the line, which #3386 changed without meaning to:
TypeConverter.ToString→ astringTypeConverter.ToJsString→ astringplus aJsStringwrapper, for every operand that is not already a stringConcatManystoragestring[]JsString[]and astring[]built from it forstring.Concatxparb's
"l, F d, Y g:i:s A"compiles to one fifteen-operand chain. Per evaluation that is a second 144-byte array plus a wrapper per numeric operand —getFullYear(), the hour expression — every byte of it dead before the result was copied out. That is the doubled variable-size allocations and the tripled pointer-array allocations the profile measured, and none of it existed before #3386.What changed
An operand is now held in whichever form its coercion already produced: the
JsStringit was — whose representation has to survive, because flattening it is precisely the copy the deferred lane exists to avoid — or the plain text a non-string primitive coerced to. Both answer their length without materializing anything, and the length is all the cutoff is decided on, so holding the cheaper form until the decision is made gives nothing up.Below the cutoff the result is then joined through a
ValueStringBuilderover a cutoff-sized stack buffer — free, since the assembly carries[module: SkipLocalsInit], and it cannot grow because the cutoff bounds it — so there is no second array and no wrapper. Above the cutoff the fold throughJsString.Concatis untouched.The cutoff itself stays at 512, and its remarks now carry the arithmetic that was missing: a
RopeStringis 56 bytes on 64-bit and does not remove the flat allocation, it postpones it, so a result read once costs 56 bytes and an indirection more than copying it would have. The node earns them back only across iterations. Which is why the line sits far above the point where a node is merely affordable — and why what the path below the line allocates matters more than where the line is.Every lane that can reach the deferred representation
JsString.Concatis the only producer ofRopeString, and it has exactly two callers.ApplyAdditionToPrimitives— the pairwise+, and the pairwise fold a numeric chain falls back ton + "x") coerces to text and builds the wrapper only on the branch that goes on to defer.AdditionChainExpression— the flatteneda + b + c […], both the direct path and the resumed one, which share the same three joins+=—JintAssignmentExpressionJsString.ConcatenatedString, theStringBuilder-backed mutable accumulator. Never callsJsString.Concat, never produces aRopeString. #3386 did not touch it and neither does this.String.prototype.concatEnsureCapacity/AppendonConcatenatedString.JsString.CreateSliced→SlicedStringSemantics
Unchanged, deliberately and checkably. Every path produces the same characters through the same
JsString.Create, so the same shared instances are returned for the empty and single-character results, and the same representation decision is taken on the same numbers —typeof,.length, indexing, equality and hashing all see exactly what they saw. Coercion order is preserved (left operand before right;ToStringof a symbol still throws from the same side), and the length guard still runs on the summed lengths before anything is built. No public API, no observable behaviour change, so nodocs/v5-migration.mdentry.Failing first
AShortChainDoesNotPayForTheDeferredRepresentationmeasures bytes per evaluation of an eleven-operand short chain as a delta ofGC.GetAllocatedBytesForCurrentThread, which says what a profile says without depending on what else the machine is doing. Measured against this branch and againstmainwith only the three runtime files reverted:mainThe ceiling is 400 B — 35% above the largest after-figure, 30% below the smallest before-figure — so it fails on every framework against unfixed code, and neither side is close enough to the line to be fragile.
The asymptotic guarantee #3386 exists for is pinned as before by
AccumulatingWithPlusIsNoLongerQuadratic(allocation ratio at 4,000 vs 8,000 iterations under 3.0, plus a 16 MB absolute ceiling — the old shape allocated 640 MB), now extended with the two five-operand chain shapes that reachConcatMany:s = s + chunk + chunk + chunk + chunkands = chunk + chunk + chunk + chunk + s. Both stay linear.Also added: the mixed coerced/string chain at every arity that has its own join (3, 4, 5, 6, 11) asserted character-for-character, and the two-operand mixed pair asserted on both sides of the cutoff — flat below it,
RopeStringabove it in both leanings.Verification
dotnet build -c Release— clean, 0 warnings, all five target frameworks.Jint.Tests— 11,625 passed on net10.0 and net8.0, 8,245 on net472, 0 failed.Jint.Tests.PublicInterface— 3,355 / 3,345 / 2,722 passed on net10.0 / net8.0 / net472, 0 failed.Jint.Tests.CommonScripts— 28/28 on net10.0 and net472. These are the SunSpider scripts themselves,date-format-xparbincluded, run for correctness.Jint.Tests.SourceGenerators— 71/71.Jint.Tests.Test262— 102,537 passed, 0 failed, 151 skipped of 102,688. Exactly the control.Benchmarks — not run
Nothing here was measured with BenchmarkDotNet; this machine runs agents concurrently. The paired gates belong to whoever runs them, and these are the predictions the change should be judged against:
date-format-xparb,string-tagcloud,crypto-md5(SunSpider) — should recover. These are the three rows SunSpider: date-format-xparb pays +3.9% and bitops-3bit +1.8% for the deferred-copy string representation #3527 records as having paid, and all three are built from short concatenations; xparb is the one with a measured mechanism and should move most.ObjectRegExp(Dromaeo) — must keep Strings: a long+defers its copy, sos = s + xis linear likes += x#3386's −8.9% to −11.5%. Nothing on the deferring path changed: the cutoff, the fold andRopeStringare all as they were, and a chain that crosses 512 characters takes the branch it took yesterday. A regression here would mean the operand-form change moved the cutoff decision, which is the one thing it is built not to do.StringConcatLargeBenchmark'sAssign*lanes — flat. They accumulate past the cutoff within their first iterations and are deferred from then on.ChainSmallThree/ChainSmallSix— should improve slightly, being exactly the eager-chain shape this touches.+=rows (AppendSmallChunks,AppendLargeChunks,BuildLargeThenScan) — must be flat. Not one line on their path changed.The other half of #3527
bitops-3bit-bits-in-byte— the +1.81% row — does not reproduce, and needs no code. The re-profile in the analysis comment found the candidate capture had fewer total samples than base (150,057 vs 152,123) with no function moved beyond ±0.3%; the row contains no string work in its loop, and the original figure was measured with a virus scanner active. The issue's own framing allows "not reproducible" as an outcome for that half, which is why this closes the issue rather than leaving it open on that row.Fixes #3527.
🤖 Generated with Claude Code
https://claude.ai/code/session_014W5mbjGhyvgAS4pivXoc4S