Skip to content

Commit 48837a1

Browse files
author
Abduqodiri Qurbonzoda
committed
Avoid hashCode calculation in map TrieNode collision functions
1 parent 01838db commit 48837a1

File tree

1 file changed

+16
-32
lines changed
  • core/commonMain/src/implementations/immutableMap

1 file changed

+16
-32
lines changed

core/commonMain/src/implementations/immutableMap/TrieNode.kt

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -337,10 +337,8 @@ internal class TrieNode<K, V>(
337337
return TrieNode(dataMap, nodeMap xor positionMask, newBuffer, owner)
338338
}
339339

340-
private fun collisionContainsKey(key: K): Boolean {
340+
private fun collisionContainsKey(keyHash: Int, key: K): Boolean {
341341
val collisionHash = keyAtIndex(0).hashCode()
342-
val keyHash = key.hashCode()
343-
344342
if (keyHash != collisionHash) {
345343
return false
346344
}
@@ -351,10 +349,8 @@ internal class TrieNode<K, V>(
351349
return false
352350
}
353351

354-
private fun collisionGet(key: K): V? {
352+
private fun collisionGet(keyHash: Int, key: K): V? {
355353
val collisionHash = keyAtIndex(0).hashCode()
356-
val keyHash = key.hashCode()
357-
358354
if (keyHash != collisionHash) {
359355
return null
360356
}
@@ -367,10 +363,8 @@ internal class TrieNode<K, V>(
367363
return null
368364
}
369365

370-
private fun collisionPut(key: K, value: V, shift: Int): ModificationResult<K, V>? {
366+
private fun collisionPut(keyHash: Int, key: K, value: V, shift: Int): ModificationResult<K, V>? {
371367
val collisionHash = keyAtIndex(0).hashCode()
372-
val keyHash = key.hashCode()
373-
374368
if (keyHash != collisionHash) {
375369
return makeNode(collisionHash, this, keyHash, key, value, shift, null).asInsertResult()
376370
}
@@ -389,10 +383,8 @@ internal class TrieNode<K, V>(
389383
return makeCollisionNode(newBuffer, null).asInsertResult()
390384
}
391385

392-
private fun mutableCollisionPut(key: K, value: V, shift: Int, mutator: PersistentHashMapBuilder<K, V>): TrieNode<K, V> {
386+
private fun mutableCollisionPut(keyHash: Int, key: K, value: V, shift: Int, mutator: PersistentHashMapBuilder<K, V>): TrieNode<K, V> {
393387
val collisionHash = keyAtIndex(0).hashCode()
394-
val keyHash = key.hashCode()
395-
396388
if (keyHash != collisionHash) {
397389
mutator.size++
398390
return makeNode(collisionHash, this, keyHash, key, value, shift, mutator.ownership)
@@ -423,10 +415,8 @@ internal class TrieNode<K, V>(
423415
return makeCollisionNode(newBuffer, mutator.ownership)
424416
}
425417

426-
private fun collisionRemove(key: K): TrieNode<K, V>? {
418+
private fun collisionRemove(keyHash: Int, key: K): TrieNode<K, V>? {
427419
val collisionHash = keyAtIndex(0).hashCode()
428-
val keyHash = key.hashCode()
429-
430420
if (keyHash != collisionHash) {
431421
return this
432422
}
@@ -439,10 +429,8 @@ internal class TrieNode<K, V>(
439429
return this
440430
}
441431

442-
private fun mutableCollisionRemove(key: K, mutator: PersistentHashMapBuilder<K, V>): TrieNode<K, V>? {
432+
private fun mutableCollisionRemove(keyHash: Int, key: K, mutator: PersistentHashMapBuilder<K, V>): TrieNode<K, V>? {
443433
val collisionHash = keyAtIndex(0).hashCode()
444-
val keyHash = key.hashCode()
445-
446434
if (keyHash != collisionHash) {
447435
return this
448436
}
@@ -455,10 +443,8 @@ internal class TrieNode<K, V>(
455443
return this
456444
}
457445

458-
private fun collisionRemove(key: K, value: V): TrieNode<K, V>? {
446+
private fun collisionRemove(keyHash: Int, key: K, value: V): TrieNode<K, V>? {
459447
val collisionHash = keyAtIndex(0).hashCode()
460-
val keyHash = key.hashCode()
461-
462448
if (keyHash != collisionHash) {
463449
return this
464450
}
@@ -471,10 +457,8 @@ internal class TrieNode<K, V>(
471457
return this
472458
}
473459

474-
private fun mutableCollisionRemove(key: K, value: V, mutator: PersistentHashMapBuilder<K, V>): TrieNode<K, V>? {
460+
private fun mutableCollisionRemove(keyHash: Int, key: K, value: V, mutator: PersistentHashMapBuilder<K, V>): TrieNode<K, V>? {
475461
val collisionHash = keyAtIndex(0).hashCode()
476-
val keyHash = key.hashCode()
477-
478462
if (keyHash != collisionHash) {
479463
return this
480464
}
@@ -489,7 +473,7 @@ internal class TrieNode<K, V>(
489473

490474
fun containsKey(keyHash: Int, key: K, shift: Int): Boolean {
491475
if (isCollision()) {
492-
return collisionContainsKey(key)
476+
return collisionContainsKey(keyHash, key)
493477
}
494478

495479
val keyPositionMask = 1 shl indexSegment(keyHash, shift)
@@ -508,7 +492,7 @@ internal class TrieNode<K, V>(
508492

509493
fun get(keyHash: Int, key: K, shift: Int): V? {
510494
if (isCollision()) {
511-
return collisionGet(key)
495+
return collisionGet(keyHash, key)
512496
}
513497

514498
val keyPositionMask = 1 shl indexSegment(keyHash, shift)
@@ -532,7 +516,7 @@ internal class TrieNode<K, V>(
532516

533517
fun put(keyHash: Int, key: K, value: @UnsafeVariance V, shift: Int): ModificationResult<K, V>? {
534518
if (isCollision()) {
535-
return collisionPut(key, value, shift)
519+
return collisionPut(keyHash, key, value, shift)
536520
}
537521

538522
val keyPositionMask = 1 shl indexSegment(keyHash, shift)
@@ -561,7 +545,7 @@ internal class TrieNode<K, V>(
561545

562546
fun mutablePut(keyHash: Int, key: K, value: @UnsafeVariance V, shift: Int, mutator: PersistentHashMapBuilder<K, V>): TrieNode<K, V> {
563547
if (isCollision()) {
564-
return mutableCollisionPut(key, value, shift, mutator)
548+
return mutableCollisionPut(keyHash, key, value, shift, mutator)
565549
}
566550

567551
val keyPositionMask = 1 shl indexSegment(keyHash, shift)
@@ -598,7 +582,7 @@ internal class TrieNode<K, V>(
598582

599583
fun remove(keyHash: Int, key: K, shift: Int): TrieNode<K, V>? {
600584
if (isCollision()) {
601-
return collisionRemove(key)
585+
return collisionRemove(keyHash, key)
602586
}
603587

604588
val keyPositionMask = 1 shl indexSegment(keyHash, shift)
@@ -629,7 +613,7 @@ internal class TrieNode<K, V>(
629613

630614
fun mutableRemove(keyHash: Int, key: K, shift: Int, mutator: PersistentHashMapBuilder<K, V>): TrieNode<K, V>? {
631615
if (isCollision()) {
632-
return mutableCollisionRemove(key, mutator)
616+
return mutableCollisionRemove(keyHash, key, mutator)
633617
}
634618

635619
val keyPositionMask = 1 shl indexSegment(keyHash, shift)
@@ -660,7 +644,7 @@ internal class TrieNode<K, V>(
660644

661645
fun remove(keyHash: Int, key: K, value: @UnsafeVariance V, shift: Int): TrieNode<K, V>? {
662646
if (isCollision()) {
663-
return collisionRemove(key, value)
647+
return collisionRemove(keyHash, key, value)
664648
}
665649

666650
val keyPositionMask = 1 shl indexSegment(keyHash, shift)
@@ -691,7 +675,7 @@ internal class TrieNode<K, V>(
691675

692676
fun mutableRemove(keyHash: Int, key: K, value: @UnsafeVariance V, shift: Int, mutator: PersistentHashMapBuilder<K, V>): TrieNode<K, V>? {
693677
if (isCollision()) {
694-
return mutableCollisionRemove(key, value, mutator)
678+
return mutableCollisionRemove(keyHash, key, value, mutator)
695679
}
696680

697681
val keyPositionMask = 1 shl indexSegment(keyHash, shift)

0 commit comments

Comments
 (0)