Skip to content
Browse files

Updating hashCode implementations.

Per discussion on the last commit's pull request [1], updating the
implementations of hashCode for RubyNil and RubyBoolean. Now the hashCode
behavior for nil and booleans will only change when consistent hashing is
enabled. Adds a hashCode instance variable to RubyBoolean and RubyNil that is
set in the constructor to the Object#hashCode value (using
System.identityHashCode) or a static value.

[1]: jruby#590
  • Loading branch information...
1 parent a74232a commit e96b458e9d6e1f24377c8e6338a6a8361186e846 @rdblue committed Mar 23, 2013
Showing with 28 additions and 9 deletions.
  1. +12 −6 src/org/jruby/RubyBoolean.java
  2. +12 −1 src/org/jruby/RubyNil.java
  3. +4 −2 test/org/jruby/test/TestRubyNil.java
View
18 src/org/jruby/RubyBoolean.java
@@ -46,13 +46,23 @@
*/
@JRubyClass(name={"TrueClass", "FalseClass"})
public class RubyBoolean extends RubyObject {
-
+
+ private final int hashCode;
+
RubyBoolean(Ruby runtime, boolean value) {
super(runtime,
(value ? runtime.getTrueClass() : runtime.getFalseClass()),
false); // Don't put in object space
if (!value) flags = FALSE_F;
+
+ if (RubyInstanceConfig.CONSISTENT_HASHING_ENABLED) {
+ // default to a fixed value
+ this.hashCode = value ? 155 : -48;
+ } else {
+ // save the object id based hash code;
+ this.hashCode = System.identityHashCode(this);
+ }
}
@Override
@@ -168,11 +178,7 @@ public RubyFixnum hash(ThreadContext context) {
@Override
public int hashCode() {
- if ((flags & FALSE_F) == 0) {
- return 155;
- } else {
- return -48;
- }
+ return hashCode;
}
@Override
View
13 src/org/jruby/RubyNil.java
@@ -45,9 +45,20 @@
*/
@JRubyClass(name="NilClass")
public class RubyNil extends RubyObject {
+
+ private final int hashCode;
+
public RubyNil(Ruby runtime) {
super(runtime, runtime.getNilClass(), false);
flags |= NIL_F | FALSE_F;
+
+ if (RubyInstanceConfig.CONSISTENT_HASHING_ENABLED) {
+ // default to a fixed value
+ this.hashCode = 34;
+ } else {
+ // save the object id based hash code;
+ this.hashCode = System.identityHashCode(this);
+ }
}
public static final ObjectAllocator NIL_ALLOCATOR = new ObjectAllocator() {
@@ -189,7 +200,7 @@ public RubyFixnum hash(ThreadContext context) {
@Override
public int hashCode() {
- return 34;
+ return hashCode;
}
@Override
View
6 test/org/jruby/test/TestRubyNil.java
@@ -104,10 +104,12 @@ public void testOpXOr() {
public void testHash() {
IRubyObject hash = RuntimeHelpers.invoke(
runtime.getCurrentContext(), rubyNil, "hash");
- assertEquals(RubyFixnum.newFixnum(runtime, 34), hash);
+ assertEquals(RubyFixnum.newFixnum(
+ runtime, System.identityHashCode(rubyNil)), hash);
}
public void testHashCode() {
- assertEquals(34, rubyNil.hashCode());
+ // should be the default Object#hashCode()
+ assertEquals(System.identityHashCode(rubyNil), rubyNil.hashCode());
}
}

0 comments on commit e96b458

Please sign in to comment.
Something went wrong with that request. Please try again.