Skip to content

Commit

Permalink
[Truffle] Update Fixnum/Complex#hash
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Fish committed Sep 15, 2016
1 parent 0634516 commit a9b6515
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
1 change: 0 additions & 1 deletion spec/truffle/tags/core/complex/hash_tags.txt

This file was deleted.

Expand Up @@ -40,6 +40,7 @@
import org.jcodings.Encoding;
import org.jcodings.specific.UTF8Encoding;
import org.jruby.common.IRubyWarnings;
import org.jruby.runtime.Helpers;
import org.jruby.runtime.Visibility;
import org.jruby.truffle.Layouts;
import org.jruby.truffle.RubyContext;
Expand Down Expand Up @@ -894,16 +895,20 @@ private static String gets(BufferedReader reader) throws InterruptedException {
@CoreMethod(names = "hash")
public abstract static class HashNode extends CoreMethodArrayArgumentsNode {

private static final int MURMUR_ARRAY_SEED = System.identityHashCode(HashNode.class);

@Specialization
public int hash(int value) {
// TODO(CS): should check this matches MRI
return value;
public long hash(int value) {
long h = Helpers.hashStart(getContext().getJRubyRuntime(), value);
h = Helpers.murmurCombine(h, MURMUR_ARRAY_SEED);

This comment has been minimized.

Copy link
@eregon

eregon Sep 15, 2016

Member

Should the seed be passed to start? I think that's how Array#hash did it, isn't it?

This comment has been minimized.

Copy link
@bjfish

bjfish Sep 15, 2016

Contributor

Yes, I think the seed should be first: 7ddb76a

return Helpers.hashEnd(h);
}

@Specialization
public int hash(long value) {
// TODO(CS): should check this matches MRI
return Long.valueOf(value).hashCode();
public long hash(long value) {
long h = Helpers.hashStart(getContext().getJRubyRuntime(), value);
h = Helpers.murmurCombine(h, MURMUR_ARRAY_SEED);
return Helpers.hashEnd(h);
}

@Specialization
Expand Down
Expand Up @@ -37,8 +37,10 @@
import org.jruby.truffle.language.dispatch.CallDispatchHeadNode;
import org.jruby.truffle.language.dispatch.DispatchHeadNodeFactory;
import org.jruby.truffle.language.methods.UnsupportedOperationBehavior;
import org.jruby.util.SipHashInline;

import java.math.BigInteger;
import java.nio.ByteBuffer;

@CoreClass("Fixnum")
public abstract class FixnumNodes {
Expand Down Expand Up @@ -1180,6 +1182,40 @@ public DynamicObject coerce(int a, Object b) {
return null; // Primitive failure
}

}


@Primitive(name = "fixnum_memhash")
public static abstract class FixnumMemhashPrimitiveNode extends PrimitiveArrayArgumentsNode {


@Specialization
public long memhashIntInt(int a, int b) {
return memhashLongLong((long) a, (long) b);
}

@Specialization
public long memhashLongInt(long a, int b) {
return memhashLongLong(a, (long) b);
}

@Specialization
public long memhash(int a, long b) {

This comment has been minimized.

Copy link
@eregon

eregon Sep 15, 2016

Member

These 3 specializations above are not needed, due to the implicit cast int=>long.

This comment has been minimized.

Copy link
@bjfish

bjfish Sep 15, 2016

Contributor

I've fixed this at 7ddb76a, thanks!

return memhashLongLong((long) a, b);
}


@Specialization
public long memhashLongLong(long a, long b) {
final ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES * 2);
buffer.putLong(a);
buffer.putLong(b);
return SipHashInline.hash24(getContext().getJRubyRuntime().getHashSeedK0(),
getContext().getJRubyRuntime().getHashSeedK1(), buffer.array());
}



}

@Primitive(name = "fixnum_pow")
Expand Down
2 changes: 1 addition & 1 deletion truffle/src/main/ruby/core/complex.rb
Expand Up @@ -321,7 +321,7 @@ def to_s
end

def hash
@real.hash ^ @imag.hash
Truffle.invoke_primitive :fixnum_memhash, @real.hash, @imag.hash
end

def inspect
Expand Down

0 comments on commit a9b6515

Please sign in to comment.