GH-115685: Optimize TO_BOOL and variants based on truthiness of input. (GH-116311)

This commit is contained in:
Mark Shannon 2024-03-05 11:23:46 +00:00 committed by GitHub
parent a29998a06b
commit cbf3d38cbe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 150 additions and 55 deletions

View file

@ -298,9 +298,31 @@ remove_globals(_PyInterpreterFrame *frame, _PyUOpInstruction *buffer,
#define sym_set_type _Py_uop_sym_set_type
#define sym_set_const _Py_uop_sym_set_const
#define sym_is_bottom _Py_uop_sym_is_bottom
#define sym_truthiness _Py_uop_sym_truthiness
#define frame_new _Py_uop_frame_new
#define frame_pop _Py_uop_frame_pop
static int
optimize_to_bool(
_PyUOpInstruction *this_instr,
_Py_UOpsContext *ctx,
_Py_UopsSymbol *value,
_Py_UopsSymbol **result_ptr)
{
if (sym_matches_type(value, &PyBool_Type)) {
REPLACE_OP(this_instr, _NOP, 0, 0);
*result_ptr = value;
return 1;
}
int truthiness = sym_truthiness(value);
if (truthiness >= 0) {
PyObject *load = truthiness ? Py_True : Py_False;
REPLACE_OP(this_instr, _POP_TOP_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)load);
*result_ptr = sym_new_const(ctx, load);
return 1;
}
return 0;
}
/* 1 for success, 0 for not ready, cannot error at the moment. */
static int