Skip to content

Commit e4b3ccd

Browse files
committed
use table for common consts
1 parent ef78fd1 commit e4b3ccd

File tree

9 files changed

+22
-85
lines changed

9 files changed

+22
-85
lines changed

Include/internal/pycore_interp_structs.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
extern "C" {
55
#endif
66

7+
#include "pycore_opcode_utils.h" // NUM_COMMON_CONSTANTS
78
#include "pycore_structs.h"
89
#include "pycore_pymath.h" // _PY_SHORT_FLOAT_REPR
910
#include "pycore_llist.h"
@@ -655,8 +656,6 @@ struct _Py_unicode_state {
655656
struct callable_cache {
656657
PyObject *isinstance;
657658
PyObject *len;
658-
PyObject *all;
659-
PyObject *any;
660659
PyObject *list_append;
661660
PyObject *object__getattribute__;
662661
};
@@ -927,6 +926,7 @@ struct _is {
927926
struct ast_state ast;
928927
struct types_state types;
929928
struct callable_cache callable_cache;
929+
PyObject *common_consts[NUM_COMMON_CONSTANTS];
930930
bool jit;
931931
struct _PyExecutorObject *executor_list_head;
932932
size_t trace_run_counter;

Include/internal/pycore_opcode_metadata.h

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

Include/internal/pycore_opcode_utils.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE define"
99
#endif
1010

11-
#include "opcode_ids.h"
12-
1311
#define MAX_REAL_OPCODE 254
1412

1513
#define IS_WITHIN_OPCODE_RANGE(opcode) \

Include/internal/pycore_uop_metadata.h

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

Objects/genobject.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include "pycore_pyerrors.h" // _PyErr_ClearExcState()
1818
#include "pycore_pystate.h" // _PyThreadState_GET()
1919

20+
#include "opcode_ids.h" // RESUME, etc
21+
2022
// Forward declarations
2123
static PyObject* gen_close(PyObject *, PyObject *);
2224
static PyObject* async_gen_asend_new(PyAsyncGenObject *, PyObject *);

Python/bytecodes.c

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,25 +1405,8 @@ dummy_func(
14051405
inst(LOAD_COMMON_CONSTANT, ( -- value)) {
14061406
// Keep in sync with _common_constants in opcode.py
14071407
// If we ever have more than two constants, use a lookup table
1408-
if (oparg == CONSTANT_ASSERTIONERROR) {
1409-
value = PyStackRef_FromPyObjectImmortal(PyExc_AssertionError);
1410-
}
1411-
else if (oparg == CONSTANT_NOTIMPLEMENTEDERROR) {
1412-
value = PyStackRef_FromPyObjectImmortal(PyExc_NotImplementedError);
1413-
}
1414-
else if (oparg == CONSTANT_BUILTIN_TUPLE) {
1415-
value = PyStackRef_FromPyObjectImmortal((PyObject*)&PyTuple_Type);
1416-
}
1417-
else if (oparg == CONSTANT_BUILTIN_ALL) {
1418-
value = PyStackRef_FromPyObjectNew(tstate->interp->callable_cache.all);
1419-
}
1420-
else if (oparg == CONSTANT_BUILTIN_ANY) {
1421-
value = PyStackRef_FromPyObjectNew(tstate->interp->callable_cache.any);
1422-
}
1423-
else {
1424-
_PyErr_SetString(tstate, PyExc_ValueError, "unknown common const");
1425-
ERROR_IF(true, error);
1426-
}
1408+
assert(oparg < NUM_COMMON_CONSTANTS);
1409+
value = PyStackRef_FromPyObjectNew(tstate->interp->common_consts[oparg]);
14271410
}
14281411

14291412
inst(LOAD_BUILD_CLASS, ( -- bc)) {

Python/executor_cases.c.h

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

Python/generated_cases.c.h

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

Python/pylifecycle.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -798,13 +798,21 @@ pycore_init_builtins(PyThreadState *tstate)
798798
if (!all) {
799799
goto error;
800800
}
801-
interp->callable_cache.all = all;
802801

803802
PyObject *any = PyDict_GetItemWithError(builtins_dict, &_Py_ID(any));
804803
if (!any) {
805804
goto error;
806805
}
807-
interp->callable_cache.any = any;
806+
807+
interp->common_consts[CONSTANT_ASSERTIONERROR] = (PyObject*)&PyTuple_Type;
808+
interp->common_consts[CONSTANT_NOTIMPLEMENTEDERROR] = (PyObject*)&PyTuple_Type;
809+
interp->common_consts[CONSTANT_BUILTIN_TUPLE] = (PyObject*)&PyTuple_Type;
810+
interp->common_consts[CONSTANT_BUILTIN_ALL] = all;
811+
interp->common_consts[CONSTANT_BUILTIN_ANY] = any;
812+
813+
for (int i=0; i < NUM_COMMON_CONSTANTS; i++) {
814+
assert(interp->common_consts[i] != NULL);
815+
}
808816

809817
PyObject *list_append = _PyType_Lookup(&PyList_Type, &_Py_ID(append));
810818
if (list_append == NULL) {

0 commit comments

Comments
 (0)