Skip to content

Commit 6614ae6

Browse files
Use POP_TWO_LOAD_CONST_INLINE_BORROW
1 parent c45e661 commit 6614ae6

File tree

2 files changed

+118
-11
lines changed

2 files changed

+118
-11
lines changed

Python/optimizer_bytecodes.c

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,20 @@ dummy_func(void) {
207207
else {
208208
res = sym_new_type(ctx, &PyFloat_Type);
209209
}
210+
211+
if (sym_is_const(ctx, left) && sym_is_const(ctx, right)) {
212+
PyObject *temp = PyObject_RichCompare(sym_get_const(ctx, left),
213+
sym_get_const(ctx, right),
214+
oparg >> 5);
215+
if (temp == NULL) {
216+
goto error;
217+
}
218+
assert(PyBool_Check(temp));
219+
assert(_Py_IsImmortal(temp));
220+
REPLACE_OP(this_instr, _POP_TWO_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)temp);
221+
res = sym_new_const(ctx, temp);
222+
Py_DECREF(temp);
223+
}
210224
}
211225

212226
op(_BINARY_OP_ADD_INT, (left, right -- res)) {
@@ -486,11 +500,43 @@ dummy_func(void) {
486500
}
487501

488502
op(_COMPARE_OP_FLOAT, (left, right -- res)) {
489-
res = sym_new_type(ctx, &PyBool_Type);
503+
if (sym_is_const(ctx, left) && sym_is_const(ctx, right)) {
504+
assert(PyLong_CheckExact(sym_get_const(ctx, left)));
505+
assert(PyLong_CheckExact(sym_get_const(ctx, right)));
506+
PyObject *tmp = PyObject_RichCompare(sym_get_const(ctx, left),
507+
sym_get_const(ctx, right),
508+
oparg >> 5);
509+
if (tmp == NULL) {
510+
goto error;
511+
}
512+
assert(PyBool_Check(tmp));
513+
REPLACE_OP(this_instr, _POP_TWO_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)tmp);
514+
res = sym_new_const(ctx, tmp);
515+
Py_DECREF(tmp);
516+
}
517+
else {
518+
res = sym_new_type(ctx, &PyBool_Type);
519+
}
490520
}
491521

492522
op(_COMPARE_OP_STR, (left, right -- res)) {
493-
res = sym_new_type(ctx, &PyBool_Type);
523+
if (sym_is_const(ctx, left) && sym_is_const(ctx, right)) {
524+
assert(PyLong_CheckExact(sym_get_const(ctx, left)));
525+
assert(PyLong_CheckExact(sym_get_const(ctx, right)));
526+
PyObject *tmp = PyObject_RichCompare(sym_get_const(ctx, left),
527+
sym_get_const(ctx, right),
528+
oparg >> 5);
529+
if (tmp == NULL) {
530+
goto error;
531+
}
532+
assert(PyBool_Check(tmp));
533+
REPLACE_OP(this_instr, _POP_TWO_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)tmp);
534+
res = sym_new_const(ctx, tmp);
535+
Py_DECREF(tmp);
536+
}
537+
else {
538+
res = sym_new_type(ctx, &PyBool_Type);
539+
}
494540
}
495541

496542
op(_IS_OP, (left, right -- b)) {
@@ -1117,4 +1163,4 @@ dummy_func(void) {
11171163

11181164
// END BYTECODES //
11191165

1120-
}
1166+
}

Python/optimizer_cases.c.h

Lines changed: 69 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)