Skip to content

Fix #1776: local pointer in non-default Writer string scan loops - #1777

Closed
pjfanning wants to merge 4 commits into
FasterXML:3.xfrom
pjfanning:core-1776-local-ptr
Closed

pjfanning wants to merge 4 commits into
FasterXML:3.xfrom
pjfanning:core-1776-local-ptr

Conversation

@pjfanning

Copy link
Copy Markdown
Member

Fixes #1776. Builds on #1682.

Change

Applies #1682's _writeString2 fix to the two non-default WriterBasedJsonGenerator string scan loops: scan with a local pointer and write _outputTail back once, instead of ++_outputTail per char. C2 doesn't keep the field in a register across loop iterations, so the old loops stored it on every char.

  • _writeStringASCII(int, int): used with JsonWriteFeature.ESCAPE_NON_ASCII / highestNonEscapedChar(...)
  • _writeStringCustom(int): used with custom CharacterEscapes. The getEscapeSequence(c) call inside the loop has no access to generator state, so a local pointer is safe there too; no write-back is needed before the call (contrary to what I suggested in Use local pointer in non-default WriterBasedJsonGenerator string scan loops #1776).

The _writeSegment* and char[] counterparts already use local pointers.

Options

This branch is built on #1682 (head 7a7a330b3), so until #1682 is merged the diff here also shows #1682's commits. The only new commit is 4284dad. Two ways to handle it:

  1. Fold into Fix #1680: drop per-character escape-table load in Writer string loops #1682: cherry-pick 4284dad onto Fix #1680: drop per-character escape-table load in Writer string loops #1682's branch and close this PR. It is the same pattern and the same file, so it can be reviewed together.
  2. Hold back: merge Fix #1680: drop per-character escape-table load in Writer string loops #1682 first, then merge this later (e.g. 3.4.x, as Use local pointer in non-default WriterBasedJsonGenerator string scan loops #1776 suggests). These are non-default configurations, so there's no urgency. Once Fix #1680: drop per-character escape-table load in Writer string loops #1682 is merged this PR's diff shrinks to the one commit.

Either is fine with me.

Testing

JMH (JDK 17, i7-8550U)

writeString(String) x 200 ASCII values per op into a no-op Writer, us/op, #1682 vs this PR:

configuration len #1682 this PR change
JsonpCharacterEscapes 100 41.9 ± 1.8 29.8 ± 5.7 -29%
JsonpCharacterEscapes 1000 261.7 ± 20.1 211.7 ± 20.0 -19%
ESCAPE_NON_ASCII (3 forks) 100 44.3 ± 1.9 40.5 ± 2.1 -8%
ESCAPE_NON_ASCII (3 forks) 1000 252.6 ± 11.8 227.0 ± 14.9 -10%

No release-notes entry yet: it depends on which option is chosen and which version it lands in.

🤖 Generated with Claude Code

pjfanning and others added 4 commits October 15, 2026 10:08
…ring loops

Same as FasterXML#1681 for WriterBasedJsonGenerator: with the standard escape
table the fast scan in _writeString2, _writeSegment and
_writeString(char[]) tests constants instead of loading escCodes[c].
Custom quote char, escaped slashes and CharacterEscapes keep the lookup.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
C2 does not keep the _outputTail field in a register, so the loop stored it
on every character; scan with a local and write the field back once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…an loops

Same change as _writeString2 for _writeStringASCII (ESCAPE_NON_ASCII /
highestNonEscapedChar) and _writeStringCustom (CharacterEscapes).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pjfanning pjfanning added the performance Issue related to performance problems or enhancements label Oct 15, 2026
@github-actions

Copy link
Copy Markdown
Contributor

📈 Overall Code Coverage

Metric Coverage Change
Instructions coverage 86.49% 📉 -0.040%
Branches branches 80.49% 📉 -0.130%

Overall project coverage from JaCoCo test results. Change values compare against the latest base branch build.

@pjfanning

Copy link
Copy Markdown
Member Author

Folded into #1682 (commit 4284dad, results: #1682 (comment)), so closing this one.

@pjfanning pjfanning closed this Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Issue related to performance problems or enhancements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use local pointer in non-default WriterBasedJsonGenerator string scan loops

2 participants