Skip to content

Commit 967cb98

Browse files
authored
Merge pull request #5299 from ChrisBr/hash/resize
Improve RubyHash#resize performance
2 parents ae777f0 + bd2e0c0 commit 967cb98

File tree

1 file changed

+10
-19
lines changed

1 file changed

+10
-19
lines changed

core/src/main/java/org/jruby/RubyHash.java

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -488,28 +488,18 @@ private static int MRIBucketIndex(final int h, final int length) {
488488
return ((h & HASH_SIGN_BIT_MASK) % length);
489489
}
490490

491-
private final synchronized void resize(int newCapacity) {
492-
final IRubyObject[] oldTable = entries;
493-
final IRubyObject[] newTable = new IRubyObject[newCapacity << 1];
491+
private final synchronized void resize(final int newCapacity) {
492+
final IRubyObject[] newEntries = new IRubyObject[newCapacity << 1];
494493
final int[] newBins = new int[newCapacity << 1];
495494
final int[] newHashes = new int[newCapacity];
496495
Arrays.fill(newBins, EMPTY_BIN);
497-
Arrays.fill(newHashes, EMPTY_BIN);
498-
IRubyObject key, value;
496+
System.arraycopy(entries, 0, newEntries, 0, entries.length);
497+
System.arraycopy(hashes, 0, newHashes, 0, hashes.length);
499498
int index, bin, hash;
500499

501500
for (int i = start; i < end; i++) {
502-
key = oldTable[i * NUMBER_OF_ENTRIES];
503-
value = oldTable[i * NUMBER_OF_ENTRIES + 1];
504-
if (key == null) {
505-
continue;
506-
}
507-
508-
newTable[i * NUMBER_OF_ENTRIES] = key;
509-
newTable[i * NUMBER_OF_ENTRIES + 1] = value;
510-
hash = hashValue(key);
511-
newHashes[i] = hash;
512-
bin = bucketIndex(hash, newBins.length);
501+
if (entries[i * NUMBER_OF_ENTRIES] == null) continue;
502+
bin = bucketIndex(hashes[i], newBins.length);
513503
index = newBins[bin];
514504
while(index != EMPTY_BIN) {
515505
bin = secondaryBucketIndex(bin, newBins.length);
@@ -518,10 +508,9 @@ private final synchronized void resize(int newCapacity) {
518508
newBins[bin] = i;
519509
}
520510

521-
hashes = null;
522511
bins = newBins;
523512
hashes = newHashes;
524-
entries = newTable;
513+
entries = newEntries;
525514
}
526515

527516
// ------------------------------
@@ -543,7 +532,7 @@ private static int secondaryBucketIndex(final int bucketIndex, final int length)
543532

544533
private void checkResize() {
545534
if (getLength() == end) {
546-
resize(getLength() << 1);
535+
resize(entries.length << 2);
547536
return;
548537
}
549538
return;
@@ -744,6 +733,7 @@ private final IRubyObject internalDeleteOpenAddressing(final int hash, final Ent
744733
otherValue = entries[(index * NUMBER_OF_ENTRIES) + 1];
745734
if (matchType.matches(key, value, otherKey, otherValue)) {
746735
bins[bin] = DELETED_BIN;
736+
hashes[index] = 0;
747737
entries[index * NUMBER_OF_ENTRIES] = null;
748738
entries[(index * NUMBER_OF_ENTRIES) + 1] = null;
749739
size--;
@@ -770,6 +760,7 @@ private final IRubyObject internalDeleteLinearSearch(final int hash, final Entry
770760
continue;
771761

772762
if (matchType.matches(key, value, otherKey, otherValue)) {
763+
hashes[index] = 0;
773764
entries[index * NUMBER_OF_ENTRIES] = null;
774765
entries[(index * NUMBER_OF_ENTRIES) + 1] = null;
775766
size--;

0 commit comments

Comments
 (0)