-
Notifications
You must be signed in to change notification settings - Fork 127
The performance of creating new datetime formats can be improved #585
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
Open
DmitryNekrasov
wants to merge
32
commits into
master
Choose a base branch
from
dmitry.nekrasov/feature/571-concat-list-view
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+371
−36
Open
Changes from 27 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
b8bbed8
Add JMH benchmarks for datetime format creation and performance evalu…
DmitryNekrasov 7fcb699
Introduce `ConcatenatedListView` for efficient list concatenation in …
DmitryNekrasov 941e426
Remove redundant type parameter `<T>` in `ParserStructure.simplify` m…
DmitryNekrasov 2e096a6
Add unconditionalModifications after each step of simplification
DmitryNekrasov 4ad707b
Rename `unconditionalModificationsForTails` to `unconditionalModifica…
DmitryNekrasov d4a6b02
Refactor `ParserStructure` logic to use `buildList` for streamlined l…
DmitryNekrasov 970068c
Refactor `ParserStructure` instantiation to improve formatting and re…
DmitryNekrasov 8f2bdeb
Reorder `unconditionalModifications` placement in `ParserStructure`.
DmitryNekrasov c35a73d
Passes all tests and it has benchmarc score increase on Python dateti…
DmitryNekrasov 27bd65c
Refactor `ParserStructure` logic to extract `mergeOperations` for cle…
DmitryNekrasov b6e80f8
Refactor `ParserStructure.concat` logic to simplify `mergeOperations`…
DmitryNekrasov 82ffb49
Add missing newline
DmitryNekrasov 93accde
Rename `operations` to `mergedOperations`.
DmitryNekrasov 17d0bb9
Simplify the ` mergedTails ` condition.
DmitryNekrasov 6e7d053
Reorder and simplify `unconditionalModifications` handling in `Parser…
DmitryNekrasov 9ce4e77
Refactor `ParserStructure` logic to streamline `mergedOperations` con…
DmitryNekrasov 82e6929
Fix typo in comment: "number consumers" → "number of consumers".
DmitryNekrasov 64ea6ef
Refactor `mergedOperations` construction to reuse `operationsToMerge`…
DmitryNekrasov d97ef89
Refactor `PythonDateTimeFormatBenchmark` into `CommonFormats`, update…
DmitryNekrasov 423b3b3
Add benchmark for building four-digit UTC offset format
DmitryNekrasov e14c8fb
Add benchmark for building RFC 1123 DateTime format
DmitryNekrasov 6aa9898
Add benchmark for building ISO DateTime with offset format
DmitryNekrasov 0c5a7a6
Refactor RFC 1123 and UTC offset format benchmarks to use inline form…
DmitryNekrasov 81266f4
Remove `ConcatenatedListView` as it is no longer in use.
DmitryNekrasov 6095101
Remove `SerialFormatBenchmark` as it is no longer in use.
DmitryNekrasov 28c36e6
Update `ParallelFormatBenchmark` warmup and measurement parameters.
DmitryNekrasov edd5d29
Update copyright year range in `Parser.kt` header.
DmitryNekrasov cc93dc0
Optimize concatenation of flat parsers.
DmitryNekrasov 096f970
Refactor `Parser` to streamline handling of accumulated operations.
DmitryNekrasov 61a5b06
Add `SerialFormatBenchmark` for evaluating large format serialization…
DmitryNekrasov d334373
Refactor `Parser` to replace `drop(1)` with an explicit loop for merg…
DmitryNekrasov 31dfbe5
Refactor `Parser` to apply `unconditionalModifications` after process…
DmitryNekrasov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| /* | ||
| * Copyright 2019-2025 JetBrains s.r.o. and contributors. | ||
| * Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file. | ||
| */ | ||
|
|
||
| @file:Suppress("unused") | ||
|
|
||
| package kotlinx.datetime | ||
|
|
||
| import kotlinx.datetime.format.* | ||
| import org.openjdk.jmh.annotations.* | ||
| import org.openjdk.jmh.infra.Blackhole | ||
| import java.util.concurrent.* | ||
|
|
||
| @Warmup(iterations = 20, time = 2) | ||
| @Measurement(iterations = 30, time = 2) | ||
| @BenchmarkMode(Mode.AverageTime) | ||
| @OutputTimeUnit(TimeUnit.NANOSECONDS) | ||
| @State(Scope.Benchmark) | ||
| @Fork(2) | ||
| open class CommonFormats { | ||
|
|
||
| @Benchmark | ||
| fun buildPythonDateTimeFormat(blackhole: Blackhole) { | ||
| val v = LocalDateTime.Format { | ||
| year() | ||
| char('-') | ||
| monthNumber() | ||
| char('-') | ||
| day() | ||
| char(' ') | ||
| hour() | ||
| char(':') | ||
| minute() | ||
| optional { | ||
| char(':') | ||
| second() | ||
| optional { | ||
| char('.') | ||
| secondFraction() | ||
| } | ||
| } | ||
| } | ||
| blackhole.consume(v) | ||
| } | ||
|
|
||
| @Benchmark | ||
| fun buildIsoDateTimeFormat(blackhole: Blackhole) { | ||
| val format = LocalDateTime.Format { | ||
| date(LocalDate.Format { | ||
| year() | ||
| char('-') | ||
| monthNumber() | ||
| char('-') | ||
| day() | ||
| }) | ||
| alternativeParsing({ char('t') }) { char('T') } | ||
| time(LocalTime.Format { | ||
| hour() | ||
| char(':') | ||
| minute() | ||
| alternativeParsing({}) { | ||
| char(':') | ||
| second() | ||
| optional { | ||
| char('.') | ||
| secondFraction(1, 9) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| blackhole.consume(format) | ||
| } | ||
|
|
||
| @Benchmark | ||
| fun buildFourDigitsUtcOffsetFormat(blackhole: Blackhole) { | ||
| val format = UtcOffset.Format { | ||
| offsetHours() | ||
| offsetMinutesOfHour() | ||
| } | ||
| blackhole.consume(format) | ||
| } | ||
|
|
||
| @Benchmark | ||
| fun buildRfc1123DateTimeFormat(blackhole: Blackhole) { | ||
| val format = DateTimeComponents.Format { | ||
| alternativeParsing({ | ||
| // the day of week may be missing | ||
| }) { | ||
| dayOfWeek(DayOfWeekNames.ENGLISH_ABBREVIATED) | ||
| chars(", ") | ||
| } | ||
| day(Padding.NONE) | ||
| char(' ') | ||
| monthName(MonthNames.ENGLISH_ABBREVIATED) | ||
| char(' ') | ||
| year() | ||
| char(' ') | ||
| hour() | ||
| char(':') | ||
| minute() | ||
| optional { | ||
| char(':') | ||
| second() | ||
| } | ||
| chars(" ") | ||
| alternativeParsing({ | ||
| chars("UT") | ||
| }, { | ||
| chars("Z") | ||
| }) { | ||
| optional("GMT") { | ||
| offset(UtcOffset.Format { | ||
| offsetHours() | ||
| offsetMinutesOfHour() | ||
| }) | ||
| } | ||
| } | ||
| } | ||
| blackhole.consume(format) | ||
| } | ||
|
|
||
| @Benchmark | ||
| fun buildIsoDateTimeOffsetFormat(blackhole: Blackhole) { | ||
| val format = DateTimeComponents.Format { | ||
| date(LocalDate.Format { | ||
| year() | ||
| char('-') | ||
| monthNumber() | ||
| char('-') | ||
| day() | ||
| }) | ||
| alternativeParsing({ | ||
| char('t') | ||
| }) { | ||
| char('T') | ||
| } | ||
| hour() | ||
| char(':') | ||
| minute() | ||
| char(':') | ||
| second() | ||
| optional { | ||
| char('.') | ||
| secondFraction(1, 9) | ||
| } | ||
| alternativeParsing({ | ||
| offsetHours() | ||
| }) { | ||
| offset(UtcOffset.Format { | ||
| alternativeParsing({ chars("z") }) { | ||
| optional("Z") { | ||
| offsetHours() | ||
| char(':') | ||
| offsetMinutesOfHour() | ||
| optional { | ||
| char(':') | ||
| offsetSecondsOfMinute() | ||
| } | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
| blackhole.consume(format) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| /* | ||
| * Copyright 2019-2025 JetBrains s.r.o. and contributors. | ||
| * Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file. | ||
| */ | ||
|
|
||
| @file:Suppress("unused") | ||
|
|
||
| package kotlinx.datetime | ||
|
|
||
| import kotlinx.datetime.format.alternativeParsing | ||
| import kotlinx.datetime.format.char | ||
| import org.openjdk.jmh.annotations.* | ||
| import org.openjdk.jmh.infra.Blackhole | ||
| import java.util.concurrent.TimeUnit | ||
|
|
||
| @Warmup(iterations = 10, time = 2) | ||
| @Measurement(iterations = 20, time = 2) | ||
| @BenchmarkMode(Mode.AverageTime) | ||
| @OutputTimeUnit(TimeUnit.NANOSECONDS) | ||
| @State(Scope.Benchmark) | ||
| @Fork(1) | ||
| open class ParallelFormatBenchmark { | ||
|
|
||
| @Param("2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12") | ||
| var n = 0 | ||
|
|
||
| @Benchmark | ||
| fun formatCreationWithAlternativeParsing(blackhole: Blackhole) { | ||
| val format = LocalDateTime.Format { | ||
| repeat(n) { | ||
| alternativeParsing( | ||
| { monthNumber() }, | ||
| { day() }, | ||
| primaryFormat = { hour() } | ||
| ) | ||
| char('@') | ||
| minute() | ||
| char('#') | ||
| second() | ||
| } | ||
| } | ||
| blackhole.consume(format) | ||
| } | ||
|
|
||
| @Benchmark | ||
| fun formatCreationWithNestedAlternativeParsing(blackhole: Blackhole) { | ||
| val format = LocalDateTime.Format { | ||
| repeat(n) { index -> | ||
| alternativeParsing( | ||
| { monthNumber(); char('-'); day() }, | ||
| { day(); char('/'); monthNumber() }, | ||
| primaryFormat = { year(); char('-'); monthNumber(); char('-'); day() } | ||
| ) | ||
|
|
||
| if (index and 1 == 0) { | ||
| alternativeParsing( | ||
| { | ||
| alternativeParsing( | ||
| { hour(); char(':'); minute() }, | ||
| { minute(); char(':'); second() }, | ||
| primaryFormat = { hour(); char(':'); minute(); char(':'); second() } | ||
| ) | ||
| }, | ||
| primaryFormat = { | ||
| year(); char('-'); monthNumber(); char('-'); day() | ||
| char('T') | ||
| hour(); char(':'); minute(); char(':'); second() | ||
| } | ||
| ) | ||
| } | ||
|
|
||
| char('|') | ||
| if (index % 3 == 0) { | ||
| char('|') | ||
| } | ||
|
|
||
| if (index and 2 == 0) { | ||
| alternativeParsing( | ||
| { char('Z') }, | ||
| { char('+'); hour(); char(':'); minute() }, | ||
| primaryFormat = { char('-'); hour(); char(':'); minute() } | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| blackhole.consume(format) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
drop(1)creates an additional new list, so I'd expect a manual iteration over the indices here instead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.