Skip to content

Commit 6be4142

Browse files
committed
Put BigInteger.valueOf(val) behind TruffleBoundary
and avoid allocations of PInt when only a BigInteger is needed
1 parent 8611c08 commit 6be4142

File tree

3 files changed

+46
-41
lines changed

3 files changed

+46
-41
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ints/IntBuiltins.java

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,17 @@ long addLong(long left, long right) {
164164

165165
@Specialization
166166
PInt addPInt(long left, long right) {
167-
return factory().createInt(op(BigInteger.valueOf(left), BigInteger.valueOf(right)));
167+
return factory().createInt(op(PInt.longToBigInteger(left), PInt.longToBigInteger(right)));
168168
}
169169

170170
@Specialization
171171
PInt add(PInt left, long right) {
172-
return add(left, factory().createInt(right));
172+
return factory().createInt(op(left.getValue(), PInt.longToBigInteger(right)));
173173
}
174174

175175
@Specialization
176176
PInt add(long left, PInt right) {
177-
return add(factory().createInt(left), right);
177+
return factory().createInt(op(PInt.longToBigInteger(left), right.getValue()));
178178
}
179179

180180
@Specialization
@@ -221,17 +221,17 @@ long doLL(long x, long y) throws ArithmeticException {
221221

222222
@Specialization
223223
PInt doLLOvf(long x, long y) {
224-
return factory().createInt(op(BigInteger.valueOf(x), BigInteger.valueOf(y)));
224+
return factory().createInt(op(PInt.longToBigInteger(x), PInt.longToBigInteger(y)));
225225
}
226226

227227
@Specialization
228228
PInt doPIntLong(PInt left, long right) {
229-
return doPIntPInt(left, factory().createInt(right));
229+
return factory().createInt(op(left.getValue(), PInt.longToBigInteger(right)));
230230
}
231231

232232
@Specialization
233233
PInt doLongPInt(long left, PInt right) {
234-
return doPIntPInt(factory().createInt(left), right);
234+
return factory().createInt(op(PInt.longToBigInteger(left), right.getValue()));
235235
}
236236

237237
@Specialization
@@ -273,17 +273,17 @@ long doLL(long y, long x) throws ArithmeticException {
273273

274274
@Specialization
275275
PInt doLLOvf(long y, long x) {
276-
return factory().createInt(op(BigInteger.valueOf(x), BigInteger.valueOf(y)));
276+
return factory().createInt(op(PInt.longToBigInteger(x), PInt.longToBigInteger(y)));
277277
}
278278

279279
@Specialization
280280
PInt doPIntLong(PInt right, long left) {
281-
return doPIntPInt(factory().createInt(left), right);
281+
return factory().createInt(op(PInt.longToBigInteger(left), right.getValue()));
282282
}
283283

284284
@Specialization
285285
PInt doLongPInt(long right, PInt left) {
286-
return doPIntPInt(factory().createInt(right), left);
286+
return factory().createInt(op(PInt.longToBigInteger(right), left.getValue()));
287287
}
288288

289289
@Specialization
@@ -327,15 +327,15 @@ public abstract static class TrueDivNode extends PythonBinaryBuiltinNode {
327327

328328
@Specialization
329329
double doPI(long left, PInt right) {
330-
return doPP(factory().createInt(left), right);
330+
return op(PInt.longToBigInteger(left), right.getValue());
331331
}
332332

333333
@Specialization
334334
double doPL(PInt left, long right) {
335335
if (right == 0) {
336336
throw raise(PythonErrorType.ZeroDivisionError, "division by zero");
337337
}
338-
return doPP(left, factory().createInt(right));
338+
return op(left.getValue(), PInt.longToBigInteger(right));
339339
}
340340

341341
@Specialization
@@ -391,7 +391,7 @@ public abstract static class RTrueDivNode extends PythonBinaryBuiltinNode {
391391

392392
@Specialization
393393
double doPL(PInt right, long left) {
394-
return doPP(right, factory().createInt(left));
394+
return op(right.getValue(), PInt.longToBigInteger(left));
395395
}
396396

397397
@Specialization
@@ -477,13 +477,13 @@ long doLPiOvf(long left, PInt right) {
477477
@Specialization
478478
PInt doPiL(PInt left, int right) {
479479
raiseDivisionByZero(right == 0);
480-
return factory().createInt(op(left.getValue(), BigInteger.valueOf(right)));
480+
return factory().createInt(op(left.getValue(), PInt.longToBigInteger(right)));
481481
}
482482

483483
@Specialization
484484
PInt doPiL(PInt left, long right) {
485485
raiseDivisionByZero(right == 0);
486-
return factory().createInt(op(left.getValue(), BigInteger.valueOf(right)));
486+
return factory().createInt(op(left.getValue(), PInt.longToBigInteger(right)));
487487
}
488488

489489
@Specialization
@@ -530,13 +530,13 @@ long doLL(long right, long left) {
530530
@Specialization
531531
PInt doPiL(PInt right, long left) {
532532
raiseDivisionByZero(right.isZero());
533-
return factory().createInt(op(BigInteger.valueOf(left), right.getValue()));
533+
return factory().createInt(op(PInt.longToBigInteger(left), right.getValue()));
534534
}
535535

536536
@Specialization
537537
PInt doLPi(long right, PInt left) {
538538
raiseDivisionByZero(right == 0);
539-
return factory().createInt(op(left.getValue(), BigInteger.valueOf(right)));
539+
return factory().createInt(op(left.getValue(), PInt.longToBigInteger(right)));
540540
}
541541

542542
@Specialization
@@ -576,13 +576,13 @@ long doLL(long left, long right) {
576576
@Specialization
577577
PInt doLPi(long left, PInt right) {
578578
raiseDivisionByZero(right.isZero());
579-
return factory().createInt(op(BigInteger.valueOf(left), right.getValue()));
579+
return factory().createInt(op(PInt.longToBigInteger(left), right.getValue()));
580580
}
581581

582582
@Specialization(guards = "right >= 0")
583583
PInt doPiL(PInt left, long right) {
584584
raiseDivisionByZero(right == 0);
585-
return factory().createInt(op(left.getValue(), BigInteger.valueOf(right)));
585+
return factory().createInt(op(left.getValue(), PInt.longToBigInteger(right)));
586586
}
587587

588588
@Specialization(guards = "right.isZeroOrPositive()")
@@ -593,7 +593,7 @@ PInt doPiPi(PInt left, PInt right) {
593593

594594
@Specialization(guards = "right < 0")
595595
PInt doPiLNeg(PInt left, long right) {
596-
return factory().createInt(opNeg(left.getValue(), BigInteger.valueOf(right)));
596+
return factory().createInt(opNeg(left.getValue(), PInt.longToBigInteger(right)));
597597
}
598598

599599
@Specialization(guards = "!right.isZeroOrPositive()")
@@ -649,7 +649,7 @@ PInt doLLOvf(long x, long y) {
649649
if (((ax | ay) >>> 31 != 0)) {
650650
int leadingZeros = Long.numberOfLeadingZeros(ax) + Long.numberOfLeadingZeros(ay);
651651
if (leadingZeros < 66) {
652-
return factory().createInt(mul(BigInteger.valueOf(x), BigInteger.valueOf(y)));
652+
return factory().createInt(mul(PInt.longToBigInteger(x), PInt.longToBigInteger(y)));
653653
}
654654
}
655655
return factory().createInt(r);
@@ -668,7 +668,7 @@ PInt doPIntLongOne(PInt left, @SuppressWarnings("unused") long right) {
668668

669669
@Specialization(guards = {"right != 0", "right != 1"})
670670
PInt doPIntLong(PInt left, long right) {
671-
return factory().createInt(mul(left.getValue(), BigInteger.valueOf(right)));
671+
return factory().createInt(mul(left.getValue(), PInt.longToBigInteger(right)));
672672
}
673673

674674
@Specialization
@@ -733,7 +733,7 @@ int doIntegerFast(int left, int right, @SuppressWarnings("unused") PNone none) {
733733

734734
@Specialization(guards = "right >= 0")
735735
PInt doInteger(int left, int right, @SuppressWarnings("unused") PNone none) {
736-
return factory().createInt(op(BigInteger.valueOf(left), right));
736+
return factory().createInt(op(PInt.longToBigInteger(left), right));
737737
}
738738

739739
@Specialization(guards = "right >= 0", rewriteOn = ArithmeticException.class)
@@ -773,7 +773,7 @@ long doLongFast(long left, long right, @SuppressWarnings("unused") PNone none) {
773773

774774
@Specialization(guards = "right >= 0")
775775
PInt doLong(long left, long right, @SuppressWarnings("unused") PNone none) {
776-
return factory().createInt(op(BigInteger.valueOf(left), right));
776+
return factory().createInt(op(PInt.longToBigInteger(left), right));
777777
}
778778

779779
@Specialization
@@ -839,7 +839,7 @@ private BigInteger op(BigInteger a, long b) {
839839
} else if (value == 1) {
840840
return BigInteger.ONE;
841841
} else if (value == -1) {
842-
return (b & 1) != 0 ? BigInteger.valueOf(-1) : BigInteger.ONE;
842+
return (b & 1) != 0 ? PInt.longToBigInteger(-1) : BigInteger.ONE;
843843
}
844844
} catch (ArithmeticException e) {
845845
// fall through to normal computation
@@ -888,9 +888,9 @@ long pos(long arg) {
888888
PInt posOvf(long arg) throws IllegalArgumentException {
889889
long result = Math.abs(arg);
890890
if (result < 0) {
891-
return factory().createInt(op(BigInteger.valueOf(arg)));
891+
return factory().createInt(op(PInt.longToBigInteger(arg)));
892892
} else {
893-
return factory().createInt(BigInteger.valueOf(arg));
893+
return factory().createInt(PInt.longToBigInteger(arg));
894894
}
895895
}
896896

@@ -986,7 +986,7 @@ long neg(long arg) {
986986

987987
@Specialization
988988
PInt negOvf(long arg) {
989-
BigInteger value = arg == Long.MIN_VALUE ? negate(BigInteger.valueOf(arg)) : BigInteger.valueOf(-arg);
989+
BigInteger value = arg == Long.MIN_VALUE ? negate(PInt.longToBigInteger(arg)) : PInt.longToBigInteger(-arg);
990990
return factory().createInt(value);
991991
}
992992

@@ -1084,7 +1084,7 @@ Object doIIOvf(int left, int right) {
10841084
try {
10851085
return leftShiftExact(left, right);
10861086
} catch (ArithmeticException e) {
1087-
return doGuardedBiI(BigInteger.valueOf(left), right);
1087+
return doGuardedBiI(PInt.longToBigInteger(left), right);
10881088
}
10891089
}
10901090

@@ -1102,7 +1102,7 @@ Object doLLOvf(long left, long right) {
11021102
} catch (ArithmeticException e) {
11031103
int rightI = (int) right;
11041104
if (rightI == right) {
1105-
return factory().createInt(op(BigInteger.valueOf(left), rightI));
1105+
return factory().createInt(op(PInt.longToBigInteger(left), rightI));
11061106
} else {
11071107
throw raise(PythonErrorType.OverflowError);
11081108
}
@@ -1113,7 +1113,7 @@ Object doLLOvf(long left, long right) {
11131113
PInt doLPi(long left, PInt right) {
11141114
raiseNegativeShiftCount(!right.isZeroOrPositive());
11151115
try {
1116-
return factory().createInt(op(BigInteger.valueOf(left), right.intValue()));
1116+
return factory().createInt(op(PInt.longToBigInteger(left), right.intValue()));
11171117
} catch (ArithmeticException e) {
11181118
throw raise(PythonErrorType.OverflowError);
11191119
}
@@ -1191,13 +1191,13 @@ long doLL(long left, long right) {
11911191
@Specialization
11921192
PInt doIPi(int left, PInt right) {
11931193
raiseNegativeShiftCount(!right.isZeroOrPositive());
1194-
return factory().createInt(op(BigInteger.valueOf(left), right.intValue()));
1194+
return factory().createInt(op(PInt.longToBigInteger(left), right.intValue()));
11951195
}
11961196

11971197
@Specialization
11981198
PInt doLPi(long left, PInt right) {
11991199
raiseNegativeShiftCount(!right.isZeroOrPositive());
1200-
return factory().createInt(op(BigInteger.valueOf(left), right.intValue()));
1200+
return factory().createInt(op(PInt.longToBigInteger(left), right.intValue()));
12011201
}
12021202

12031203
@Specialization
@@ -1266,12 +1266,12 @@ long doInteger(long left, long right) {
12661266

12671267
@Specialization
12681268
PInt doPInt(long left, PInt right) {
1269-
return factory().createInt(op(BigInteger.valueOf(left), right.getValue()));
1269+
return factory().createInt(op(PInt.longToBigInteger(left), right.getValue()));
12701270
}
12711271

12721272
@Specialization
12731273
PInt doPInt(PInt left, long right) {
1274-
return factory().createInt(op(left.getValue(), BigInteger.valueOf(right)));
1274+
return factory().createInt(op(left.getValue(), PInt.longToBigInteger(right)));
12751275
}
12761276

12771277
@Specialization
@@ -1449,10 +1449,10 @@ boolean eqVoidPtrPInt(PythonNativeVoidPtr a, PInt b,
14491449
long ptrVal = lib.asPointer(a.object);
14501450
if (ptrVal < 0) {
14511451
// pointers are considered unsigned
1452-
BigInteger bi = BigInteger.valueOf(ptrVal).add(BigInteger.ONE.shiftLeft(64));
1452+
BigInteger bi = PInt.longToBigInteger(ptrVal).add(BigInteger.ONE.shiftLeft(64));
14531453
return bi.equals(b.getValue());
14541454
}
1455-
return BigInteger.valueOf(ptrVal).equals(b.getValue());
1455+
return PInt.longToBigInteger(ptrVal).equals(b.getValue());
14561456
} catch (UnsupportedMessageException e) {
14571457
// fall through
14581458
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ints/PInt.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public int compareTo(long i) {
223223

224224
@TruffleBoundary
225225
private static final int compareTo(BigInteger left, long right) {
226-
return left.compareTo(BigInteger.valueOf(right));
226+
return left.compareTo(longToBigInteger(right));
227227
}
228228

229229
@Override
@@ -236,6 +236,11 @@ private static final String toString(BigInteger value) {
236236
return value.toString();
237237
}
238238

239+
@TruffleBoundary
240+
public static final BigInteger longToBigInteger(long value) {
241+
return BigInteger.valueOf(value);
242+
}
243+
239244
public double doubleValue() {
240245
return doubleValue(value);
241246
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/object/PythonObjectFactory.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,23 +220,23 @@ public SuperObject createSuperObject(LazyPythonClass self) {
220220
* Primitive types
221221
*/
222222
public PInt createInt(int value) {
223-
return trace(new PInt(PythonBuiltinClassType.PInt, BigInteger.valueOf(value)));
223+
return trace(new PInt(PythonBuiltinClassType.PInt, PInt.longToBigInteger(value)));
224224
}
225225

226226
public PInt createInt(long value) {
227-
return trace(new PInt(PythonBuiltinClassType.PInt, BigInteger.valueOf(value)));
227+
return trace(new PInt(PythonBuiltinClassType.PInt, PInt.longToBigInteger(value)));
228228
}
229229

230230
public PInt createInt(BigInteger value) {
231231
return trace(new PInt(PythonBuiltinClassType.PInt, value));
232232
}
233233

234234
public Object createInt(LazyPythonClass cls, int value) {
235-
return trace(new PInt(cls, BigInteger.valueOf(value)));
235+
return trace(new PInt(cls, PInt.longToBigInteger(value)));
236236
}
237237

238238
public Object createInt(LazyPythonClass cls, long value) {
239-
return trace(new PInt(cls, BigInteger.valueOf(value)));
239+
return trace(new PInt(cls, PInt.longToBigInteger(value)));
240240
}
241241

242242
public PInt createInt(LazyPythonClass cls, BigInteger value) {

0 commit comments

Comments
 (0)