Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 863f0123fcb0
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: f5bb3a95d358
Choose a head ref
  • 2 commits
  • 6 files changed
  • 1 contributor

Commits on Feb 22, 2018

  1. Align Integer methods with CRuby.

    This binds several methods like `<` that were going through
    Comparable for all Integer types. Numeric algorithms that make
    heavy use of those comparisons appear to be as much as 2x faster.
    headius committed Feb 22, 2018
    Copy the full SHA
    c27fabd View commit details
  2. Copy the full SHA
    f5bb3a9 View commit details
16 changes: 16 additions & 0 deletions core/src/main/java/org/jruby/RubyFixnum.java
Original file line number Diff line number Diff line change
@@ -1383,6 +1383,22 @@ public RubyRational convertToRational() {
return RubyRational.newRationalRaw(runtime, this, one(runtime));
}

@Override
public IRubyObject remainder(ThreadContext context, IRubyObject y) {
RubyFixnum x = this;
JavaSites.FixnumSites sites = sites(context);
IRubyObject z = sites.op_mod.call(context, this, this, y);

if ((!Helpers.rbEqual(context, z, RubyFixnum.zero(context.runtime), sites.op_equal).isTrue()) &&
((x.isNegative() &&
((RubyInteger) y).isPositive()) ||
(x.isPositive() &&
((RubyInteger) y).isNegative()))) {
return sites.op_minus.call(context, z, z, y);
}
return z;
}

private static JavaSites.FixnumSites sites(ThreadContext context) {
return context.sites.Fixnum;
}
28 changes: 17 additions & 11 deletions core/src/main/java/org/jruby/RubyInteger.java
Original file line number Diff line number Diff line change
@@ -610,16 +610,9 @@ public IRubyObject gcd(ThreadContext context, IRubyObject other) {

// MRI: rb_int_fdiv_double and rb_int_fdiv in one
@Override
@JRubyMethod(name = "fdiv")
public IRubyObject fdiv(ThreadContext context, IRubyObject y) {
RubyInteger x = this;
if (y instanceof RubyInteger && !((RubyInteger) y).isZero()) {
IRubyObject gcd = gcd(context, y);
if (!((RubyInteger) gcd).isZero()) {
x = (RubyInteger) div(context, gcd);
y = ((RubyInteger) y).div(context, gcd);
}
}
return x.fdivDouble(context, y);
return fdivDouble(context, y);
}

public abstract IRubyObject fdivDouble(ThreadContext context, IRubyObject y);
@@ -678,8 +671,8 @@ public IRubyObject denominator(ThreadContext context) {
return RubyFixnum.one(context.runtime);
}

@JRubyMethod(name = "to_s")
@Override
@JRubyMethod(name = {"to_s", "inspect"})
public abstract RubyString to_s();

@JRubyMethod(name = "to_s")
@@ -740,7 +733,7 @@ public IRubyObject magnitude(ThreadContext context) {
return abs(context);
}

@JRubyMethod(name = "==")
@JRubyMethod(name = {"==", "==="})
@Override
public abstract IRubyObject op_equal(ThreadContext context, IRubyObject other);

@@ -785,21 +778,34 @@ boolean isOne() {
return getBigIntegerValue().equals(BigInteger.ONE);
}

@JRubyMethod(name = ">")
public IRubyObject op_gt(ThreadContext context, IRubyObject other) {
return RubyComparable.op_gt(context, this, other);
}

@JRubyMethod(name = "<")
public IRubyObject op_lt(ThreadContext context, IRubyObject other) {
return RubyComparable.op_lt(context, this, other);
}

@JRubyMethod(name = ">=")
public IRubyObject op_ge(ThreadContext context, IRubyObject other) {
return RubyComparable.op_ge(context, this, other);
}

@JRubyMethod(name = "<=")
public IRubyObject op_le(ThreadContext context, IRubyObject other) {
return RubyComparable.op_le(context, this, other);
}
@JRubyMethod(name = "remainder")
public IRubyObject remainder(ThreadContext context, IRubyObject dividend) {
return context.nil;
}

@JRubyMethod(name = "divmod")
public IRubyObject divmod(ThreadContext context, IRubyObject other) {
return context.nil;
}

public IRubyObject op_uminus() {
return op_uminus(getRuntime().getCurrentContext());
17 changes: 16 additions & 1 deletion core/src/main/java/org/jruby/runtime/Helpers.java
Original file line number Diff line number Diff line change
@@ -1879,7 +1879,22 @@ public static Block getBlock(Ruby runtime, ThreadContext context, IRubyObject se
public static RubyBoolean rbEqual(ThreadContext context, IRubyObject a, IRubyObject b) {
Ruby runtime = context.runtime;
if (a == b) return runtime.getTrue();
IRubyObject res = invokedynamic(context, a, OP_EQUAL, b);
IRubyObject res = sites(context).op_equal.call(context, a, a, b);
return runtime.newBoolean(res.isTrue());
}

/**
* Equivalent to rb_equal in MRI
*
* @param context
* @param a
* @param b
* @return
*/
public static RubyBoolean rbEqual(ThreadContext context, IRubyObject a, IRubyObject b, CallSite equal) {
Ruby runtime = context.runtime;
if (a == b) return runtime.getTrue();
IRubyObject res = equal.call(context, a, a, b);
return runtime.newBoolean(res.isTrue());
}

2 changes: 2 additions & 0 deletions core/src/main/java/org/jruby/runtime/JavaSites.java
Original file line number Diff line number Diff line change
@@ -236,6 +236,7 @@ public static class FixnumSites {
public final CheckedSites checked_op_and = new CheckedSites("&");
public final CheckedSites checked_op_or = new CheckedSites("|");
public final CheckedSites checked_op_xor = new CheckedSites("^");
public final CallSite op_equal = new FunctionalCachingCallSite("==");
}

public static class BignumSites {
@@ -339,6 +340,7 @@ public static class TypeConverterSites {

public static class HelpersSites {
public final CallSite hash = new FunctionalCachingCallSite("hash");
public final CallSite op_equal = new FunctionalCachingCallSite("==");

public final ThreadContext.RecursiveFunctionEx<Ruby> recursive_hash = new ThreadContext.RecursiveFunctionEx<Ruby>() {
public IRubyObject call(ThreadContext context, Ruby runtime, IRubyObject obj, boolean recur) {
3 changes: 3 additions & 0 deletions core/src/main/java/org/jruby/runtime/MethodIndex.java
Original file line number Diff line number Diff line change
@@ -43,6 +43,7 @@
import org.jruby.runtime.callsite.LtCallSite;
import org.jruby.runtime.callsite.LeCallSite;
import org.jruby.runtime.callsite.MinusCallSite;
import org.jruby.runtime.callsite.ModCallSite;
import org.jruby.runtime.callsite.MulCallSite;
import org.jruby.runtime.callsite.NormalCachingCallSite;
import org.jruby.runtime.callsite.GtCallSite;
@@ -122,6 +123,7 @@ public static CallSite getCallSite(String name) {
{"+", "op_plus"},
{"-", "op_minus"},
{"*", "op_mul"},
{"%", "op_mod"},
{"==", "op_equal"},
{"<", "op_lt"},
{"<=", "op_le"},
@@ -169,6 +171,7 @@ public static CallSite getFastFixnumOpsCallSite(String name) {
case "+" : return new PlusCallSite();
case "-" : return new MinusCallSite();
case "*" : return new MulCallSite();
case "%" : return new ModCallSite();
case "<" : return new LtCallSite();
case "<=" : return new LeCallSite();
case ">" : return new GtCallSite();
Original file line number Diff line number Diff line change
@@ -234,6 +234,10 @@ public static IRubyObject fixnum_op_mul(ThreadContext context, IRubyObject calle
return ((RubyFixnum)self).op_mul(context, value);
}

public static IRubyObject fixnum_op_mod(ThreadContext context, IRubyObject caller, IRubyObject self, long value) throws Throwable {
return ((RubyFixnum)self).op_mod(context, value);
}

public static IRubyObject fixnum_op_equal(ThreadContext context, IRubyObject caller, IRubyObject self, long value) throws Throwable {
return ((RubyFixnum)self).op_equal(context, value);
}