perf: direct long-to-chars rendering in visitFloat64#685
Draft
He-Pin wants to merge 1 commit intodatabricks:masterfrom
Draft
perf: direct long-to-chars rendering in visitFloat64#685He-Pin wants to merge 1 commit intodatabricks:masterfrom
He-Pin wants to merge 1 commit intodatabricks:masterfrom
Conversation
Replaces i.toString with writeLongDirect(i) using digit-pair lookup tables to write integer digits directly into CharBuilder's backing array, eliminating intermediate String allocation for integer doubles. Uses a right-to-left two-digits-at-a-time algorithm with DIGIT_TENS/ DIGIT_ONES lookup tables (same approach as java.lang.Long.toString). Handles edge cases: 0, Long.MinValue overflow, negative values. Adds regression test for large integer rendering (values > Int.MaxValue) to verify correctness for numbers up to 2^53. Upstream: jit branch commit d60ba61
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
When rendering numeric values in JSON output,
visitFloat64currently converts numbers toStringviajava.lang.Double.toString()and then copies the string character-by-character. For integer-valued doubles (common in Jsonnet), we can render the long value directly to the output buffer.Key Design Decision
Add a fast path in
visitFloat64that detects integer-valued doubles and renders them directly as long-to-chars, avoidingDouble.toString()string allocation and the intermediate String object.Modification
visitFloat64(check ifv == v.toLong)Double.toString()for non-integer valuesBenchmark Results
JMH (JVM, 3 iterations warmup + 3 measurement)
Analysis
The bench.02 improvement (-11.3%) reflects the many integer fields in the benchmark output. The realistic2 benefit (-12.0%) comes from integer port numbers, counts, and indices in the generated JSON. The tight error bars (±2.8) confirm this is a real structural optimization. Since most Jsonnet numbers are integer-valued, this fast path hits frequently.
References
d60ba61aResult
All 23 tests pass. All benchmarks positive, no regressions.