bpo-40137: Add pycore_moduleobject.h internal header (GH-25507)

Add pycore_moduleobject.h internal header file with static inline
functions to access module members:

* _PyModule_GetDict()
* _PyModule_GetDef()
* _PyModule_GetState()

These functions don't check at runtime if their argument has a valid
type and can be inlined even if Python is not built with LTO.

_PyType_GetModuleByDef() uses _PyModule_GetDef().

Replace PyModule_GetState() with _PyModule_GetState() in the
extension modules, considered as performance sensitive:

* _abc
* _functools
* _operator
* _pickle
* _queue
* _random
* _sre
* _struct
* _thread
* _winapi
* array
* posix

The following extensions are now built with the Py_BUILD_CORE_MODULE
macro defined, to be able to use the internal pycore_moduleobject.h
header: _abc, array, _operator, _queue, _sre, _struct.
This commit is contained in:
Victor Stinner 2021-04-22 00:52:52 +02:00 committed by GitHub
parent a32f8fe713
commit cdad2724e6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 97 additions and 47 deletions

View file

@ -0,0 +1,42 @@
#ifndef Py_INTERNAL_MODULEOBJECT_H
#define Py_INTERNAL_MODULEOBJECT_H
#ifdef __cplusplus
extern "C" {
#endif
#ifndef Py_BUILD_CORE
# error "this header requires Py_BUILD_CORE define"
#endif
typedef struct {
PyObject_HEAD
PyObject *md_dict;
struct PyModuleDef *md_def;
void *md_state;
PyObject *md_weaklist;
// for logging purposes after md_dict is cleared
PyObject *md_name;
} PyModuleObject;
static inline PyModuleDef* _PyModule_GetDef(PyObject *mod) {
assert(PyModule_Check(mod));
return ((PyModuleObject *)mod)->md_def;
}
static inline void* _PyModule_GetState(PyObject* mod) {
assert(PyModule_Check(mod));
return ((PyModuleObject *)mod)->md_state;
}
static inline PyObject* _PyModule_GetDict(PyObject *mod) {
assert(PyModule_Check(mod));
PyObject *dict = ((PyModuleObject *)mod) -> md_dict;
// _PyModule_GetDict(mod) must not be used after calling module_clear(mod)
assert(dict != NULL);
return dict;
}
#ifdef __cplusplus
}
#endif
#endif /* !Py_INTERNAL_MODULEOBJECT_H */

View file

@ -1161,6 +1161,7 @@ PYTHON_HEADERS= \
$(srcdir)/Include/internal/pycore_interp.h \
$(srcdir)/Include/internal/pycore_list.h \
$(srcdir)/Include/internal/pycore_long.h \
$(srcdir)/Include/internal/pycore_moduleobject.h \
$(srcdir)/Include/internal/pycore_object.h \
$(srcdir)/Include/internal/pycore_pathconfig.h \
$(srcdir)/Include/internal/pycore_pyarena.h \

View file

@ -105,13 +105,13 @@ posix -DPy_BUILD_CORE_BUILTIN -I$(srcdir)/Include/internal posixmodule.c # posix
errno errnomodule.c # posix (UNIX) errno values
pwd pwdmodule.c # this is needed to find out the user's home dir
# if $HOME is not set
_sre _sre.c # Fredrik Lundh's new regular expressions
_sre -DPy_BUILD_CORE_BUILTIN _sre.c # Fredrik Lundh's new regular expressions
_codecs _codecsmodule.c # access to the builtin codecs and codec registry
_weakref _weakref.c # weak references
_functools -DPy_BUILD_CORE_BUILTIN -I$(srcdir)/Include/internal _functoolsmodule.c # Tools for working with functions and callable objects
_operator _operator.c # operator.add() and similar goodies
_operator -DPy_BUILD_CORE_BUILTIN _operator.c # operator.add() and similar goodies
_collections _collectionsmodule.c # Container types
_abc _abc.c # Abstract base classes
_abc -DPy_BUILD_CORE_BUILTIN _abc.c # Abstract base classes
itertools itertoolsmodule.c # Functions creating iterators for efficient looping
atexit atexitmodule.c # Register functions to be run at interpreter-shutdown
_signal -DPy_BUILD_CORE_BUILTIN -I$(srcdir)/Include/internal signalmodule.c
@ -166,17 +166,17 @@ _symtable symtablemodule.c
# Modules that should always be present (non UNIX dependent):
#array arraymodule.c # array objects
#array -DPy_BUILD_CORE_MODULE arraymodule.c # array objects
#cmath cmathmodule.c _math.c -DPy_BUILD_CORE_MODULE # -lm # complex math library functions
#math mathmodule.c _math.c -DPy_BUILD_CORE_MODULE # -lm # math library functions, e.g. sin()
#_contextvars _contextvarsmodule.c # Context Variables
#_struct _struct.c # binary structure packing/unpacking
#_struct -DPy_BUILD_CORE_MODULE _struct.c # binary structure packing/unpacking
#_weakref _weakref.c # basic weak reference support
#_testcapi _testcapimodule.c # Python C API test module
#_testinternalcapi _testinternalcapi.c -I$(srcdir)/Include/internal -DPy_BUILD_CORE_MODULE # Python internal C API test module
#_random _randommodule.c -DPy_BUILD_CORE_MODULE # Random number generator
#_elementtree -I$(srcdir)/Modules/expat -DHAVE_EXPAT_CONFIG_H -DUSE_PYEXPAT_CAPI _elementtree.c # elementtree accelerator
#_pickle _pickle.c # pickle accelerator
#_pickle -DPy_BUILD_CORE_MODULE _pickle.c # pickle accelerator
#_datetime _datetimemodule.c # datetime accelerator
#_zoneinfo _zoneinfo.c -DPy_BUILD_CORE_MODULE # zoneinfo accelerator
#_bisect _bisectmodule.c # Bisection algorithms

