diff --git a/src/main/java/org/truffleruby/core/hash/library/CompactHashStore.java b/src/main/java/org/truffleruby/core/hash/library/CompactHashStore.java index 07f25af1ed41..2766e6989c73 100644 --- a/src/main/java/org/truffleruby/core/hash/library/CompactHashStore.java +++ b/src/main/java/org/truffleruby/core/hash/library/CompactHashStore.java @@ -160,14 +160,6 @@ private static int indexPosToValuePos(int[] index, int indexPos) { return index[indexPos + 1]; } - // For promoting from packed to compact - public void putHashKeyValue(int hashcode, Object key, Object value) { - int pos = kvStoreInsertionPos; - SetKvAtNode.insertIntoKv(this, key, value); - SetKvAtNode.insertIntoIndex(hashcode, pos + 1, index, - InlinedLoopConditionProfile.getUncached(), null); - } - @ExportMessage Object lookupOrDefault(Frame frame, RubyHash hash, Object key, PEBiFunction defaultNode, @Cached @Shared GetIndexPosForKeyNode getIndexPosForKeyNode, @@ -586,7 +578,7 @@ static boolean keyDoesntExist( } keyPos = store.kvStoreInsertionPos; - insertIntoKv(store, key, value); + insertIntoKv(store, keyPos, key, value); assert store.index[indexPos + 1] <= 0; store.index[indexPos] = keyHash; @@ -596,32 +588,31 @@ static boolean keyDoesntExist( if (indexResizingIsNeeded.profile(node, hash.size >= store.indexGrowthThreshold)) { // Resize the index array after insertion, as it invalidates indexPos - resizeIndex(store, node); + resizeIndex(store); } return true; } - private static void insertIntoIndex(int keyHash, int kvPos, int[] index, - InlinedLoopConditionProfile unavailableSlot, Node node) { - int pos = indexPosFromHashCode(keyHash, index.length); + private static void insertIntoIndex(int hashCode, int valuePos, int[] index) { + int pos = indexPosFromHashCode(hashCode, index.length); - while (unavailableSlot.profile(node, index[pos + 1] > INDEX_SLOT_UNUSED)) { + while (index[pos + 1] > INDEX_SLOT_UNUSED) { pos = incrementIndexPos(pos, index.length); } - index[pos] = keyHash; - index[pos + 1] = kvPos; + index[pos] = hashCode; + index[pos + 1] = valuePos; } - private static void insertIntoKv(CompactHashStore store, Object key, Object value) { - store.kvStore[store.kvStoreInsertionPos] = key; - store.kvStore[store.kvStoreInsertionPos + 1] = value; - store.kvStoreInsertionPos += 2; + private static void insertIntoKv(CompactHashStore store, int keyPos, Object key, Object value) { + store.kvStore[keyPos] = key; + store.kvStore[keyPos + 1] = value; + store.kvStoreInsertionPos = keyPos + 2; } @TruffleBoundary - private static void resizeIndex(CompactHashStore store, Node node) { + private static void resizeIndex(CompactHashStore store) { int[] oldIndex = store.index; int[] newIndex = new int[2 * oldIndex.length]; int newIndexCapacity = newIndex.length >> 1; @@ -629,10 +620,10 @@ private static void resizeIndex(CompactHashStore store, Node node) { int i = 0; for (; i < oldIndex.length; i += 2) { int hash = oldIndex[i]; - int kvPos = oldIndex[i + 1]; + int valuePos = oldIndex[i + 1]; - if (kvPos > INDEX_SLOT_UNUSED) { - insertIntoIndex(hash, kvPos, newIndex, InlinedLoopConditionProfile.getUncached(), node); + if (valuePos > INDEX_SLOT_UNUSED) { + insertIntoIndex(hash, valuePos, newIndex); } } @@ -645,6 +636,14 @@ private static void resizeKvStore(CompactHashStore store) { } } + /** For promoting from packed to compact */ + void insertHashKeyValue(int hashCode, Object key, Object value) { + int keyPos = kvStoreInsertionPos; + int valuePos = keyPos + 1; + SetKvAtNode.insertIntoKv(this, keyPos, key, value); + SetKvAtNode.insertIntoIndex(hashCode, valuePos, index); + } + public static final class CompactHashLiteralNode extends HashLiteralNode { @Child HashStoreLibrary hashes; diff --git a/src/main/java/org/truffleruby/core/hash/library/PackedHashStoreLibrary.java b/src/main/java/org/truffleruby/core/hash/library/PackedHashStoreLibrary.java index 8e1f22491a14..002052b92e11 100644 --- a/src/main/java/org/truffleruby/core/hash/library/PackedHashStoreLibrary.java +++ b/src/main/java/org/truffleruby/core/hash/library/PackedHashStoreLibrary.java @@ -158,7 +158,7 @@ private static void promoteToBuckets(RubyHash hash, Object[] store, int size) { private static void promoteToCompact(RubyHash hash, Object[] store) { CompactHashStore newStore = new CompactHashStore(MAX_ENTRIES); for (int n = 0; n < MAX_ENTRIES; n++) { - newStore.putHashKeyValue(getHashed(store, n), getKey(store, n), getValue(store, n)); + newStore.insertHashKeyValue(getHashed(store, n), getKey(store, n), getValue(store, n)); } hash.store = newStore; hash.size = MAX_ENTRIES;