gh-111789: Simplify bytecodes.c by using PyDict_GetItemRef() (GH-111978)

This commit is contained in:
Serhiy Storchaka 2023-11-14 15:38:49 +02:00 committed by GitHub
parent 4f04172c92
commit b11c443bb2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 140 deletions

View file

@ -643,16 +643,12 @@ dummy_func(
inst(BINARY_SUBSCR_DICT, (unused/1, dict, sub -- res)) { inst(BINARY_SUBSCR_DICT, (unused/1, dict, sub -- res)) {
DEOPT_IF(!PyDict_CheckExact(dict)); DEOPT_IF(!PyDict_CheckExact(dict));
STAT_INC(BINARY_SUBSCR, hit); STAT_INC(BINARY_SUBSCR, hit);
res = PyDict_GetItemWithError(dict, sub); int rc = PyDict_GetItemRef(dict, sub, &res);
if (res == NULL) { if (rc == 0) {
if (!_PyErr_Occurred(tstate)) {
_PyErr_SetKeyError(sub); _PyErr_SetKeyError(sub);
} }
DECREF_INPUTS(); DECREF_INPUTS();
ERROR_IF(true, error); ERROR_IF(rc <= 0, error); // not found or error
}
Py_INCREF(res); // Do this before DECREF'ing dict, sub
DECREF_INPUTS();
} }
inst(BINARY_SUBSCR_GETITEM, (unused/1, container, sub -- unused)) { inst(BINARY_SUBSCR_GETITEM, (unused/1, container, sub -- unused)) {
@ -1349,14 +1345,10 @@ dummy_func(
GOTO_ERROR(error); GOTO_ERROR(error);
} }
if (v == NULL) { if (v == NULL) {
v = PyDict_GetItemWithError(GLOBALS(), name); if (PyDict_GetItemRef(GLOBALS(), name, &v) < 0) {
if (v != NULL) {
Py_INCREF(v);
}
else if (_PyErr_Occurred(tstate)) {
GOTO_ERROR(error); GOTO_ERROR(error);
} }
else { if (v == NULL) {
if (PyMapping_GetOptionalItem(BUILTINS(), name, &v) < 0) { if (PyMapping_GetOptionalItem(BUILTINS(), name, &v) < 0) {
GOTO_ERROR(error); GOTO_ERROR(error);
} }
@ -1383,14 +1375,10 @@ dummy_func(
GOTO_ERROR(error); GOTO_ERROR(error);
} }
if (v == NULL) { if (v == NULL) {
v = PyDict_GetItemWithError(GLOBALS(), name); if (PyDict_GetItemRef(GLOBALS(), name, &v) < 0) {
if (v != NULL) {
Py_INCREF(v);
}
else if (_PyErr_Occurred(tstate)) {
GOTO_ERROR(error); GOTO_ERROR(error);
} }
else { if (v == NULL) {
if (PyMapping_GetOptionalItem(BUILTINS(), name, &v) < 0) { if (PyMapping_GetOptionalItem(BUILTINS(), name, &v) < 0) {
GOTO_ERROR(error); GOTO_ERROR(error);
} }
@ -1663,22 +1651,6 @@ dummy_func(
ERROR_IF(true, error); ERROR_IF(true, error);
} }
/* check if __annotations__ in locals()... */ /* check if __annotations__ in locals()... */
if (PyDict_CheckExact(LOCALS())) {
ann_dict = _PyDict_GetItemWithError(LOCALS(),
&_Py_ID(__annotations__));
if (ann_dict == NULL) {
ERROR_IF(_PyErr_Occurred(tstate), error);
/* ...if not, create a new one */
ann_dict = PyDict_New();
ERROR_IF(ann_dict == NULL, error);
err = PyDict_SetItem(LOCALS(), &_Py_ID(__annotations__),
ann_dict);
Py_DECREF(ann_dict);
ERROR_IF(err, error);
}
}
else {
/* do the same if locals() is not a dict */
ERROR_IF(PyMapping_GetOptionalItem(LOCALS(), &_Py_ID(__annotations__), &ann_dict) < 0, error); ERROR_IF(PyMapping_GetOptionalItem(LOCALS(), &_Py_ID(__annotations__), &ann_dict) < 0, error);
if (ann_dict == NULL) { if (ann_dict == NULL) {
ann_dict = PyDict_New(); ann_dict = PyDict_New();
@ -1692,7 +1664,6 @@ dummy_func(
Py_DECREF(ann_dict); Py_DECREF(ann_dict);
} }
} }
}
inst(BUILD_CONST_KEY_MAP, (values[oparg], keys -- map)) { inst(BUILD_CONST_KEY_MAP, (values[oparg], keys -- map)) {
if (!PyTuple_CheckExact(keys) || if (!PyTuple_CheckExact(keys) ||

View file

@ -512,18 +512,13 @@
dict = stack_pointer[-2]; dict = stack_pointer[-2];
DEOPT_IF(!PyDict_CheckExact(dict), BINARY_SUBSCR); DEOPT_IF(!PyDict_CheckExact(dict), BINARY_SUBSCR);
STAT_INC(BINARY_SUBSCR, hit); STAT_INC(BINARY_SUBSCR, hit);
res = PyDict_GetItemWithError(dict, sub); int rc = PyDict_GetItemRef(dict, sub, &res);
if (res == NULL) { if (rc == 0) {
if (!_PyErr_Occurred(tstate)) {
_PyErr_SetKeyError(sub); _PyErr_SetKeyError(sub);
} }
Py_DECREF(dict); Py_DECREF(dict);
Py_DECREF(sub); Py_DECREF(sub);
if (true) goto pop_2_error_tier_two; if (rc <= 0) goto pop_2_error_tier_two;
}
Py_INCREF(res); // Do this before DECREF'ing dict, sub
Py_DECREF(dict);
Py_DECREF(sub);
STACK_SHRINK(1); STACK_SHRINK(1);
stack_pointer[-1] = res; stack_pointer[-1] = res;
break; break;
@ -1022,14 +1017,10 @@
GOTO_ERROR(error); GOTO_ERROR(error);
} }
if (v == NULL) { if (v == NULL) {
v = PyDict_GetItemWithError(GLOBALS(), name); if (PyDict_GetItemRef(GLOBALS(), name, &v) < 0) {
if (v != NULL) {
Py_INCREF(v);
}
else if (_PyErr_Occurred(tstate)) {
GOTO_ERROR(error); GOTO_ERROR(error);
} }
else { if (v == NULL) {
if (PyMapping_GetOptionalItem(BUILTINS(), name, &v) < 0) { if (PyMapping_GetOptionalItem(BUILTINS(), name, &v) < 0) {
GOTO_ERROR(error); GOTO_ERROR(error);
} }
@ -1059,14 +1050,10 @@
GOTO_ERROR(error); GOTO_ERROR(error);
} }
if (v == NULL) { if (v == NULL) {
v = PyDict_GetItemWithError(GLOBALS(), name); if (PyDict_GetItemRef(GLOBALS(), name, &v) < 0) {
if (v != NULL) {
Py_INCREF(v);
}
else if (_PyErr_Occurred(tstate)) {
GOTO_ERROR(error); GOTO_ERROR(error);
} }
else { if (v == NULL) {
if (PyMapping_GetOptionalItem(BUILTINS(), name, &v) < 0) { if (PyMapping_GetOptionalItem(BUILTINS(), name, &v) < 0) {
GOTO_ERROR(error); GOTO_ERROR(error);
} }
@ -1404,22 +1391,6 @@
if (true) goto error_tier_two; if (true) goto error_tier_two;
} }
/* check if __annotations__ in locals()... */ /* check if __annotations__ in locals()... */
if (PyDict_CheckExact(LOCALS())) {
ann_dict = _PyDict_GetItemWithError(LOCALS(),
&_Py_ID(__annotations__));
if (ann_dict == NULL) {
if (_PyErr_Occurred(tstate)) goto error_tier_two;
/* ...if not, create a new one */
ann_dict = PyDict_New();
if (ann_dict == NULL) goto error_tier_two;
err = PyDict_SetItem(LOCALS(), &_Py_ID(__annotations__),
ann_dict);
Py_DECREF(ann_dict);
if (err) goto error_tier_two;
}
}
else {
/* do the same if locals() is not a dict */
if (PyMapping_GetOptionalItem(LOCALS(), &_Py_ID(__annotations__), &ann_dict) < 0) goto error_tier_two; if (PyMapping_GetOptionalItem(LOCALS(), &_Py_ID(__annotations__), &ann_dict) < 0) goto error_tier_two;
if (ann_dict == NULL) { if (ann_dict == NULL) {
ann_dict = PyDict_New(); ann_dict = PyDict_New();
@ -1432,7 +1403,6 @@
else { else {
Py_DECREF(ann_dict); Py_DECREF(ann_dict);
} }
}
break; break;
} }

View file

@ -896,18 +896,13 @@
dict = stack_pointer[-2]; dict = stack_pointer[-2];
DEOPT_IF(!PyDict_CheckExact(dict), BINARY_SUBSCR); DEOPT_IF(!PyDict_CheckExact(dict), BINARY_SUBSCR);
STAT_INC(BINARY_SUBSCR, hit); STAT_INC(BINARY_SUBSCR, hit);
res = PyDict_GetItemWithError(dict, sub); int rc = PyDict_GetItemRef(dict, sub, &res);
if (res == NULL) { if (rc == 0) {
if (!_PyErr_Occurred(tstate)) {
_PyErr_SetKeyError(sub); _PyErr_SetKeyError(sub);
} }
Py_DECREF(dict); Py_DECREF(dict);
Py_DECREF(sub); Py_DECREF(sub);
if (true) goto pop_2_error; if (rc <= 0) goto pop_2_error;
}
Py_INCREF(res); // Do this before DECREF'ing dict, sub
Py_DECREF(dict);
Py_DECREF(sub);
STACK_SHRINK(1); STACK_SHRINK(1);
stack_pointer[-1] = res; stack_pointer[-1] = res;
DISPATCH(); DISPATCH();
@ -1950,14 +1945,10 @@
GOTO_ERROR(error); GOTO_ERROR(error);
} }
if (v == NULL) { if (v == NULL) {
v = PyDict_GetItemWithError(GLOBALS(), name); if (PyDict_GetItemRef(GLOBALS(), name, &v) < 0) {
if (v != NULL) {
Py_INCREF(v);
}
else if (_PyErr_Occurred(tstate)) {
GOTO_ERROR(error); GOTO_ERROR(error);
} }
else { if (v == NULL) {
if (PyMapping_GetOptionalItem(BUILTINS(), name, &v) < 0) { if (PyMapping_GetOptionalItem(BUILTINS(), name, &v) < 0) {
GOTO_ERROR(error); GOTO_ERROR(error);
} }
@ -1990,14 +1981,10 @@
GOTO_ERROR(error); GOTO_ERROR(error);
} }
if (v == NULL) { if (v == NULL) {
v = PyDict_GetItemWithError(GLOBALS(), name); if (PyDict_GetItemRef(GLOBALS(), name, &v) < 0) {
if (v != NULL) {
Py_INCREF(v);
}
else if (_PyErr_Occurred(tstate)) {
GOTO_ERROR(error); GOTO_ERROR(error);
} }
else { if (v == NULL) {
if (PyMapping_GetOptionalItem(BUILTINS(), name, &v) < 0) { if (PyMapping_GetOptionalItem(BUILTINS(), name, &v) < 0) {
GOTO_ERROR(error); GOTO_ERROR(error);
} }
@ -2422,22 +2409,6 @@
if (true) goto error; if (true) goto error;
} }
/* check if __annotations__ in locals()... */ /* check if __annotations__ in locals()... */
if (PyDict_CheckExact(LOCALS())) {
ann_dict = _PyDict_GetItemWithError(LOCALS(),
&_Py_ID(__annotations__));
if (ann_dict == NULL) {
if (_PyErr_Occurred(tstate)) goto error;
/* ...if not, create a new one */
ann_dict = PyDict_New();
if (ann_dict == NULL) goto error;
err = PyDict_SetItem(LOCALS(), &_Py_ID(__annotations__),
ann_dict);
Py_DECREF(ann_dict);
if (err) goto error;
}
}
else {
/* do the same if locals() is not a dict */
if (PyMapping_GetOptionalItem(LOCALS(), &_Py_ID(__annotations__), &ann_dict) < 0) goto error; if (PyMapping_GetOptionalItem(LOCALS(), &_Py_ID(__annotations__), &ann_dict) < 0) goto error;
if (ann_dict == NULL) { if (ann_dict == NULL) {
ann_dict = PyDict_New(); ann_dict = PyDict_New();
@ -2450,7 +2421,6 @@
else { else {
Py_DECREF(ann_dict); Py_DECREF(ann_dict);
} }
}
DISPATCH(); DISPATCH();
} }