Repository navigation
Conversation
See spring-projectsgh-52557 Signed-off-by: Raunak <[email protected]>
6673daa to
e620291
Compare
|
Thanks very much @raunakpandey1! |
|
Thanks @philwebb! |
|
While that fix is brilliant, it introduces noticeable overhead due to multiple intermediate BigDecimal allocations. I would suggest a slightly more verbose but significantly more efficient alternative: /**
* Microseconds.
*/
MICROS(ChronoUnit.MICROS, "us", (duration) -> {
long seconds = duration.getSeconds();
long nanos = duration.getNano();
if (seconds < 0) {
seconds++;
nanos -= 1_000_000_000L;
}
return Math.addExact(Math.multiplyExact(seconds, 1_000_000L), nanos / 1_000L);
}),This approach avoids heap allocations entirely, correctly truncates toward zero for negative durations (following the pattern in Duration.toMillis()), and safely throws an ArithmeticException on overflow via JIT-intrinsic Math.*Exact calls. What do you think? Thanks, András Dobrosi |
Can you provide an example? In what real-world situations is the overhead noticeable? |
|
I'm sharing this benchmark report with you: |
|
@dobrosi that's not a real-world situation, it's a non-representative microbenchmark. A real-world situation would be a Spring Boot app where the time taken by the code in question to convert durations to microseconds has a meaningful impact on performance. |
|
I understand your point of view, but in this technical matter, I prefer the more efficient approach. I consider this matter closed on my end. |
|
I don't think the microseconds |
|
Thank you. It's not just about the time; this solution doesn't quite fit this pattern. The original toMillis() implementation is heapless but buggy, while the proposed fix is correct but allocates on the heap. My solution (which, by the way, follows the OpenJDK approach for nanos, millis, etc.) handles everything in one place. dividedBy() is a useful method, but it's not needed here. Have a nice day, and good job! |
Switch to an implementation aligned with the standard `Duration` code to reduce heap usage. See spring-projectsgh-52557
Converting a
Durationto microseconds currently callstoNanos()before dividing by 1000. This throwsArithmeticExceptionwhen the nanosecond count exceedslong, even if the microsecond count fits. It affects bothDurationStyle.SIMPLE.printand duration-to-number conversion with@DurationUnit(MICROS).For example, on current
main(4.2.0-SNAPSHOT):Use
Duration.dividedBy(Duration.ofNanos(1000))to obtain the microsecond count directly. This supports the fulllongrange, preserves truncation toward zero for fractional microseconds, and still throws when the microsecond result itself overflows.Regression tests cover both signed limits (including fractional remainders), positive and negative submicrosecond truncation, genuine overflow, and number conversion through standalone and application conversion services.
Validation on Temurin JDK 25.0.5:
DurationStyleTestsandDurationToNumberConverterTests, with four regression failures caused by the intermediate nanosecond overflow.org.springframework.boot.convert.*passed, with no skips.:core:spring-boot:checkFormat,:core:spring-boot:checkstyleMain, and:core:spring-boot:checkstyleTestpassed.