Skip to content

perf: speed up timestamp parsing in DateTimes.of and SegmentId - #20974

Open
tyrobro wants to merge 3 commits into
apache:masterfrom
tyrobro:perf/coordinator-segment-metadata-cache-cpu
Open

tyrobro wants to merge 3 commits into
apache:masterfrom
tyrobro:perf/coordinator-segment-metadata-cache-cpu

Conversation

@tyrobro

@tyrobro tyrobro commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Related to #20927 (scoped by the maintainer to SegmentId.tryParse and DateTimes.of via SegmentRecord.fromResultSet). This does not fully resolve that issue.

Description

Draft PR for the two method-level optimizations suggested on #20927. One shows a small measurable gain and the other does not, so I'd like guidance before taking it further. Behavior, config, metrics and wire formats are unchanged.

DateTimes.of(String) fast path

Added a fast path for the canonical 24-character yyyy-MM-dd'T'HH:mm:ss.SSS'Z' form. Anything else (null, no millis, non-Z offset, date-only, epoch millis, out-of-range fields, non-digit characters) falls through to the existing logic unchanged.

SegmentId.iteratePossibleParsingsWithDataSource

  • The same canonical-timestamp fast path for the interval endpoints, falling back to DateTimes.ISO_DATE_TIME.
  • Splitter/Joiner replaced with indexOf/lastIndexOf extraction (iterateAllPossibleParsings still uses them).

Tests and benchmarks

Unit tests for boundary values and fallback paths, plus two JMH benchmarks (DateTimesOfBenchmark, SegmentIdParseBenchmark) that compare against a frozen copy of the old implementation in the same run.

Benchmark results

JMH 1.37, JDK 27, Windows laptop, 3 forks x (5x1s warmup + 10x1s measurement). Each op parses 1000 inputs.

DateTimes.of (measured before I inlined the digit helpers into one method; a re-run on the current code is pending):

Scenario current (ns/op) legacy (ns/op)
canonical 703,263 ± 28,941 741,172 ± 24,094
non-canonical offset (fallback) 695,960 ± 21,489 718,185 ± 18,695
epoch millis (fallback) 1,077,660 ± 32,012 1,122,976 ± 39,607

That is roughly 3-5% faster, and the fallback paths are not slower.

SegmentId.tryParse (latest run, with both the fast path and indexOf splitting):

Scenario current (ns/op) legacy (ns/op)
no explicit partition 962,868 ± 18,994 993,186 ± 29,586
explicit partition 1,066,829 ± 36,940 1,048,268 ± 41,082
underscore in version 1,146,575 ± 32,952 1,075,170 ± 48,880

This is about 3% faster, level, and about 6% slower, with overlapping or barely separated intervals. I don't consider it a demonstrated speedup.

Issues encountered

  • An earlier version of the SegmentId fast path that used small helper methods was 4-16% slower. Inlining it into one method did not fix that.
  • Adding the indexOf splitting only moved the numbers slightly.
  • One run showed ~27% gain for current, but the frozen legacy baseline was ~40% slower than in every other run. I treated that as machine noise and discarded it.
  • In this micro-benchmark, much of the time may go to constructing and interning Interval/SegmentId objects rather than parsing, so it may understate the gain on the Coordinator sync path. I have not verified this.

Questions for reviewers

  • Is there a benchmark or profile closer to the sync path (e.g. parsing ~100K segment IDs plus SegmentRecord construction) that I should use?
  • Should I keep only the DateTimes.of change, keep both since behavior is identical, or drop this?

Release note

None for now. This is a CPU-only change with no user-visible behavior. I'll revisit if it becomes mergeable.


Key changed/added classes in this PR
  • DateTimes
  • SegmentId
  • DateTimesOfBenchmark
  • SegmentIdParseBenchmark

This PR has:

  • been self-reviewed.
  • added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.
  • added unit tests or modified existing tests to cover new code paths, ensuring the threshold for code coverage is met.

@FrankChen021 FrankChen021 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🟢 Approval recommended

The timestamp fast paths preserve the inspected canonical, fallback, boundary, invalid-input, and segment-delimiter behavior. The whitespace-only line is non-blocking and does not affect runtime behavior.

Reviewed 6 of 6 changed files; finding in 1 file.

Validation: scoped git diff --check reported trailing whitespace at DateTimes.java:208; no tests or builds were run.

Severity Findings
P0 0
P1 0
P2 0
P3 1
Total 1

This is an automated review by Codex GPT-5.6-Luna(max)

After addressing the findings or replying to the comments, you can request another review from me to trigger a new automated review.

}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

[P3] Remove trailing whitespace

Finding: The added blank line contains spaces, so scoped git diff --check reports trailing whitespace at this line.

Suggestion: Remove the spaces from the blank line.

@FrankChen021 FrankChen021 added the Approval Recommended Used when AI completes a review and reports no findings label Nov 28, 2026
@tyrobro

tyrobro commented Nov 29, 2026

Copy link
Copy Markdown
Contributor Author

hi, could I get another review now?

@FrankChen021 FrankChen021 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🟢 Approval recommended

The current head is ready to merge. The timestamp fast paths retain existing fallback behavior, and the SegmentId delimiter rewrite preserves the prior version/partition parsing semantics. The prior whitespace-only finding is resolved.

Reviewed 6 of 6 changed files. The incremental diff was checked first, followed by the complete current merge-base diff and relevant surrounding code.

Validation: git diff --check passed. Narrow Joda and Guava checks confirmed representative parser and delimiter behavior; no Maven tests or builds were run.


This is an automated review by Codex GPT-5.6-Luna(max)

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

Labels

Approval Recommended Used when AI completes a review and reports no findings

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants