Skip to content

Commit 4f8a1e1

Browse files
Optimize parser performance by introducing ConcatenatedListView
- Replace direct list concatenation in `ParserStructure.append()` with `ConcatenatedListView` for improved efficiency. - Add `ConcatenatedListView` implementation to lazily combine two lists without creating a new collection.
1 parent 5070925 commit 4f8a1e1

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright 2019-2025 JetBrains s.r.o. and contributors.
3+
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
4+
*/
5+
6+
package kotlinx.datetime.internal.format.parser
7+
8+
internal class ConcatenatedListView<T>(val list1: List<T>, val list2: List<T>) : AbstractList<T>() {
9+
override val size: Int
10+
get() = list1.size + list2.size
11+
12+
override fun get(index: Int): T = if (index < list1.size) list1[index] else list2[index - list1.size]
13+
}

core/common/src/internal/format/parser/Parser.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ internal class ParserStructure<in Output>(
4444
// TODO: O(size of the resulting parser ^ 2), but can be O(size of the resulting parser)
4545
internal fun <T> List<ParserStructure<T>>.concat(): ParserStructure<T> {
4646
fun <T> ParserStructure<T>.append(other: ParserStructure<T>): ParserStructure<T> = if (followedBy.isEmpty()) {
47-
ParserStructure(operations + other.operations, other.followedBy)
47+
ParserStructure(ConcatenatedListView(operations, other.operations), other.followedBy)
4848
} else {
4949
ParserStructure(operations, followedBy.map { it.append(other) })
5050
}

0 commit comments

Comments
 (0)