Skip to content

Commit 285e47c

Browse files
committed
optimize _UNARY_INVERT
1 parent a380d57 commit 285e47c

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

Python/optimizer_bytecodes.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,26 @@ dummy_func(void) {
467467
res = sym_new_truthiness(ctx, value, false);
468468
}
469469

470+
op(_UNARY_INVERT, (value -- res)) {
471+
if (sym_is_const(ctx, value)) {
472+
PyObject *temp = PyNumber_Invert(sym_get_const(ctx, value));
473+
if (temp == NULL) {
474+
goto error;
475+
}
476+
if (_Py_IsImmortal(temp)) {
477+
REPLACE_OP(this_instr, _POP_TOP_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)temp);
478+
}
479+
res = sym_new_const(ctx, temp);
480+
Py_DECREF(temp);
481+
} else {
482+
if (sym_matches_type(value, &PyLong_Type) || sym_matches_type(value, &PyBool_Type)) {
483+
res = sym_new_type(ctx, &PyLong_Type);
484+
} else {
485+
res = sym_new_not_null(ctx);
486+
}
487+
}
488+
}
489+
470490
op(_COMPARE_OP, (left, right -- res)) {
471491
if (oparg & 16) {
472492
res = sym_new_type(ctx, &PyBool_Type);

Python/optimizer_cases.c.h

Lines changed: 20 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)