Skip to content

Commit 5ccdb2e

Browse files
committed
hash_internal is now primitive
1 parent 792d397 commit 5ccdb2e

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

src/main/java/org/truffleruby/core/array/ArrayNodes.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import org.truffleruby.core.format.FormatExceptionTranslator;
5656
import org.truffleruby.core.format.exceptions.FormatException;
5757
import org.truffleruby.core.format.pack.PackCompiler;
58+
import org.truffleruby.core.hash.HashingNodes;
5859
import org.truffleruby.core.kernel.KernelNodes;
5960
import org.truffleruby.core.kernel.KernelNodes.SameOrEqlNode;
6061
import org.truffleruby.core.kernel.KernelNodes.SameOrEqualNode;
@@ -1049,20 +1050,19 @@ protected Object fillFallback(VirtualFrame frame, RubyArray array, Object[] args
10491050

10501051
}
10511052

1052-
@CoreMethod(names = "hash_internal", visibility = Visibility.PRIVATE)
1053-
@ReportPolymorphism
1054-
public abstract static class HashNode extends ArrayCoreMethodNode {
1053+
@Primitive(name = "hash_internal")
1054+
@ImportStatic(ArrayGuards.class)
1055+
public abstract static class HashNode extends PrimitiveArrayArgumentsNode {
10551056

10561057
private static final int CLASS_SALT = 42753062; // random number, stops hashes for similar values but different classes being the same, static because we want deterministic hashes
10571058

10581059
@Specialization(limit = "storageStrategyLimit()")
10591060
protected long hash(VirtualFrame frame, RubyArray array,
10601061
@Bind("array.getStore()") Object store,
10611062
@CachedLibrary("store") ArrayStoreLibrary stores,
1062-
@Cached DispatchNode toHashNode,
1063-
@Cached ToLongNode toLongNode,
1064-
@Cached IntValueProfile arraySizeProfile,
1065-
@Cached LoopConditionProfile loopProfile) {
1063+
@Cached HashingNodes.ToHashByHashCode toHashByHashCode,
1064+
@Cached @Shared IntValueProfile arraySizeProfile,
1065+
@Cached @Shared LoopConditionProfile loopProfile) {
10661066
final int size = arraySizeProfile.profile(array.size);
10671067
long h = getContext().getHashing(this).start(size);
10681068
h = Hashing.update(h, CLASS_SALT);
@@ -1071,8 +1071,7 @@ protected long hash(VirtualFrame frame, RubyArray array,
10711071
try {
10721072
for (; loopProfile.inject(n < size); n++) {
10731073
final Object value = stores.read(store, n);
1074-
final long valueHash = toLongNode.execute(toHashNode.call(value, "hash"));
1075-
h = Hashing.update(h, valueHash);
1074+
h = Hashing.update(h, toHashByHashCode.execute(value));
10761075
TruffleSafepoint.poll(this);
10771076
}
10781077
} finally {

src/main/ruby/truffleruby/core/array.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ def flatten!(level = -1)
520520
def hash
521521
unless Primitive.array_can_contain_object?(self)
522522
# Primitive arrays do not need the recursion check
523-
return hash_internal
523+
return Primitive.hash_internal(self)
524524
end
525525

526526
hash_val = size
@@ -544,7 +544,7 @@ def hash
544544
begin
545545
objects[id] = true
546546

547-
hash_val = hash_internal
547+
hash_val = Primitive.hash_internal(self)
548548
ensure
549549
objects.delete id
550550
end
@@ -556,7 +556,7 @@ def hash
556556
objects[:__detect_outermost_recursion__] = true
557557
objects[id] = true
558558

559-
hash_val = hash_internal
559+
hash_val = Primitive.hash_internal(self)
560560

561561
# An inner version will raise to return back here, indicating that
562562
# the whole structure is recursive. In which case, abandon most of

0 commit comments

Comments
 (0)