Repository navigation
Conversation
Adds parseUnsignedLong / parseLong / parseInt overloads to LongStringUtils that take the range [start, end) of a CharSequence and give the same result, or NumberFormatException, as their String counterparts on the same chars (Java 8 has no range-based CharSequence overloads of Long.parseLong / Integer.parseInt). They let a header value be parsed in place, from a view, without making a String first. parseUnsignedLong(String) now shares its 19-20 digit path with the range overload. LongStringUtilsRangeTest compares outcomes with the String parsers on edge cases (signs, Unicode digits, every length boundary, overflow) and on random input. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
🟢 Java Benchmark SLOs — All performance SLOs passed
PR vs. master results
Commit: Load and DaCapo benchmarks can be triggered manually in the GitLab pipeline. Results will appear in the Benchmarking Platform UI after completion. |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Integer.parseInt accepts exactly the chars Long.parseLong accepts with a value in int range, and rejects the rest with the same message, so parseInt no longer repeats parseLong's loop. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
More details
The signed parsers follow the JDK decimal algorithms, and the shared unsigned path preserves existing String behavior. Static review identified no actionable defects; runtime tests were not executed.
🤖 Bits Code Review · Commit d901668
bric3
left a comment
There was a problem hiding this comment.
noticed a few things that may need a fix, otherwise the change looks good to me.
| throw new NumberFormatException("s can't be null"); | ||
| } | ||
|
|
||
| int len = end - start; |
There was a problem hiding this comment.
issue: The decimal range parsers can read past the end of a view. SubSequence.of("9123", 1, 3) represents 12, but all three overloads return 123 for [0, 3) because SubSequence.charAt indexes the backing string directly.
Could we check 0 <= start <= end <= s.length() before parseUnsignedLong calculates the length and before parseLong reads any characters? parseInt would inherit the check, and a past-end view test would cover the case where charAt still succeeds.
There was a problem hiding this comment.
Good catch, fixed in acc49ff. All three overloads now check 0 <= start <= end <= s.length() before reading anything: parseUnsignedLong before computing the length, parseLong before the first char, and parseInt inherits it. A bad range is a NumberFormatException, matching the hex range parsers in this class, so callers that already catch it for malformed input don't see a new exception type. The new test uses a view whose charAt reads past its end, as SubSequence does. It fails on the previous code ([0, 3) of "12" backed by "9123" parsed as 123) and covers the 19+-digit unsigned path too.
| * {@link #parseUnsignedLong(String)} for the chars {@code [start, end)} of {@code s}: the same | ||
| * result, or a {@code NumberFormatException}, as for the same chars in a {@code String}, without | ||
| * making one. |
There was a problem hiding this comment.
suggestion: Could we scope the allocation claim to successful parses? The error paths can materialise the range when they build the exception message.
| * {@link #parseUnsignedLong(String)} for the chars {@code [start, end)} of {@code s}: the same | |
| * result, or a {@code NumberFormatException}, as for the same chars in a {@code String}, without | |
| * making one. | |
| * Parses the chars {@code [start, end)} of {@code s} as an unsigned long. The result or | |
| * {@code NumberFormatException} matches {@link #parseUnsignedLong(String)} for the same chars. | |
| * Successful parses read the range directly. |
There was a problem hiding this comment.
Applied in acc49ff, plus a sentence on out-of-range input.
| "Illegal leading minus sign on unsigned string %s.", | ||
| s.subSequence(start, end))); | ||
| } else { | ||
| if (len <= 18) { // Signed 64 bits max is 19 digits, so this always fits |
There was a problem hiding this comment.
nitpick: Both unsigned overloads use 18 as the cutoff for choosing the signed parser. Could we name it once and use the same constant in both overloads? I.e. avoid magic values.
There was a problem hiding this comment.
Named once in acc49ff as ALWAYS_SIGNED_DIGITS, used by both unsigned overloads.
| * The range-based {@code CharSequence} parsers give the same result, or a {@code | ||
| * NumberFormatException}, as their {@code String} counterparts on the same chars. | ||
| */ | ||
| class LongStringUtilsRangeTest { |
There was a problem hiding this comment.
suggestion: Could we add these decimal range cases to LongStringUtilsTest? That class already holds the hexadecimal range cases, so the parser contract tests would live together.
There was a problem hiding this comment.
Moved in acc49ff: the decimal range cases now live in LongStringUtilsTest next to the hex ones, and LongStringUtilsRangeTest is gone.
| "2147483647", | ||
| "2147483648", | ||
| "-2147483648", | ||
| "-2147483649", | ||
| "+2147483647", | ||
| "9223372036854775807", | ||
| "9223372036854775808", | ||
| "-9223372036854775808", | ||
| "-9223372036854775809", | ||
| "+9223372036854775807", |
There was a problem hiding this comment.
nitpick: Could we derive the exact signed limits from Integer.MIN_VALUE, Integer.MAX_VALUE, Long.MIN_VALUE and Long.MAX_VALUE? That would make the boundary each case exercises visible next to the overflow inputs.
There was a problem hiding this comment.
Done in acc49ff: the int and long boundary inputs come from the MIN_VALUE/MAX_VALUE constants (±1 via BigInteger), and the unsigned max from Long.toUnsignedString(-1L).
| } else { | ||
| throw new NumberFormatException("Empty input string"); | ||
| } |
There was a problem hiding this comment.
thought: Have you thought about using Maybe ? (this apply to other functions)
There was a problem hiding this comment.
Yes, and I think a Maybe-like construction could make sense here. I've been keeping this PR to the quick fix and putting that decision off rather than ruling it out.
For now these mirror the JDK's throwing parseLong/parseInt, so callers can switch to them directly. When we do take it up, there are two shapes:
- A default-value variant like this class's existing
parseUnsignedLongHexOrZero, e.g.parseLongOrDefault(s, start, end, fallback). No new dependency, so it fits dd-trace-api today, but the fallback is ambiguous when it's also a valid value. Maybeitself. Unambiguous, but it lives in internal-api, which dd-trace-api can't depend on until the toolbox moves into components (Extract the perf toolbox into a dependency-free components/toolbox module (FFE optimization) #13531).
Some views' charAt reads past their own end into a backing string, so a
range past the view parsed chars outside it: SubSequence.of("9123", 1, 3)
parsed [0, 3) as 123. The range is checked first, as a
NumberFormatException like the hex parsers'. The always-fits cutoff is
named once, and the range tests join LongStringUtilsTest with their
limits derived from the MIN/MAX constants.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
What Does This Do
Adds range-based
CharSequenceoverloads toLongStringUtils(an internal utility in dd-trace-api):parseUnsignedLong(CharSequence, start, end),parseLong(CharSequence, start, end)andparseInt(CharSequence, start, end). Each gives the same result, orNumberFormatException, as itsStringcounterpart on the same chars. No callers yet.Motivation
Part 1 of letting propagation parse header values in place. A Kafka consumer profile shows the header value
Strings as the tracer's largest allocation site; several of them (x-datadog-trace-id,x-datadog-parent-id,x-datadog-sampling-priority) are parsed into numbers and never kept. Parsing them from a view of the record's bytes needs number parsers that take aCharSequencerange, which Java 8 doesn't have (Long.parseLong(CharSequence, int, int, int)and itsIntegercounterpart are Java 9+).The parts, each its own PR:
CharSequencerange parsers.Additional Notes
parseLongandparseIntfollow the JDK's own algorithms for base 10 (optional+/-,Character.digit, accumulating negatively to detect overflow exactly), applied to the range.parseUnsignedLong(CharSequence, ...)mirrorsparseUnsignedLong(String):parseLongup to 18 chars, and the existing 19–20 digit unsigned path, now shared by both overloads (parseLongUnsignedDigits). TheStringoverload's results are unchanged.LongStringUtilsRangeTestcompares each overload's outcome with itsStringcounterpart (Long.parseLong,Integer.parseInt,parseUnsignedLong(String)) on the range of a largerStringBuilder: edge cases (empty, lone signs,+/-prefixes, spaces, letters, Unicode digits, every length boundary, signed and unsigned overflow), 50,000 random strings, and 20,000 random numbers of every magnitude.🤖 Generated with Claude Code