Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API: Speed up Timestamps#toHumanString #12447

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

suneet-s
Copy link

@suneet-s suneet-s commented Mar 3, 2025

String#format uses regexes internally which can be expensive when used repeatedly. This PR switches out the implementation for a char array implementation that pads zeros as needed.

A simple benchmark was written to compare these 2 implementations which shows an improvement from ~ 300ns per op to 15ns per op

Benchmark                                    (monthOrdinal)  Mode  Cnt    Score     Error  Units
DateFormattingBenchmark.charArrayFormat               0  avgt   10   17.580 ±   5.029  ns/op
DateFormattingBenchmark.charArrayFormat               1  avgt   10   16.082 ±   0.220  ns/op
DateFormattingBenchmark.charArrayFormat              12  avgt   10   13.504 ±   0.151  ns/op
DateFormattingBenchmark.charArrayFormat              15  avgt   10   13.541 ±   0.684  ns/op
DateFormattingBenchmark.charArrayFormat             123  avgt   10   17.616 ±   5.112  ns/op
DateFormattingBenchmark.charArrayFormat            1234  avgt   10   13.350 ±   0.544  ns/op
DateFormattingBenchmark.charArrayFormat           12345  avgt   10   13.656 ±   0.128  ns/op
DateFormattingBenchmark.stringFormat                      0  avgt   10  296.576 ±   1.027  ns/op
DateFormattingBenchmark.stringFormat                      1  avgt   10  356.724 ± 277.416  ns/op
DateFormattingBenchmark.stringFormat                     12  avgt   10  320.441 ±   1.802  ns/op
DateFormattingBenchmark.stringFormat                     15  avgt   10  298.042 ±  11.098  ns/op
DateFormattingBenchmark.stringFormat                    123  avgt   10  302.227 ±  11.005  ns/op
DateFormattingBenchmark.stringFormat                   1234  avgt   10  309.586 ±  11.386  ns/op
DateFormattingBenchmark.stringFormat                  12345  avgt   10  305.583 ±   1.682  ns/op

String#format uses regexes internally which can be expensive when used repeatedly.
This PR switches out the implementation for a simple char array implementation that
pads zeros as needed.

A simple benchmark was written to compare these 2 implementations which shows an
improvement from ~ 300ns per op to 15ns per op

```
Benchmark                                    (monthOrdinal)  Mode  Cnt    Score     Error  Units
DateFormattingBenchmark.charArrayFormat               0  avgt   10   17.580 ±   5.029  ns/op
DateFormattingBenchmark.charArrayFormat               1  avgt   10   16.082 ±   0.220  ns/op
DateFormattingBenchmark.charArrayFormat              12  avgt   10   13.504 ±   0.151  ns/op
DateFormattingBenchmark.charArrayFormat              15  avgt   10   13.541 ±   0.684  ns/op
DateFormattingBenchmark.charArrayFormat             123  avgt   10   17.616 ±   5.112  ns/op
DateFormattingBenchmark.charArrayFormat            1234  avgt   10   13.350 ±   0.544  ns/op
DateFormattingBenchmark.charArrayFormat           12345  avgt   10   13.656 ±   0.128  ns/op
DateFormattingBenchmark.stringFormat                      0  avgt   10  296.576 ±   1.027  ns/op
DateFormattingBenchmark.stringFormat                      1  avgt   10  356.724 ± 277.416  ns/op
DateFormattingBenchmark.stringFormat                     12  avgt   10  320.441 ±   1.802  ns/op
DateFormattingBenchmark.stringFormat                     15  avgt   10  298.042 ±  11.098  ns/op
DateFormattingBenchmark.stringFormat                    123  avgt   10  302.227 ±  11.005  ns/op
DateFormattingBenchmark.stringFormat                   1234  avgt   10  309.586 ±  11.386  ns/op
DateFormattingBenchmark.stringFormat                  12345  avgt   10  305.583 ±   1.682  ns/op
```
@github-actions github-actions bot added the API label Mar 3, 2025
@suneet-s suneet-s marked this pull request as draft March 3, 2025 21:36
@suneet-s suneet-s marked this pull request as ready for review March 3, 2025 21:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant