Skip to content

Parse decimal numbers from a CharSequence range (header optimization) - #13616

Open
dougqh wants to merge 3 commits into
masterfrom
dougqh/charsequence-parsers
Open

dougqh wants to merge 3 commits into
masterfrom
dougqh/charsequence-parsers

Conversation

@dougqh

@dougqh dougqh commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

What Does This Do

Adds range-based CharSequence overloads to LongStringUtils (an internal utility in dd-trace-api): parseUnsignedLong(CharSequence, start, end), parseLong(CharSequence, start, end) and parseInt(CharSequence, start, end). Each gives the same result, or NumberFormatException, as its String counterpart 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 a CharSequence range, which Java 8 doesn't have (Long.parseLong(CharSequence, int, int, int) and its Integer counterpart are Java 9+).

The parts, each its own PR:

  1. This PR: CharSequence range parsers.
  2. A byte-backed ASCII view type, with a "valid only during the call; copy to keep" contract.
  3. The propagation contract: classifiers that accept a transient value, and a carrier cache that can hold views.
  4. Datadog extraction parsing ids and sampling priority from views.
  5. The Kafka carrier handing views for those headers.

Additional Notes

  • parseLong and parseInt follow the JDK's own algorithms for base 10 (optional +/-, Character.digit, accumulating negatively to detect overflow exactly), applied to the range.
  • parseUnsignedLong(CharSequence, ...) mirrors parseUnsignedLong(String): parseLong up to 18 chars, and the existing 19–20 digit unsigned path, now shared by both overloads (parseLongUnsignedDigits). The String overload's results are unchanged.
  • LongStringUtilsRangeTest compares each overload's outcome with its String counterpart (Long.parseLong, Integer.parseInt, parseUnsignedLong(String)) on the range of a larger StringBuilder: 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

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>
@dougqh dougqh added tag: performance Performance related changes tag: no release notes Changes to exclude from release notes type: refactoring comp: api Tracer public API tag: ai generated Largely based on code generated by an AI or LLM labels Nov 21, 2026
@datadog-datadog-us1-prod

datadog-datadog-us1-prod Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

🎯 Code Coverage (details)
• Patch Coverage: 100.00%
• Overall Coverage: 59.37% (-1.59%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: acc49ff | Docs | Give us feedback!

@dd-octo-sts

dd-octo-sts Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

🟢 Java Benchmark SLOs — All performance SLOs passed

Suite Status
Startup 🟢 pass

SLO thresholds are defined here based on automatically generated metrics. A warning is raised when results are within 5% of the threshold.

PR vs. master results
Scenario Candidate master Δ (95% CI of mean)
startup:insecure-bank:iast:Agent 14.84 s 14.74 s [-0.3%; +1.6%] (no difference)
startup:insecure-bank:tracing:Agent 13.69 s 13.72 s [-1.0%; +0.6%] (no difference)
startup:petclinic:appsec:Agent 17.69 s 17.07 s [-0.7%; +7.9%] (no difference)
startup:petclinic:iast:Agent 17.37 s 17.48 s [-1.7%; +0.5%] (no difference)
startup:petclinic:profiling:Agent 16.87 s 16.78 s [-5.6%; +6.7%] (unstable)
startup:petclinic:sca:Agent 17.63 s 17.56 s [-0.7%; +1.4%] (no difference)
startup:petclinic:tracing:Agent 16.77 s 16.89 s [-1.9%; +0.4%] (no difference)

Commit: acc49ff1 · CI Pipeline · Benchmarking Platform UI


Load and DaCapo benchmarks can be triggered manually in the GitLab pipeline. Results will appear in the Benchmarking Platform UI after completion.

@dougqh
dougqh marked this pull request as ready for review November 23, 2026 20:58
@dougqh
dougqh requested a review from a team as a code owner November 23, 2026 20:58
@dougqh
dougqh requested review from bric3 and removed request for a team November 23, 2026 20:58
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-23T21:00:07.996936Z d901668 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

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>
@dougqh dougqh changed the title Parse decimal numbers from a CharSequence range Parse decimal numbers from a CharSequence range (quick perf fix) Nov 23, 2026
@dougqh dougqh changed the title Parse decimal numbers from a CharSequence range (quick perf fix) Parse decimal numbers from a CharSequence range (header optimization) Nov 23, 2026

@datadog-datadog-us1-prod datadog-datadog-us1-prod Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bits Code Review: PASS

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.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit d901668

@bric3
bric3 added this pull request to stack #13705 November 26, 2026 14:43

@bric3 bric3 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Override
public char charAt(int index) {
return this.str.charAt(this.beginIndex + index);
}
@Override
public int length() {
return this.endIndex - this.beginIndex;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +145 to +147
* {@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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Could we scope the allocation claim to successful parses? The error paths can materialise the range when they build the exception message.

Suggested change
* {@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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved in acc49ff: the decimal range cases now live in LongStringUtilsTest next to the hex ones, and LongStringUtilsRangeTest is gone.

Comment on lines +35 to +44
"2147483647",
"2147483648",
"-2147483648",
"-2147483649",
"+2147483647",
"9223372036854775807",
"9223372036854775808",
"-9223372036854775808",
"-9223372036854775809",
"+9223372036854775807",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Comment on lines 170 to 172
} else {
throw new NumberFormatException("Empty input string");
}

@bric3 bric3 Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought: Have you thought about using Maybe ? (this apply to other functions)

@dougqh dougqh Nov 30, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

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>

This branch has not been deployed

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

Labels

comp: api Tracer public API tag: ai generated Largely based on code generated by an AI or LLM tag: no release notes Changes to exclude from release notes tag: performance Performance related changes type: refactoring

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants