RFR: 8366224: Introduce DecimalDigits.appendPair for efficient two-digit formatting and refactor DateTimeHelper
Shaojin Wen
swen at openjdk.org
Wed Aug 27 09:16:06 UTC 2025
On Sat, 23 Aug 2025 04:06:13 GMT, Shaojin Wen <swen at openjdk.org> wrote:
> This PR introduces a new efficient API for appending two-digit integers to StringBuilders and refactors DateTimeHelper to leverage this new functionality.
>
> Changes include:
>
> 1. New `appendPair` method for efficient two-digit integer formatting (00-99):
> - Added `AbstractStringBuilder.appendPair(int i)` with core implementation
> - Added `JavaLangAccess.appendPair(StringBuilder, int)` for internal access
> - Added `System.JavaLangAccessImpl.appendPair(StringBuilder, int)` bridge
> - Added `DecimalDigits.appendPair(StringBuilder, int)` public static utility method
> - Enhanced Javadoc documentation for all new methods
>
> 2. Refactored `DateTimeHelper` to use the new `DecimalDigits.appendPair`:
> - Updated `DateTimeHelper.formatTo` methods for `LocalDate` and `LocalTime`
> - Replaced manual formatting logic with the new efficient two-digit appending
> - Improved code clarity and consistency in date/time formatting
>
> These changes improve code clarity and performance when formatting two-digit numbers, particularly in date/time formatting scenarios.
I've run performance tests comparing the baseline (7b9969d) and this PR's branch (appendPair_202508) using the
`make test TEST="micro:java.time.ToStringBench` benchmark. The results show significant performance improvements across multiple time
formatting operations:
## Performance Comparison Results
| Benchmark | Baseline (7b9969d) | This PR (appendPair_202508) | Improvement |
|-----------|--------------------|-----------------------------|-------------|
| instantToString | 9.078 ± 0.504 ops/ms | 12.904 ± 3.810 ops/ms | ~42% faster |
| localDateTimeToString | 15.666 ± 1.916 ops/ms | 31.306 ± 5.758 ops/ms | ~100% faster |
| localDateToString | 23.257 ± 1.596 ops/ms | 24.976 ± 2.456 ops/ms | ~7.4% faster |
| localTimeToString | 22.727 ± 1.991 ops/ms | 37.266 ± 1.453 ops/ms | ~64% faster |
| zonedDateTimeToString | 13.094 ± 0.053 ops/ms | 24.040 ± 8.511 ops/ms | ~84% faster |
The most significant improvements are seen in `localDateTimeToString` and `localTimeToString` operations, which are now
approximately 2x faster. This validates that the changes in this PR provide meaningful performance benefits for time
formatting operations.
Tests were run on macOS ARM64 The performance gains are consistent with the optimization goals of this PR.
-------------
PR Comment: https://git.openjdk.org/jdk/pull/26911#issuecomment-3216680148
More information about the security-dev
mailing list