Fix #1680: scan + bulk-copy fast path for longer String values in UTF8JsonGenerator #1775
End-to-end StringWriteBench.utf8String (200 writeString(String) calls per op, one UTF8JsonGenerator), i7-8550U/WSL2, 3 forks. ascii = no escapes, escaped = one " near the end of each value. Benchmark source: https://github.com/FasterXML/jackson-core/pull/1681#issuecomment-6191270066
us/op (lower is better), change vs 3.x @ 4bd708fa0:
| JDK | content | len | 3.x | #1681 | this PR (edbf7ef85) |
|---|---|--:|--:|--:|--:|
| 17 | ascii | 12 | 7.6 ± 0.5 | 7.9 ± 0.7 (+3%) | 7.2 ± 0.3 (-5%) |
| 17 | ascii | 100 | 56.1 ± 5.1 | 51.4 ± 4.0 (-8%) | 42.6 ± 2.4 (-24%) |
| 17 | ascii | 1000 | 375.1 ± 20.0 | 397.5 ± 18.7 (+6%) | 206.2 ± 16.1 (-45%) |
| 17 | escaped | 12 | 10.6 ± 0.4 | 10.8 ± 0.5 (+2%) | 11.4 ± 0.6 (+8%) |
| 17 | escaped | 100 | 45.3 ± 2.7 | 45.3 ± 1.8 (0%) | 30.5 ± 1.8 (-33%) |
| 17 | escaped | 1000 | 392.8 ± 17.8 | 395.4 ± 35.2 (+1%) | 251.2 ± 11.9 (-36%) |
| 25 | ascii | 12 | 8.5 ± 0.5 | 8.2 ± 0.4 (-4%) | 8.4 ± 0.6 (-1%) |
| 25 | ascii | 100 | 53.2 ± 2.5 | 40.6 ± 1.5 (-24%) | 36.2 ± 7.6 (-32%) |
| 25 | ascii | 1000 | 290.9 ± 14.6 | 298.8 ± 14.1 (+3%) | 211.7 ± 68.8 (-27%) |
| 25 | escaped | 12 | 10.2 ± 1.1 | 8.3 ± 0.4 (-18%) | 24.2 ± 2.0 (outlier, see below) |
| 25 | escaped | 100 | 52.2 ± 2.4 | 35.2 ± 2.0 (-33%) | 32.3 ± 4.4 (-38%) |
| 25 | escaped | 1000 | 373.9 ± 16.9 | 300.4 ± 12.4 (-20%) | 233.2 ± 12.5 (-38%) |
12-char values are below the 32-char cutoff, so they don't use the new path. The JDK 25 escaped/12 outlier did not reproduce in a 3-fork re-run (us/op):
| JDK | content | 3.x | edbf7ef85 | 291572cd3 (bulk path in separate method) |
|---|---|--:|--:|--:|
| 25 | ascii | 8.73 ± 0.62 | 8.28 ± 0.45 | 7.98 ± 0.37 |
| 25 | escaped | 9.91 ± 0.39 | 9.58 ± 0.39 | 9.50 ± 0.40 |
| 17 | ascii | 7.08 ± 0.21 | 7.44 ± 0.33 | 8.37 ± 1.81 |
| 17 | escaped | 11.08 ± 0.49 | 11.35 ± 0.63 | 10.68 ± 0.49 |
All within noise. I still pushed 291572cd3, which moves the scan + getBytes path into a separate small method (_writeUnescapedAsciiBulk), so _writeStringSegment keeps its 3.x size and shape for short segments. That removes the inlining/code-layout risk at no measured cost. The long-string rows above were measured with edbf7ef85; 291572cd3 only moves that code into the helper.
Summary: on long strings (100+ chars) this approach avoids #1681's JDK 17 regression and is 24-45% faster than 3.x on both JDKs; short strings are neutral. Still an experiment, not for merge.
TBH this version looks better to me than mine @pjfanning !
And is a bit less "fragile" to JIT decisions (shit can always happen you know...) so +1 from me
And wow, Claude sometimes really surprise me!
@cowtowncoder can we go with this then over #1681. It seems to have perf improvements in most test cases for both Java 17 and 25.
@pjfanning I'm running it against https://github.com/franz1981/c2-writestring-spill-jmh/blob/master/README.md as well, to be sure :pray:
Ran this against the reproducer in https://github.com/franz1981/c2-writestring-spill-jmh — ExtendedPersonBench.serialize, which models a real Quarkus endpoint: a bean with two String properties plus two nested beans holding two Strings each, six writeString calls across three generated serializers, with writeString inlined into the callers. Ryzen 9 7950X pinned at 4.3 GHz, JDK 25.0.2, -Xms1g -Xmx1g -XX:+UseParallelGC as the benchmark fixes, -f 2 -wi 3 -i 3, no profiler.
Three arms, all on jackson-core 3.1.4 with databind #6183 applied (without it the StdDateFormat charAt profile pollution stops the copy loop unrolling at all and masks everything):
base — 3.1.4 as-isconst — #1681's constant escape testbulk — this PR's change, hand-applied to the 3.1.4 source (cutoff 32 + _writeUnescapedAsciiBulk), since 3.3.0-SNAPSHOT core cannot be mixed with 3.1.4 databind herens/op, lower is better:
strLen=0 (bean's natural 4-11 char values) strLen=64
base 2900.659 +- 35.9 base 11034.613 +- 105.9
const 2715.169 +- 34.0 -6.4% const 8261.396 +- 96.9 -25.1%
bulk 2948.483 +- 33.4 +1.6% bulk 6038.657 +- 23.5 -45.3%
On the realistic workload the two PRs do not overlap. With the bean's own field values — 4 to 11 characters — every segment is below MIN_SEGMENT_FOR_BULK_COPY = 32, so this PR takes the else branch, which is the unchanged original loop: bulk lands within noise of base (+1.6%, bars overlapping), while const is 6.4% faster. At 64 characters the ordering reverses and this PR is clearly best, 45% under base and 27% under const.
So from this benchmark they look complementary rather than alternatives: this PR wins on long values, #1681 wins on the short field values that dominate ordinary bean serialization. Taking this one instead of #1681 would leave the common case untouched.
Worth noting the cutoff is doing real work here — 32 is above the length of most JSON field values I see in practice (names, ids, enum-ish strings), so most real payloads will never reach the bulk path. If both land, the short path is exactly where #1681's constant test applies.
Caveat on provenance: the bulk arm is my transcription of this PR onto 3.1.4, not your branch built as-is, so treat it as "this change's shape" rather than a byte-for-byte test of #1775. Output was not separately diffed for this arm; the other two are the repo's existing jars.
@franz1981 thanks, agreed they are complementary. Pushed dac5a8f8f: segments under 32 chars now use #1681's constant escape check (only when the standard escape table is in use; otherwise the table loop as before), and 32+ chars keep the scan + getBytes path. You're credited as co-author on that commit.
Short strings only, StringWriteBench.utf8String (200 writeString(String) per op), i7-8550U/WSL2, 2 forks, us/op:
| JDK | content | len | 3.x | dac5a8f8f | change |
|---|---|--:|--:|--:|--:|
| 17 | ascii | 8 | 5.82 ± 0.35 | 5.91 ± 0.40 | +1.6% |
| 17 | ascii | 12 | 7.08 ± 0.44 | 7.38 ± 0.48 | +4.2% |
| 17 | ascii | 24 | 13.06 ± 2.58 | 13.42 ± 3.47 | +2.8% |
| 17 | escaped | 8 | 8.61 ± 0.52 | 8.91 ± 0.67 | +3.5% |
| 17 | escaped | 12 | 10.33 ± 0.59 | 10.45 ± 0.68 | +1.1% |
| 17 | escaped | 24 | 16.71 ± 0.91 | 15.69 ± 1.02 | -6.1% |
| 25 | ascii | 8 | 6.58 ± 0.72 | 6.56 ± 0.32 | -0.4% |
| 25 | ascii | 12 | 8.25 ± 0.65 | 8.12 ± 0.47 | -1.6% |
| 25 | ascii | 24 | 13.12 ± 1.10 | 13.05 ± 0.99 | -0.5% |
| 25 | escaped | 8 | 8.10 ± 0.47 | 7.03 ± 0.83 | -13.3% |
| 25 | escaped | 12 | 9.92 ± 0.78 | 7.95 ± 0.45 | -19.9% |
| 25 | escaped | 24 | 14.94 ± 1.02 | 10.82 ± 0.61 | -27.5% |
3.x on both JDKs).Your realistic workload (4-11 char values, writeString inlined into the serializers) would be a good confirmation on this exact branch, if you have time.
@pjfanning ran the confirmation you asked for, on b547cf17. Short version: dac5a8f8 fixes the case that matters for the application, and the PR as it stood before it did not.
Setup. ExtendedPersonBench from c2-writestring-spill-jmh — a bean with six String properties (4-11 chars, the application's own values), 20 copies per op, written through custom serializers so writeString gets inlined six times the way the generated serializers inline it. Ryzen 9 7950X, pinned to 3 non-SMT cores on one NUMA node, frequency fixed at 4.3 GHz, Temurin 17.0.17 and 25.0.2, -Xms1g -Xmx1g -XX:+UseParallelGC, 10 forks (20 for cells whose error bar exceeded 1.5%), no profiler attached.
The "out of line" rows are the same benchmark run with -XX:CompileCommand=dontinline,tools/jackson/core/json/UTF8JsonGenerator.writeString. It is a control, not a proposal - it shows how much of each effect depends on C2 inlining writeString into the serializers.
Four arms differing in exactly one class (UTF8JsonGenerator), all compiled from source at --release 17 so the same jars run on both JDKs: base = 3.1.4, #1681, #1775 old = this PR before dac5a8f8, #1775 head = b547cf17. All four produce byte-identical JSON on a 48-string set (quotes, backslash, control chars, DEL, Latin-1, euro, surrogate pair, and lengths spanning the 2000-char segment boundary), on both JDKs.
Profile pollution is a run parameter here, not a second jar. All four arms carry databind #6183 (lazy RFC1123 holder), and pollution is injected by calling String.charAt on a non-Latin1 String 64 times from one interpreted method before anything is compiled — what StdDateFormat.<clinit> does via DecimalFormatSymbols. I checked that reproduces the jar-level pollution rather than assuming it: compiled _writeStringSegment goes from 15 to 22 instructions per character, with the coder test and the value reload moving back inside the loop, the same shape the stock-databind jar produces.
ns/op, lower is better, delta vs base in the same row:
| JDK | writeString | profile | base | #1681 | #1775 old | #1775 head |
|---|---|---|--:|--:|--:|--:|
| 17 | inlined | clean | 3092.3 ± 25.7 | 2931.2 ± 37.2 (-5.2%) | 3111.2 ± 43.6 (+0.6%) | 2946.3 ± 43.5 (-4.7%) |
| 17 | inlined | polluted | 3441.7 ± 30.4 | 3036.2 ± 11.5 (-11.8%) | 3439.6 ± 34.9 (-0.1%) | 3020.6 ± 18.6 (-12.2%) |
| 17 | out of line | clean | 3172.3 ± 25.0 | 3083.8 ± 20.1 (-2.8%) | 3198.1 ± 14.3 (+0.8%) | 3086.3 ± 10.6 (-2.7%) |
| 17 | out of line | polluted | 2801.6 ± 40.9 | 2726.0 ± 15.0 (-2.7%) | 2796.7 ± 22.6 (-0.2%) | 2758.6 ± 16.1 (-1.5%) |
| 25 | inlined | clean | 2843.5 ± 13.7 | 2729.6 ± 7.6 (-4.0%) | 2925.6 ± 13.7 (+2.9%) | 2791.2 ± 23.3 (-1.8%) |
| 25 | inlined | polluted | 3647.0 ± 19.6 | 3056.9 ± 9.1 (-16.2%) | 3665.4 ± 14.4 (+0.5%) | 3092.8 ± 42.3 (-15.2%) |
| 25 | out of line | clean | 3060.6 ± 23.6 | 2875.5 ± 17.1 (-6.0%) | 3083.7 ± 12.5 (+0.8%) | 2915.8 ± 27.1 (-4.7%) |
| 25 | out of line | polluted | 2758.5 ± 30.9 | 2823.2 ± 16.9 (+2.3%) | 2754.0 ± 12.0 (-0.2%) | 2822.3 ± 19.8 (+2.3%) |
Inlined + polluted is the application's configuration — the Quarkus app has the pollution and C2 inlines writeString into the generated serializers. There #1775 head and #1681 are statistically indistinguishable (JDK 17: 3036.2 ± 11.5 vs 3020.6 ± 18.6; JDK 25: 3056.9 ± 9.1 vs 3092.8 ± 42.3, bars overlap on both), and #1775 old was flat.
#1775 old is within ±2.9% of base in all eight cells. That is what I measured before dac5a8f8 and why I thought the two PRs were not substitutes; with dac5a8f8 that objection is gone.
The assembly says why they match. The escape test per character, JDK 25 polluted, inside the
inlined copy loop in ExtendedPersonSer::serializeContent (PrintAssembly + hsdis):
base #1681 and #1775 head (identical bar registers)
cmp $0x7f,%r8d lea -0x20(%r9),%r10d
jg ... cmp $0x60,%r10d
cmp %r11d,%r8d <- bounds check jae ...
jae ... cmp $0x22,%r9d
mov 0x30(%rsp),%ecx <- table oop je ...
mov 0x10(%rcx,%r8,4),%ebp <- table[ch] cmp $0x5c,%r9d
test %ebp,%ebp je ...
jne ...
C2 does unswitch the stdEsc ternary across the inlined copies: there is no stdEsc test inside
the loop and no table load, and the resulting body is instruction-for-instruction the same as
#1681's apart from register allocation. Your code comment's bet holds up.
Worth noting the whole loop is 31 instructions per character for base, 32 for #1681, 33 for
#1775 head - the patched arms are not shorter. What they remove is the dependent load pair (table
oop off the stack, then the table element) plus a bounds-check branch, replaced by register-only
work.
Two things neither patch touches, visible in all three loops: the cmpb $0x0,0x10(%r8) coder test
and the value reload stay inside the loop when the profile is polluted, and all three still spill
heavily - 0x4(%rsp), 0x8(%rsp), 0x10(%rsp), 0x28(%rsp), 0x50(%rsp) and the loop limit at0x2c(%rsp) are touched every iteration.
Out of line, both patches are flat to slightly negative. Nothing in this workload reaches the 32-char bulk path, so for #1775 that is expected and the row says nothing about the bulk copy.
One methodology note, since your tables are 2 forks: these arms are bimodal across forks on this box. At 2 forks I had cells reporting ±443 ns/op on 3100. The per-fork breakdown shows two separate shapes — whole-fork plateaus present from the first measurement iteration (one cell's ten forks sat at ~3030, ~3120 and ~3210), plus occasional mid-run step changes inside a single fork (3670 3677 3871 5021 5045, and one going the other way, 3213 3164 3069 3070 3064). Within-fork scatter is only 7-87 ns/op, so the box is not the problem — the fork count is. A -f 10 on the rows you care about would be worth it; it moved several of my own cells by more than the effect I was measuring.
@franz1981 thanks for the confirmation run and the fork-count advice. I re-ran my JDK 17 short-string cells with -f 10 (same StringWriteBench.utf8String, 200 writeString(String) per op, i7-8550U/WSL2, JDK 17.0.20, 3.x vs dac5a8f8f), us/op:
| content | len | 3.x | dac5a8f8f | change | 3.x fork means | dac5a8f8f fork means |
|---|--:|--:|--:|--:|--:|--:|
| ascii | 8 | 6.87 ± 0.55 | 6.85 ± 0.12 | -0.3% | 6.22-8.77 | 6.68-7.09 |
| ascii | 12 | 8.13 ± 0.20 | 8.52 ± 0.14 | +4.9% | 7.92-8.65 | 8.34-8.74 |
| ascii | 24 | 13.41 ± 0.75 | 14.91 ± 1.46 | +11.2% | 10.96-16.10 | 11.25-19.26 |
| escaped | 8 | 8.53 ± 0.14 | 8.72 ± 0.13 | +2.2% | 8.38-8.73 | 8.49-8.98 |
| escaped | 12 | 10.59 ± 0.32 | 10.61 ± 0.33 | +0.2% | 10.11-11.13 | 9.93-11.43 |
| escaped | 24 | 16.02 ± 0.43 | 14.71 ± 0.24 | -8.1% | 15.25-16.83 | 14.34-15.21 |
\ benchmark artifact, see below
The ascii/24 bimodality comes from the benchmark, not this PR. The dac5a8f8f forks clustered at ~11.3, ~13.5 and 17.4-19.3. Logging C2 inlining per fork showed the slow mode lines up with whether GeneratorBase.close() gets inlined into the JMH stub. close() is inlined unless it happened to be compiled separately first (a compile-thread race), and inlining it drags in the whole close path (_flushBuffer, _releaseBuffers, the recycler CAS) next to the string loop. Neither branch changes close(); it's a side effect of the bench creating and closing a generator per op. With -XX:CompileCommand=dontinline,tools/jackson/core/base/GeneratorBase.close, 10 forks:
| content | len | 3.x | dac5a8f8f | change | 3.x fork means | dac5a8f8f fork means |
|---|--:|--:|--:|--:|--:|--:|
| ascii | 12 | 8.12 ± 0.14 | 8.76 ± 0.29 | +7.9% | 7.86-8.32 | 8.33-9.56 |
| ascii | 24 | 13.40 ± 0.60 | 13.46 ± 0.33 | +0.4% | 12.73-15.41 | 12.88-14.15 |
So on this box, with writeString not inlined into serializers, the short path on JDK 17 trades about 5-8% at ascii/12 for about 8% at escaped/24. That fits the JDK 17 front-end cost of the three-compare check that we saw for #1681. It's less favourable than your inlined Ryzen results (-4.7% clean, -12.2% polluted), so the CPU and the surrounding code shape seem to matter.
s inlined unless it happened to be compiled separately first
Disabling background compilation and turning off tiered compilation could help here
It's less favourable than your inlined Ryzen results (-4.7% clean, -12.2% polluted), so the CPU and the surrounding code shape seem to matter.
Yep, wider frontends machines are probably more keen to pay it less, and could rely on later phases (when fused uops happen?) to apply optimization idioms which free some RoB slots too, maybe
I don't know where to go from here. So far, for short ASCII strings, the results are mixed. Whether the existing code is better than what is in 1681 or this (1775). For longer strings, this (1775) has big advantages.
3.3.0 is due to be released soon. The options are:
skip 3.3.0 and aim for 3.4.0 (4-6 months away)
change this (1775) to only kick in for strings with escapes and for longer strings (>32 chars) and try to get that into 3.3.0 and delay 1681 to 3.4.0
I created a gist showing how users can use their own custom Utf8 generator with Jackson ObjectMapper.
https://gist.github.com/pjfanning/2d763dbdd67e2c14bba2697e4d6f6998
checking @pjfanning one sec
@pjfanning we reproduced your JDK 17 ascii/12 regression on an Intel laptop and are working on a fix that keeps the Zen 4 gain without it. Could you give us a little time before settling the 3.3.0 options? We'll come back with a proposal.
@pjfanning proposal: drop the cutoff. Your scan + getBytes path for every length (this PR with the MIN_SEGMENT_FOR_BULK_COPY branch removed) measured best overall:
writeString inlined into generated serializers, 4–11 char fields), Zen 4, JDK 25: −23% vs 3.x with stock jackson-databind; the current head is −16%.The Zen 4 loss at short strings comes from the JVM: HotSpot sets the ArrayOperationPartialInlineSize default only for Intel (inside if (is_intel()) in vm_version_x86.cpp), so on AMD a short getBytes calls the arraycopy stub instead of an inline masked copy. With -XX:ArrayOperationPartialInlineSize=32 on Zen 4, the no-cutoff version beats 3.x at 8 and 12 chars too. JDK 27 fixes the AMD default (JDK-8376794); it is not backported. Working around it in Java (constant-length chunked copies, byte loops) made Intel slower, so we'd rather not.
Not measured yet: escaped strings, and JDK 21/25 with your benchmark. Running those next.
@franz1981 I ran the no-cutoff variant on my i7-8550U (Kaby Lake R, no AVX-512) on JDK 17: StringWriteBench.utf8String, 10 forks, close() inlining disabled, us/op.
| content | len | 3.x | dac5a8f8f | no-cutoff |
|---|--:|--:|--:|--:|
| ascii | 8 | 6.73 ± 0.46 | 6.87 ± 0.14 (+2.0%) | 8.05 ± 0.27 (+19.6%) |
| ascii | 12 | 7.99 ± 0.13 | 8.52 ± 0.14 (+6.7%) | 9.40 ± 0.24 (+17.7%) |
| ascii | 24 | 13.33 ± 0.56 | 13.44 ± 0.25 (+0.8%) | 11.44 ± 0.48 (-14.2%) |
| escaped | 8 | 9.88 ± 0.19 | 10.38 ± 0.44 (+5.0%) | 9.89 ± 0.14 (0.0%) |
| escaped | 12 | 12.19 ± 0.28 | 11.84 ± 0.17 (-2.8%) | 11.24 ± 0.35 (-7.8%) |
| escaped | 24 | 18.38 ± 0.60 | 17.50 ± 0.48 (-4.8%) | 14.38 ± 0.51 (-21.8%) |
ArrayOperationPartialInlineSize is 0 here on both JDK 17 and 25. I think the partial inline needs AVX-512, so short getBytes calls the stub, like your Zen 4 case. If so, this would also hit Alder Lake/Raptor Lake consumer parts (AVX-512 fused off), and JDK-8376794 wouldn't help them. Your i7-11850H has AVX-512, which may be why it didn't show this. JDK 25 numbers are coming.
JDK 25, same setup (i7-8550U, 10 forks, close() inlining disabled), us/op:
| content | len | 3.x | dac5a8f8f | no-cutoff |
|---|--:|--:|--:|--:|
| ascii | 8 | 5.74 ± 0.20 | 6.07 ± 0.11 (+5.8%) | 7.74 ± 0.25 (+34.8%) |
| ascii | 12 | 6.89 ± 0.13 | 7.04 ± 0.14 (+2.1%) | 8.74 ± 0.38 (+26.8%) |
| ascii | 24 | 10.48 ± 0.19 | 10.15 ± 0.15 (-3.2%) | 11.88 ± 0.36 (+13.3%) |
| escaped | 8 | 6.69 ± 0.09 | 7.78 ± 0.13 (+16.3%) | 9.29 ± 0.27 (+38.8%) |
| escaped | 12 | 8.39 ± 0.21 | 9.18 ± 0.14 (+9.4%) | 10.82 ± 0.36 (+28.9%) |
| escaped | 24 | 11.54 ± 0.31 | 13.18 ± 0.57 (+14.2%) | 13.67 ± 0.79 (+18.5%) |
Without AVX-512, no-cutoff is slower than 3.x at every length up to 24 on JDK 25.
The close() control also changes the dac5a8f8f vs 3.x picture here. Mostly 3.x gets faster: escaped/24 went from 14.94 to 11.54. So my earlier JDK 25 escaped gains (-13 to -28%) were benchmark-shape dependent. Short-string comparisons with this bench probably need -XX:-TieredCompilation -Xbatch, as you suggested, or your serializer-shaped benchmark.
@pjfanning you're right, I withdraw the no-cutoff proposal. HotSpot only inlines short array copies with AVX-512 (UseAVX > 2); without it every short getBytes is a stub call, on every JDK including 27. Both machines I measured had AVX-512.
Serializer-shaped benchmark, i7-11850H with -XX:UseAVX=2, vs 3.x:
| | dac5a8f8f | no-cutoff |
|---|--:|--:|
| JDK 25, databind ≥ 3.1.7 | -0.1% | +7.1% |
| JDK 25, databind ≤ 3.1.6 | -10.1% | -7.9% |
| JDK 17, databind ≥ 3.1.7 | -1.9% | +0.7% |
| JDK 17, databind ≤ 3.1.6 | -5.3% | -4.6% |
dac5a8f8f as is looks right to me. Escaped strings not measured in this shape.
@pjfanning thanks a lot for folding #1681's check into this PR. dac5a8f8f is the version that fixes the serializer shape we care about. One note: #1681 also covers the _writeStringSegment(char[]) overload, which this PR leaves unchanged, so it's not fully superseded.
@franz1981 I re-ran with your suggestion (-XX:-TieredCompilation -Xbatch, no close() control), i7-8550U, 10 forks, us/op. "branch" = dac5a8f8f plus #1681's char[] check (local, not pushed yet).
JDK 17
| bench | content | len | 3.x | branch | change |
|---|---|--:|--:|--:|--:|
| utf8String | ascii | 8 | 8.48 ± 0.36 | 7.99 ± 0.26 | -5.8% |
| utf8String | ascii | 12 | 10.85 ± 0.44 | 9.53 ± 0.19 | -12.2% |
| utf8String | ascii | 24 | 16.01 ± 0.23 | 15.08 ± 0.54 | -5.8% |
| utf8String | escaped | 8 | 10.39 ± 0.22 | 10.39 ± 0.40 | 0.0% |
| utf8String | escaped | 12 | 14.60 ± 0.77 | 14.82 ± 0.70 | +1.5% |
| utf8String | escaped | 24 | 19.43 ± 0.45 | 20.84 ± 0.48 | +7.3% |
| utf8Chars | ascii | 8 | 8.03 ± 0.59 | 7.29 ± 0.31 | -9.2% |
| utf8Chars | ascii | 12 | 9.30 ± 0.36 | 8.41 ± 0.13 | -9.6% |
| utf8Chars | ascii | 24 | 12.74 ± 0.22 | 12.51 ± 0.22 | -1.8% |
| utf8Chars | escaped | 8 | 9.75 ± 0.71 | 9.72 ± 0.18 | -0.4% |
| utf8Chars | escaped | 12 | 11.28 ± 0.21 | 11.09 ± 0.23 | -1.6% |
| utf8Chars | escaped | 24 | 15.46 ± 0.33 | 15.34 ± 0.46 | -0.8% |
JDK 25
| bench | content | len | 3.x | branch | change |
|---|---|--:|--:|--:|--:|
| utf8String | ascii | 8 | 5.41 ± 0.17 | 8.42 ± 0.15 | +55.8% |
| utf8String | ascii | 12 | 6.70 ± 0.36 | 10.06 ± 0.21 | +50.3% |
| utf8String | ascii | 24 | 9.72 ± 0.12 | 15.13 ± 0.61 | +55.6% |
| utf8String | escaped | 8 | 7.70 ± 0.15 | 9.83 ± 0.27 | +27.7% |
| utf8String | escaped | 12 | 11.10 ± 0.21 | 13.20 ± 0.27 | +18.9% |
| utf8String | escaped | 24 | 15.53 ± 0.22 | 17.73 ± 0.99 | +14.1% |
| utf8Chars | ascii | 8 | 7.28 ± 0.55 | 6.56 ± 0.07 | -9.9% |
| utf8Chars | ascii | 12 | 8.29 ± 0.25 | 8.03 ± 0.32 | -3.0% |
| utf8Chars | ascii | 24 | 12.71 ± 0.36 | 11.49 ± 0.16 | -9.7% |
| utf8Chars | escaped | 8 | 8.05 ± 0.64 | 8.67 ± 0.84 | +7.8% |
| utf8Chars | escaped | 12 | 10.18 ± 0.40 | 11.34 ± 0.31 | +11.4% |
| utf8Chars | escaped | 24 | 14.18 ± 0.23 | 16.14 ± 0.66 | +13.9% |
char[] path with the same check is not. I haven't profiled it yet. It looks like a code-shape cliff without the tiered profile, which may matter after deopts even if nobody runs -TieredCompilation in production.char[]?@franz1981 I think I found the JDK 25 C2-only String cliff, using perfasm plus PrintInlining. It came from the branch's larger _writeStringSegment(String) (192 bytes vs 122 in 3.x):
1. It gets compiled standalone first, and then isn't inlined into writeString ("already compiled into a big method"). In 3.x it is.
2. Inside it, the stdEsc setup calls CharTypes.get7BitOutputEscapes(). The 7-byte wrapper is inlined, but the 27-byte 2-arg method it delegates to is not ("low call site frequency", no profile without tiered). That's a real call per segment, 4.6% of samples.
Proposed fix (local, not pushed): compare against the existing static final JsonGeneratorBase.DEFAULT_OUTPUT_ESCAPES instead, in both the String and char[] paths:
final boolean stdEsc = (escCodes == DEFAULT_OUTPUT_ESCAPES);
With that, _writeStringSegment is inlined into writeString again, like 3.x. A 10-fork run (JDK 25, C2-only) is in progress, and I'll post the numbers when it's done. It shouldn't matter with tiered compilation, where the call gets inlined, but it removes a call that only exists to compute a constant.
Caveat on all my numbers: my rig isn't great for benchmarking. It's an i7-8550U laptop (4 cores, no AVX-512) running JMH under WSL2 on Windows, with no frequency pinning or core isolation, and thermal throttling likely. Treat my results as indicative, especially small differences. Your pinned, fixed-frequency setup is more trustworthy.
Pushed 0f15709a9: #1681's char[] check, plus stdEsc = (escCodes == DEFAULT_OUTPUT_ESCAPES) in both paths (@franz1981 credited as co-author).
JDK 25, -XX:-TieredCompilation -Xbatch, utf8String, 10 forks, us/op:
| content | len | 3.x | with fix | change |
|---|--:|--:|--:|--:|
| ascii | 8 | 6.58 ± 0.30 | 6.65 ± 0.18 | +1.1% |
| ascii | 12 | 7.46 ± 0.11 | 7.95 ± 0.51 | +6.5% |
| ascii | 24 | 11.37 ± 0.22 | 11.14 ± 0.16 | -2.0% |
| escaped | 8 | 9.15 ± 0.42 | 8.58 ± 0.44 | -6.3% |
| escaped | 12 | 13.16 ± 0.41 | 13.18 ± 0.55 | +0.2% |
| escaped | 24 | 18.00 ± 0.36 | 17.82 ± 0.65 | -1.0% |
\ one outlier fork (10.22); the rest are 7.48-8.18.
The 50-56% cliff is gone; everything is within a few percent of 3.x. The usual caveat applies: 3.x itself moved between runs on my laptop (ascii/8 was 5.41 last time).
:chart_with_upwards_trend: Overall Code Coverage
| Metric | Coverage | Change |
|--------|----------|--------|
| Instructions | !coverage 86.86% | 📈 +0.330% |
| Branches | !branches 81.10% | 📈 +0.480% |
Overall project coverage from JaCoCo test results. Change values compare against the latest base branch build.
<!-- jacoco-coverage-comment -->
LGTM 🚀
franz1981
pjfanning
Speeds up the
UTF8JsonGeneratorASCII fast path for longerStringvalues (#1680). This is an alternative to #1681 that avoids its JDK 17 regression on long strings.For
Stringsegments of 32+ chars,_writeStringSegmentnow scans for the first char that needs escaping (same_outputEscapescheck, but no store per char), then bulk-copies the clean prefix withString.getBytes(int, int, byte[], int), which is a single array copy for Latin-1 Strings. Truncating to the low byte is exact because every scanned char is <= 0x7F. The bulk path lives in a separate small method (_writeUnescapedAsciiBulk), so_writeStringSegmentkeeps its current size and shape for short segments. Shorter segments and thechar[]path keep the existing fused check-and-copy loop: forchar[]there is no intrinsicchar[]->byte[]copy, so the split scan-then-copy is slower there.The 32-char cutoff comes from a crossover run (16-64 chars): at 32 chars JDK 17 is already ahead (48 vs 76 ns) and JDK 25 is even (43 vs 43 ns).
StringBulkCopy1680Testchecks thatUTF8JsonGeneratoroutput matchesWriterBasedJsonGeneratorforwriteName/writeString(String)andwriteString(char[], ...), with a special char at every position of 31/32/33/100-char values (escapes, control chars,/, DEL, Latin-1, non-Latin-1 chars whose low byte is plain ASCII such asŁU+0141,€, a surrogate pair), clean values of length 0-199, mixed and multi-segment values, across 6 generator configurations (default,ESCAPE_NON_ASCII,ESCAPE_FORWARD_SLASHES,quoteChar('\''),highestNonEscapedChar(0xFF),JsonpCharacterEscapes). If the scan lets a non-ASCII char through togetBytes, both tests fail.End-to-end
StringWriteBench.utf8String(200writeString(String)calls per op), i7-8550U/WSL2, 3 forks, us/op, change vs3.x:| JDK | content | len | 3.x | #1681 | this PR |
|---|---|--:|--:|--:|--:|
| 17 | ascii | 100 | 56.1 | 51.4 (-8%) | 42.6 (-24%) |
| 17 | ascii | 1000 | 375.1 | 397.5 (+6%) | 206.2 (-45%) |
| 17 | escaped | 100 | 45.3 | 45.3 (0%) | 30.5 (-33%) |
| 17 | escaped | 1000 | 392.8 | 395.4 (+1%) | 251.2 (-36%) |
| 25 | ascii | 100 | 53.2 | 40.6 (-24%) | 36.2 (-32%) |
| 25 | ascii | 1000 | 290.9 | 298.8 (+3%) | 211.7 (-27%) |
| 25 | escaped | 100 | 52.2 | 35.2 (-33%) | 32.3 (-38%) |
| 25 | escaped | 1000 | 373.9 | 300.4 (-20%) | 233.2 (-38%) |
12-char values are below the cutoff and within noise of
3.xon both JDKs. Full tables, including the short-value re-runs, are in the comment below; background analysis (perfasm/perfnorm, JDK 17 front-end stalls) is on #1681.🤖 Generated with Claude Code