Skip to content

Reduce per-float overhead of the decimal-preserving tree parser #1362

Closed

Description

@stevehu

Follow-up to #1357 (fixed by #1360 on master and #1361 on 2.x).

Problem

The wrapper parser that preserves lossy decimals (SchemaNodeReader.PrecisionParser.promote()) calls currentTokenLocation() (allocates a JsonLocation) and getString() for every floating-point token, even when the value is lossless and never promoted. The location is only used to avoid repeating the decision when Jackson calls several number getters for the same token.

Measurement

Review of #1360, master, end-to-end Schema.validate(String) of a 200,000-element array, best of 8 (base c83e4eb vs 3a67403):

Payload Before After Ratio
integers 3 ms 3 ms 1.0x
plain floats (123.25) 5 ms 17 ms 3.4x
random machine doubles 12 ms 15 ms 1.25x
lossy 19-digit decimals 10 ms 37 ms 3.7x
40-digit decimals 36 ms 83 ms 2.3x

The PR's own evidence (raw Jackson tree vs selective, JSON and YAML, JDK 8 to 25) shows similar ratios on both branches. Absolute times are small, but float-heavy instances are common.

Proposal

Avoid the per-token location lookup on the common lossless path, for example by resetting a per-token flag from an overridden nextToken()/nextName() (or comparing the parser's token count), so the decision is made once per token without allocating a location. Keep getString() only when the token needs a closer look (fast path: compare against Double.toString(value) as today).

Acceptance

  • No behaviour change: the existing oracle and Issue1357Test/Issue1350Test suites pass unchanged on master and 2.x.
  • Re-measure the same payloads (and JSON/YAML); plain and machine-double floats within roughly 1.5x of the native tree read.

Activity

  1. stevehu commented on Oct 24, 2026

    @stevehu
    ContributorAuthor

    Status after #1364 (464510c) and #1365 (746c512): partly done, keeping this open.

    On master (validate() of a 200,000-element array, best of 40) the change helped short decimals but not machine doubles:

    Payload Before After
    plain floats (123.25) 10 ms 5 ms
    machine doubles (16-17 digits) 14 ms (about 1.6x native) 14 ms
    19-digit decimals 43 ms 40 ms
    40-digit decimals 85 ms 87 ms

    On 2.x plain floats went from 25 to 13 ms and machine doubles were unchanged. The PR text already lists JSON machine doubles at about 2.3x native as the unmet part of the acceptance criteria.

    Remaining work, from the review of those PRs:

    1. Machine doubles. Tokens with 16-17 digits still allocate Double.toString and compare. A cheaper lossless check (for example a round-trip test, or a comparison that avoids building the shortest-repr string) is needed for this case.
    2. Boundary tests. Nothing pins the isShortDecimal()/matchesShortest() edges: 15 vs 16 digits, 16 vs 17 characters, a leading -, .5/5. YAML spellings, underscores, a leading +. nextNameMatch is also not exercised by Issue1362Test. My checks found no current bug (502,430 numbers in mixed nested documents matched the exact classification; 29 million random fixed-point tokens of at most 15 digits were lossless on JDK 8, 11, 17 and 21), but a limit change would not fail any checked-in test.
    3. YAML allocation. isShortDecimal() and matchesShortest() each call getStringCharacters(), and the YAML parser builds a new array on every call. Fetch it once per token. JSON parsers expose a shared buffer and are unaffected.
    4. Benchmark vs doc. The checked-in JMH benchmark uses 20,000 elements, while doc/issue-1362-performance.md reports JSON ratios for 200,000 elements from a separate harness that is not in the repository. State which numbers come from which harness, or add a 200,000-element JSON parameter.

    Acceptance (unchanged): plain and machine-double floats within roughly 1.5x of the native tree read, with the oracle and jackson-upgrade suites passing on both branches.

    🤖 Generated with Claude Code

  2. stevehu commented on Oct 24, 2026

    @stevehu
    ContributorAuthor

    Closing with the target only partly met, as an accepted trade-off.

    Done in #1364 (464510c) and #1365 (746c512): the per-token location lookup and string allocation are gone, the precision decision is cached per token, and short fixed-point tokens skip the string and decimal work. On master (validate() of a 200,000-element array, best of 40) plain floats went from 10 ms to 5 ms.

    Not met: JSON machine doubles (16-17 digit tokens) are unchanged at about 14 ms, about 1.6x a native tree read (2.3x in the PR's own table), against the roughly 1.5x goal. The remaining cost is small in absolute terms and only affects that payload; closing the gap needs a cleverer lossless check in the parser, which is the riskiest code here, so it is not worth the risk for now. If it ever matters, a round-trip check that avoids building the shortest-repr string is the place to start.

    The correctness-related leftovers (boundary tests, the YAML allocation and the benchmark/doc clarification) are tracked in #1368.

    🤖 Generated with Claude Code

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions