Skip to content

Commit 2e4b534

Browse files
committed
[Truffle] Add ImplicitCast from int to long.
* Allows to remove all the (int,long) and (long,int) useless overrides. This works for most cases, either because they were converting to long anyway or they were using BigInteger.valueOf(long) which has no int equivalent. * We can also remove (BigInteger,int) and (int,BigInteger) if there is a corresponding (BigInteger,long) and (long,BigInteger) since these operations will need to cast the primitive to a BigInteger. * We keep the (int,double) and (double,int) specializations since often they convert the int to a double and that is likely faster than a long to a double (plus the possible int->long). * We also have some additional specializations for binary ops on int that cannot exceed the range of long (+,-,*) and therefore need no overflow check.
1 parent 67371e1 commit 2e4b534

File tree

6 files changed

+29
-423
lines changed

6 files changed

+29
-423
lines changed

core/src/main/java/org/jruby/truffle/nodes/RubyTypes.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@
7373

7474
public class RubyTypes {
7575

76+
@ImplicitCast
77+
public long int2long(int value) {
78+
return value;
79+
}
80+
7681
@ImplicitCast
7782
public boolean unboxBoolean(RubyTrueClass value) {
7883
return true;

core/src/main/java/org/jruby/truffle/nodes/core/BignumNodes.java

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,6 @@ public AddNode(AddNode prev) {
7676
fixnumOrBignum = prev.fixnumOrBignum;
7777
}
7878

79-
@Specialization
80-
public Object add(BigInteger a, int b) {
81-
return SlowPathBigInteger.add(a, BigInteger.valueOf(b));
82-
}
83-
8479
@Specialization
8580
public Object add(BigInteger a, long b) {
8681
return SlowPathBigInteger.add(a, BigInteger.valueOf(b));
@@ -113,11 +108,6 @@ public SubNode(SubNode prev) {
113108
fixnumOrBignum = prev.fixnumOrBignum;
114109
}
115110

116-
@Specialization
117-
public Object sub(BigInteger a, int b) {
118-
return SlowPathBigInteger.subtract(a, BigInteger.valueOf(b));
119-
}
120-
121111
@Specialization
122112
public Object sub(BigInteger a, long b) {
123113
return SlowPathBigInteger.subtract(a, BigInteger.valueOf(b));
@@ -146,11 +136,6 @@ public MulNode(MulNode prev) {
146136
super(prev);
147137
}
148138

149-
@Specialization
150-
public Object mul(BigInteger a, int b) {
151-
return SlowPathBigInteger.multiply(a, BigInteger.valueOf(b));
152-
}
153-
154139
@Specialization
155140
public Object mul(BigInteger a, long b) {
156141
return SlowPathBigInteger.multiply(a, BigInteger.valueOf(b));
@@ -214,11 +199,6 @@ public DivNode(DivNode prev) {
214199
super(prev);
215200
}
216201

217-
@Specialization
218-
public Object div(BigInteger a, int b) {
219-
return SlowPathBigInteger.divide(a, BigInteger.valueOf(b));
220-
}
221-
222202
@Specialization
223203
public Object div(BigInteger a, long b) {
224204
return SlowPathBigInteger.divide(a, BigInteger.valueOf(b));
@@ -247,11 +227,6 @@ public ModNode(ModNode prev) {
247227
super(prev);
248228
}
249229

250-
@Specialization
251-
public Object mod(BigInteger a, int b) {
252-
return RubyFixnum.fixnumOrBignum(SlowPathBigInteger.mod(a, BigInteger.valueOf(b)));
253-
}
254-
255230
@Specialization
256231
public Object mod(BigInteger a, long b) {
257232
return RubyFixnum.fixnumOrBignum(SlowPathBigInteger.mod(a, BigInteger.valueOf(b)));
@@ -279,11 +254,6 @@ public DivModNode(DivModNode prev) {
279254
divModNode = new GeneralDivModNode(getContext());
280255
}
281256

282-
@Specialization
283-
public RubyArray divMod(BigInteger a, int b) {
284-
return divModNode.execute(a, b);
285-
}
286-
287257
@Specialization
288258
public RubyArray divMod(BigInteger a, long b) {
289259
return divModNode.execute(a, b);
@@ -307,11 +277,6 @@ public LessNode(LessNode prev) {
307277
super(prev);
308278
}
309279

310-
@Specialization
311-
public boolean less(BigInteger a, int b) {
312-
return SlowPathBigInteger.compareTo(a, BigInteger.valueOf(b)) < 0;
313-
}
314-
315280
@Specialization
316281
public boolean less(BigInteger a, long b) {
317282
return SlowPathBigInteger.compareTo(a, BigInteger.valueOf(b)) < 0;
@@ -339,11 +304,6 @@ public LessEqualNode(LessEqualNode prev) {
339304
super(prev);
340305
}
341306

342-
@Specialization
343-
public boolean lessEqual(BigInteger a, int b) {
344-
return SlowPathBigInteger.compareTo(a, BigInteger.valueOf(b)) <= 0;
345-
}
346-
347307
@Specialization
348308
public boolean lessEqual(BigInteger a, long b) {
349309
return SlowPathBigInteger.compareTo(a, BigInteger.valueOf(b)) <= 0;
@@ -371,11 +331,6 @@ public EqualNode(EqualNode prev) {
371331
super(prev);
372332
}
373333

374-
@Specialization
375-
public boolean equal(BigInteger a, int b) {
376-
return SlowPathBigInteger.compareTo(a, BigInteger.valueOf(b)) == 0;
377-
}
378-
379334
@Specialization
380335
public boolean equal(BigInteger a, long b) {
381336
return SlowPathBigInteger.compareTo(a, BigInteger.valueOf(b)) == 0;
@@ -403,11 +358,6 @@ public CompareNode(CompareNode prev) {
403358
super(prev);
404359
}
405360

406-
@Specialization
407-
public int compare(BigInteger a, int b) {
408-
return SlowPathBigInteger.compareTo(a, BigInteger.valueOf(b));
409-
}
410-
411361
@Specialization
412362
public int compare(BigInteger a, long b) {
413363
return SlowPathBigInteger.compareTo(a, BigInteger.valueOf(b));
@@ -435,11 +385,6 @@ public NotEqualNode(NotEqualNode prev) {
435385
super(prev);
436386
}
437387

438-
@Specialization
439-
public boolean notEqual(BigInteger a, int b) {
440-
return SlowPathBigInteger.compareTo(a, BigInteger.valueOf(b)) != 0;
441-
}
442-
443388
@Specialization
444389
public boolean notEqual(BigInteger a, long b) {
445390
return SlowPathBigInteger.compareTo(a, BigInteger.valueOf(b)) != 0;
@@ -467,11 +412,6 @@ public GreaterEqualNode(GreaterEqualNode prev) {
467412
super(prev);
468413
}
469414

470-
@Specialization
471-
public boolean greaterEqual(BigInteger a, int b) {
472-
return SlowPathBigInteger.compareTo(a, BigInteger.valueOf(b)) >= 0;
473-
}
474-
475415
@Specialization
476416
public boolean greaterEqual(BigInteger a, long b) {
477417
return SlowPathBigInteger.compareTo(a, BigInteger.valueOf(b)) >= 0;
@@ -499,11 +439,6 @@ public GreaterNode(GreaterNode prev) {
499439
super(prev);
500440
}
501441

502-
@Specialization
503-
public boolean greater(BigInteger a, int b) {
504-
return SlowPathBigInteger.compareTo(a, BigInteger.valueOf(b)) > 0;
505-
}
506-
507442
@Specialization
508443
public boolean greater(BigInteger a, long b) {
509444
return SlowPathBigInteger.compareTo(a, BigInteger.valueOf(b)) > 0;
@@ -535,11 +470,6 @@ public BitAndNode(BitAndNode prev) {
535470
fixnumOrBignumNode = prev.fixnumOrBignumNode;
536471
}
537472

538-
@Specialization
539-
public Object bitAnd(BigInteger a, int b) {
540-
return fixnumOrBignumNode.fixnumOrBignum(SlowPathBigInteger.and(a, BigInteger.valueOf(b)));
541-
}
542-
543473
@Specialization
544474
public Object bitAnd(BigInteger a, long b) {
545475
return fixnumOrBignumNode.fixnumOrBignum(SlowPathBigInteger.and(a, BigInteger.valueOf(b)));
@@ -566,11 +496,6 @@ public BitOrNode(BitOrNode prev) {
566496
fixnumOrBignumNode = prev.fixnumOrBignumNode;
567497
}
568498

569-
@Specialization
570-
public Object bitOr(BigInteger a, int b) {
571-
return fixnumOrBignumNode.fixnumOrBignum(SlowPathBigInteger.or(a, BigInteger.valueOf(b)));
572-
}
573-
574499
@Specialization
575500
public Object bitOr(BigInteger a, long b) {
576501
return fixnumOrBignumNode.fixnumOrBignum(SlowPathBigInteger.or(a, BigInteger.valueOf(b)));
@@ -597,11 +522,6 @@ public BitXOrNode(BitXOrNode prev) {
597522
fixnumOrBignumNode = prev.fixnumOrBignumNode;
598523
}
599524

600-
@Specialization
601-
public Object bitXOr(BigInteger a, int b) {
602-
return fixnumOrBignumNode.fixnumOrBignum(SlowPathBigInteger.xor(a, BigInteger.valueOf(b)));
603-
}
604-
605525
@Specialization
606526
public Object bitXOr(BigInteger a, long b) {
607527
return fixnumOrBignumNode.fixnumOrBignum(SlowPathBigInteger.xor(a, BigInteger.valueOf(b)));

0 commit comments

Comments
 (0)