Skip to content

Commit 540d96c

Browse files
committed
perf(longobject): restore JIT optimizer cases for wide ints
1 parent c1a95ef commit 540d96c

3 files changed

Lines changed: 190 additions & 6 deletions

File tree

Python/optimizer_bytecodes.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,21 @@ dummy_func(void) {
396396
REPLACE_OPCODE_IF_EVALUATES_PURE(left, right, res);
397397
}
398398

399+
op(_BINARY_OP_ADD_INT, (left, right -- res, l, r)) {
400+
if (PyJitRef_IsUnique(left)) {
401+
REPLACE_OP(this_instr, _BINARY_OP_ADD_INT_INPLACE, 0, 0);
402+
}
403+
else if (PyJitRef_IsUnique(right)) {
404+
REPLACE_OP(this_instr, _BINARY_OP_ADD_INT_INPLACE_RIGHT, 0, 0);
405+
}
406+
// Result may be a unique compact int or a cached small int
407+
// at runtime. Mark as unique; inplace ops verify at runtime.
408+
res = PyJitRef_MakeUnique(sym_new_compact_int(ctx));
409+
l = left;
410+
r = right;
411+
REPLACE_OPCODE_IF_EVALUATES_PURE(left, right, res);
412+
}
413+
399414
op(_BINARY_OP_ADD_FLOAT, (left, right -- res, l, r)) {
400415
if (PyJitRef_IsUnique(left)) {
401416
ADD_OP(_BINARY_OP_ADD_FLOAT_INPLACE, 0, 0);
@@ -432,6 +447,19 @@ dummy_func(void) {
432447
res = PyJitRef_MakeUnique(sym_new_type(ctx, &PyFloat_Type));
433448
}
434449

450+
op(_BINARY_OP_SUBTRACT_INT, (left, right -- res, l, r)) {
451+
if (PyJitRef_IsUnique(left)) {
452+
REPLACE_OP(this_instr, _BINARY_OP_SUBTRACT_INT_INPLACE, 0, 0);
453+
}
454+
else if (PyJitRef_IsUnique(right)) {
455+
REPLACE_OP(this_instr, _BINARY_OP_SUBTRACT_INT_INPLACE_RIGHT, 0, 0);
456+
}
457+
res = PyJitRef_MakeUnique(sym_new_compact_int(ctx));
458+
l = left;
459+
r = right;
460+
REPLACE_OPCODE_IF_EVALUATES_PURE(left, right, res);
461+
}
462+
435463
op(_BINARY_OP_MULTIPLY_FLOAT, (left, right -- res, l, r)) {
436464
if (PyJitRef_IsUnique(left)) {
437465
ADD_OP(_BINARY_OP_MULTIPLY_FLOAT_INPLACE, 0, 0);

Python/optimizer_cases.c.h

Lines changed: 116 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""Microbenchmark compact vs wide int add/sub with pyperf.
2+
3+
Use this with PYTHON_JIT=0 and -S if you want a stable interpreter-only run:
4+
5+
PYTHON_JIT=0 ./python.exe -S Tools/scripts/bench_wide_int_pyperf.py
6+
"""
7+
8+
from __future__ import annotations
9+
10+
import pyperf
11+
12+
13+
def bench_add_compact() -> int:
14+
a = 1
15+
b = 2
16+
return a + b
17+
18+
19+
def bench_add_wide() -> int:
20+
a = 10_000_000_000
21+
b = 1
22+
return a + b
23+
24+
25+
def bench_sub_compact() -> int:
26+
a = 1
27+
b = 2
28+
return a - b
29+
30+
31+
def bench_sub_wide() -> int:
32+
a = 10_000_000_000
33+
b = 1
34+
return a - b
35+
36+
37+
def main() -> None:
38+
runner = pyperf.Runner()
39+
runner.bench_func("add_compact", bench_add_compact)
40+
runner.bench_func("add_wide", bench_add_wide)
41+
runner.bench_func("sub_compact", bench_sub_compact)
42+
runner.bench_func("sub_wide", bench_sub_wide)
43+
44+
45+
if __name__ == "__main__":
46+
main()

0 commit comments

Comments
 (0)