Skip to content

Commit dc5fb37

Browse files
author
Abduqodiri Qurbonzoda
committed
Optimize finding index of last nonnull trie child in Vector and Builder
1 parent 133372f commit dc5fb37

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

kotlinx-collections-immutable/src/main/kotlin/kotlinx/collections/immutable/implementations/immutableList/PersistentVector.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,9 @@ internal class PersistentVector<E>(private val root: Array<Any?>,
250250
return newRoot
251251
}
252252

253-
// Could be val bufferLastIndex = root.indexOfLast { it != null }, but that isn't optimized enough
254253
var bufferLastIndex = MAX_BUFFER_SIZE_MINUS_ONE
255-
while (root[bufferLastIndex] == null) {
256-
bufferLastIndex -= 1
254+
if (root[bufferLastIndex] == null) {
255+
bufferLastIndex = indexSegment(rootSize() - 1, shift)
257256
}
258257

259258
val newRoot = root.copyOf(MAX_BUFFER_SIZE)

kotlinx-collections-immutable/src/main/kotlin/kotlinx/collections/immutable/implementations/immutableList/PersistentVectorBuilder.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,8 @@ class PersistentVectorBuilder<E>(private var vector: PersistentList<E>,
299299
}
300300

301301
var bufferLastIndex = MAX_BUFFER_SIZE_MINUS_ONE
302-
while (root[bufferLastIndex] == null) {
303-
bufferLastIndex -= 1
302+
if (root[bufferLastIndex] == null) {
303+
bufferLastIndex = indexSegment(rootSize() - 1, shift)
304304
}
305305

306306
val mutableRoot = makeMutable(root)

0 commit comments

Comments
 (0)