Skip to content

Commit 5dd3741

Browse files
committed
Read Entry#getNextInLookup() only once to make sure it's consistent
1 parent 7d04207 commit 5dd3741

File tree

2 files changed

+22
-28
lines changed

2 files changed

+22
-28
lines changed

src/main/java/org/truffleruby/core/hash/library/BucketsHashStore.java

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,7 @@ private static void resize(RubyHash hash) {
129129

130130
while (entry != null) {
131131
final int bucketIndex = getBucketIndex(entry.getHashed(), bucketsCount);
132-
Entry previousInLookup = newEntries[bucketIndex];
133-
134-
if (previousInLookup == null) {
135-
newEntries[bucketIndex] = entry;
136-
} else {
137-
while (previousInLookup.getNextInLookup() != null) {
138-
previousInLookup = previousInLookup.getNextInLookup();
139-
}
140-
141-
previousInLookup.setNextInLookup(entry);
142-
}
132+
appendToLookupChain(newEntries, entry, bucketIndex);
143133

144134
entry.setNextInLookup(null);
145135
entry = entry.getNextInSequence();
@@ -186,6 +176,25 @@ private static void removeFromLookupChain(Entry[] entries, int index, Entry entr
186176
}
187177
}
188178

179+
static void appendToLookupChain(Entry[] entries, Entry entry, int bucketIndex) {
180+
Entry previousInLookup = entries[bucketIndex];
181+
182+
if (previousInLookup == null) {
183+
entries[bucketIndex] = entry;
184+
} else {
185+
while (true) {
186+
final Entry nextInLookup = previousInLookup.getNextInLookup();
187+
if (nextInLookup == null) {
188+
break;
189+
} else {
190+
previousInLookup = nextInLookup;
191+
}
192+
}
193+
194+
previousInLookup.setNextInLookup(entry);
195+
}
196+
}
197+
189198
// endregion
190199
// region Messages
191200

@@ -420,11 +429,7 @@ protected RubyArray shift(RubyHash hash,
420429
Entry entry = entries[index];
421430
while (entry != null) {
422431
if (entry == first) {
423-
if (previous == null) {
424-
entries[index] = first.getNextInLookup();
425-
} else {
426-
previous.setNextInLookup(first.getNextInLookup());
427-
}
432+
removeFromLookupChain(entries, index, first, previous);
428433
break;
429434
}
430435

src/main/java/org/truffleruby/core/hash/library/PackedHashStoreLibrary.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,7 @@ private static void promoteToBuckets(RubyHash hash, Object[] store, int size) {
147147
lastInSequence = entry;
148148

149149
final int bucketIndex = BucketsHashStore.getBucketIndex(hashed, buckets.length);
150-
151-
Entry previousInLookup = buckets[bucketIndex];
152-
153-
if (previousInLookup == null) {
154-
buckets[bucketIndex] = entry;
155-
} else {
156-
while (previousInLookup.getNextInLookup() != null) {
157-
previousInLookup = previousInLookup.getNextInLookup();
158-
}
159-
160-
previousInLookup.setNextInLookup(entry);
161-
}
150+
BucketsHashStore.appendToLookupChain(buckets, entry, bucketIndex);
162151
}
163152

164153
hash.store = new BucketsHashStore(buckets);

0 commit comments

Comments
 (0)