View file

@ -1,6 +1,7 @@
/* ABCMeta implementation */
#include "Python.h"
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "clinic/_abc.c.h"
/*[clinic input]
@ -27,7 +28,7 @@ typedef struct {
static inline _abcmodule_state*
get_abc_state(PyObject *module)
{
void *state = PyModule_GetState(module);
void *state = _PyModule_GetState(module);
assert(state != NULL);
return (_abcmodule_state *)state;
}

View file

@ -1,5 +1,6 @@
#include "Python.h"
#include "pycore_long.h" // _PyLong_GetZero()
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "pycore_object.h" // _PyObject_GC_TRACK
#include "pycore_pystate.h" // _PyThreadState_GET()
#include "pycore_tuple.h" // _PyTuple_ITEMS()
@ -23,7 +24,7 @@ typedef struct _functools_state {
static inline _functools_state *
get_functools_state(PyObject *module)
{
void *state = PyModule_GetState(module);
void *state = _PyModule_GetState(module);
assert(state != NULL);
return (_functools_state *)state;
}
@ -53,8 +54,7 @@ get_functools_state_by_type(PyTypeObject *type)
if (module == NULL) {
return NULL;
}
_functools_state *state = get_functools_state(module);
return state;
return get_functools_state(module);
}
static PyObject *

View file

@ -1,6 +1,5 @@
#include "Python.h"
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "clinic/_operator.c.h"
typedef struct {
@ -12,7 +11,7 @@ typedef struct {
static inline _operator_state*
get_operator_state(PyObject *module)
{
void *state = PyModule_GetState(module);
void *state = _PyModule_GetState(module);
assert(state != NULL);
return (_operator_state *)state;
}

View file

@ -9,6 +9,7 @@
#endif
#include "Python.h"
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "structmember.h" // PyMemberDef
PyDoc_STRVAR(pickle_module_doc,
@ -182,7 +183,7 @@ static struct PyModuleDef _picklemodule;
static PickleState *
_Pickle_GetState(PyObject *module)
{
return (PickleState *)PyModule_GetState(module);
return (PickleState *)_PyModule_GetState(module);
}
/* Find the module instance imported in the currently running sub-interpreter

View file

@ -1,4 +1,5 @@
#include "Python.h"
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "structmember.h" // PyMemberDef
#include <stddef.h> // offsetof()
@ -10,7 +11,7 @@ typedef struct {
static simplequeue_state *
simplequeue_get_state(PyObject *module)
{
simplequeue_state *state = PyModule_GetState(module);
simplequeue_state *state = _PyModule_GetState(module);
assert(state);
return state;
}

View file

@ -67,6 +67,7 @@
/* ---------------------------------------------------------------*/
#include "Python.h"
#include "pycore_moduleobject.h" // _PyModule_GetState()
#ifdef HAVE_PROCESS_H
# include <process.h> // getpid()
#endif
@ -86,7 +87,7 @@ typedef struct {
static inline _randomstate*
get_random_state(PyObject *module)
{
void *state = PyModule_GetState(module);
void *state = _PyModule_GetState(module);
assert(state != NULL);
return (_randomstate *)state;
}
@ -538,7 +539,7 @@ random_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
if (PyTuple_GET_SIZE(args) == 1)
arg = PyTuple_GET_ITEM(args, 0);
tmp = random_seed(self, arg);
if (tmp == NULL) {
Py_DECREF(self);

View file

@ -42,6 +42,7 @@ static const char copyright[] =
#include "Python.h"
#include "pycore_long.h" // _PyLong_GetZero()
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "structmember.h" // PyMemberDef
#include "sre.h"
@ -258,7 +259,7 @@ typedef struct {
static _sremodulestate *
get_sre_module_state(PyObject *m)
{
_sremodulestate *state = (_sremodulestate *)PyModule_GetState(m);
_sremodulestate *state = (_sremodulestate *)_PyModule_GetState(m);
assert(state);
return state;
}

View file

@ -6,6 +6,7 @@
#define PY_SSIZE_T_CLEAN
#include "Python.h"
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "structmember.h" // PyMemberDef
#include <ctype.h>
@ -24,7 +25,7 @@ typedef struct {
static inline _structmodulestate*
get_struct_state(PyObject *module)
{
void *state = PyModule_GetState(module);
void *state = _PyModule_GetState(module);
assert(state != NULL);
return (_structmodulestate *)state;
}

View file

@ -3,8 +3,9 @@
/* Interface to Sjoerd's portable C thread library */
#include "Python.h"
#include "pycore_pylifecycle.h"
#include "pycore_interp.h" // _PyInterpreterState.num_threads
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "pycore_pylifecycle.h"
#include "pycore_pystate.h" // _PyThreadState_Init()
#include <stddef.h> // offsetof()
#include "structmember.h" // PyMemberDef
@ -35,7 +36,7 @@ typedef struct {
static inline thread_module_state*
get_thread_state(PyObject *module)
{
void *state = PyModule_GetState(module);
void *state = _PyModule_GetState(module);
assert(state != NULL);
return (thread_module_state *)state;
}

View file

@ -35,7 +35,7 @@
/* See http://www.python.org/2.4/license for licensing details. */
#include "Python.h"
#include "moduleobject.h" // PyModuleDef_Slot
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "structmember.h" // PyMemberDef
@ -87,7 +87,7 @@ typedef struct {
static inline WinApiState*
winapi_get_state(PyObject *module)
{
void *state = PyModule_GetState(module);
void *state = _PyModule_GetState(module);
assert(state != NULL);
return (WinApiState *)state;
}

View file

@ -5,6 +5,7 @@
#define PY_SSIZE_T_CLEAN
#include "Python.h"
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "structmember.h" // PyMemberDef
#include <stddef.h> // offsetof()
@ -63,7 +64,7 @@ typedef struct {
static array_state *
get_array_state(PyObject *module)
{
return (array_state *)PyModule_GetState(module);
return (array_state *)_PyModule_GetState(module);
}
#define find_array_state_by_type(tp) \

View file

@ -11,6 +11,7 @@
#include "Python.h"
#include "pycore_fileutils.h"
#include "pycore_moduleobject.h" // _PyModule_GetState()
#ifdef MS_WINDOWS
/* include <windows.h> early to avoid conflict with pycore_condvar.h:
@ -994,7 +995,7 @@ typedef struct {
static inline _posixstate*
get_posix_state(PyObject *module)
{
void *state = PyModule_GetState(module);
void *state = _PyModule_GetState(module);
assert(state != NULL);
return (_posixstate *)state;
}

View file

@ -2,6 +2,7 @@
#include "Python.h"
#include "pycore_ceval.h" // _PyEval_BuiltinsFromGlobals()
#include "pycore_moduleobject.h" // _PyModule_GetDict()
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
#include "frameobject.h" // PyFrameObject
@ -1176,7 +1177,7 @@ _PyEval_BuiltinsFromGlobals(PyThreadState *tstate, PyObject *globals)
PyObject *builtins = _PyDict_GetItemIdWithError(globals, &PyId___builtins__);
if (builtins) {
if (PyModule_Check(builtins)) {
builtins = PyModule_GetDict(builtins);
builtins = _PyModule_GetDict(builtins);
assert(builtins != NULL);
}
return builtins;

View file

@ -4,6 +4,7 @@
#include "Python.h"
#include "pycore_interp.h" // PyInterpreterState.importlib
#include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "pycore_moduleobject.h" // _PyModule_GetDef()
#include "structmember.h" // PyMemberDef
static Py_ssize_t max_module_number;
@ -12,15 +13,6 @@ _Py_IDENTIFIER(__doc__);
_Py_IDENTIFIER(__name__);
_Py_IDENTIFIER(__spec__);
typedef struct {
PyObject_HEAD
PyObject *md_dict;
struct PyModuleDef *md_def;
void *md_state;
PyObject *md_weaklist;
PyObject *md_name; /* for logging purposes after md_dict is cleared */
} PyModuleObject;
static PyMemberDef module_members[] = {
{"__dict__", T_OBJECT, offsetof(PyModuleObject, md_dict), READONLY},
{0}
@ -469,14 +461,11 @@ PyModule_SetDocString(PyObject *m, const char *doc)
PyObject *
PyModule_GetDict(PyObject *m)
{
PyObject *d;
if (!PyModule_Check(m)) {
PyErr_BadInternalCall();
return NULL;
}
d = ((PyModuleObject *)m) -> md_dict;
assert(d != NULL);
return d;
return _PyModule_GetDict(m);
}
PyObject*
@ -556,7 +545,7 @@ PyModule_GetDef(PyObject* m)
PyErr_BadArgument();
return NULL;
}
return ((PyModuleObject *)m)->md_def;
return _PyModule_GetDef(m);
}
void*
@ -566,7 +555,7 @@ PyModule_GetState(PyObject* m)
PyErr_BadArgument();
return NULL;
}
return ((PyModuleObject *)m)->md_state;
return _PyModule_GetState(m);
}
void

View file

@ -4,6 +4,7 @@
#include "pycore_call.h"
#include "pycore_compile.h" // _Py_Mangle()
#include "pycore_initconfig.h"
#include "pycore_moduleobject.h" // _PyModule_GetDef()
#include "pycore_object.h"
#include "pycore_pyerrors.h"
#include "pycore_pystate.h" // _PyThreadState_GET()
@ -3582,7 +3583,7 @@ PyType_GetModuleState(PyTypeObject *type)
if (m == NULL) {
return NULL;
}
return PyModule_GetState(m);
return _PyModule_GetState(m);
}
@ -3617,7 +3618,7 @@ _PyType_GetModuleByDef(PyTypeObject *type, struct PyModuleDef *def)
PyHeapTypeObject *ht = (PyHeapTypeObject*)super;
PyObject *module = ht->ht_module;
if (module && PyModule_GetDef(module) == def) {
if (module && _PyModule_GetDef(module) == def) {
return module;
}
i++;

View file

@ -199,6 +199,7 @@
<ClInclude Include="..\Include\internal\pycore_interp.h" />
<ClInclude Include="..\Include\internal\pycore_list.h" />
<ClInclude Include="..\Include\internal\pycore_long.h" />
<ClInclude Include="..\Include\internal\pycore_moduleobject.h" />
<ClInclude Include="..\Include\internal\pycore_object.h" />
<ClInclude Include="..\Include\internal\pycore_pathconfig.h" />
<ClInclude Include="..\Include\internal\pycore_pyarena.h" />

View file

@ -558,6 +558,9 @@
<ClInclude Include="..\Include\internal\pycore_long.h">
<Filter>Include\internal</Filter>
</ClInclude>
<ClInclude Include="..\Include\internal\pycore_moduleobject.h">
<Filter>Include\internal</Filter>
</ClInclude>
<ClInclude Include="..\Include\internal\pycore_object.h">
<Filter>Include\internal</Filter>
</ClInclude>

View file

@ -869,7 +869,8 @@ def detect_simple_extensions(self):
#
# array objects
self.add(Extension('array', ['arraymodule.c']))
self.add(Extension('array', ['arraymodule.c'],
extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
# Context Variables
self.add(Extension('_contextvars', ['_contextvarsmodule.c']))
@ -934,9 +935,11 @@ def detect_simple_extensions(self):
self.add(Extension("_asyncio", ["_asynciomodule.c"],
extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
# _abc speedups
self.add(Extension("_abc", ["_abc.c"]))
self.add(Extension("_abc", ["_abc.c"],
extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
# _queue module
self.add(Extension("_queue", ["_queuemodule.c"]))
self.add(Extension("_queue", ["_queuemodule.c"],
extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
# _statistics module
self.add(Extension("_statistics", ["_statisticsmodule.c"]))
@ -2696,7 +2699,8 @@ class DummyProcess:
'install_lib': PyBuildInstallLib},
# The struct module is defined here, because build_ext won't be
# called unless there's at least one extension module defined.
ext_modules=[Extension('_struct', ['_struct.c'])],
ext_modules=[Extension('_struct', ['_struct.c'],
extra_compile_args=['-DPy_BUILD_CORE_MODULE'])],
# If you change the scripts installed here, you also need to
# check the PyBuildScripts command above, and change the links