gh-81057: Generate a Separate Initializer For Each Part of the Global Objects Initializer (gh-99389)

Up until now we had a single generated initializer macro for all the statically declared global objects in _PyRuntimeState, including several one-offs (e.g. the empty tuple). The one-offs don't need to be generated, but were because we had one big initializer. Having separate initializers for set of generated global objects allows us to generate only the ones we need to.  This allows us to add initializers for one-off global objects without having to generate them.

https://github.com/python/cpython/issues/81057
This commit is contained in:
Eric Snow 2022-11-11 13:23:41 -07:00 committed by GitHub
parent 6abec1caff
commit fe55ff3f68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 1528 additions and 1517 deletions

View File

@ -25,6 +25,7 @@ _PyStaticObject_CheckRefcnt(PyObject *obj) {
#ifdef Py_DEBUG
static inline void
_PyStaticObjects_CheckRefcnt(void) {
/* generated (see pycore_runtime_init_generated.h) */
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_SINGLETON(small_ints)[_PY_NSMALLNEGINTS + -5]);
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_SINGLETON(small_ints)[_PY_NSMALLNEGINTS + -4]);
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_SINGLETON(small_ints)[_PY_NSMALLNEGINTS + -3]);
@ -287,7 +288,6 @@ _PyStaticObjects_CheckRefcnt(void) {
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_SINGLETON(small_ints)[_PY_NSMALLNEGINTS + 254]);
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_SINGLETON(small_ints)[_PY_NSMALLNEGINTS + 255]);
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_SINGLETON(small_ints)[_PY_NSMALLNEGINTS + 256]);
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_SINGLETON(bytes_empty));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_SINGLETON(bytes_characters)[0]);
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_SINGLETON(bytes_characters)[1]);
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_SINGLETON(bytes_characters)[2]);
@ -1469,6 +1469,8 @@ _PyStaticObjects_CheckRefcnt(void) {
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_SINGLETON(strings).latin1[253 - 128]);
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_SINGLETON(strings).latin1[254 - 128]);
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_SINGLETON(strings).latin1[255 - 128]);
/* non-generated */
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_SINGLETON(bytes_empty));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_SINGLETON(tuple_empty));
}
#endif // Py_DEBUG

View File

@ -28,7 +28,22 @@ extern "C" {
until _PyInterpreterState_Enable() is called. */ \
.next_id = -1, \
}, \
.global_objects = _Py_global_objects_INIT, \
.global_objects = { \
.singletons = { \
.small_ints = _Py_small_ints_INIT, \
.bytes_empty = _PyBytes_SIMPLE_INIT(0, 0), \
.bytes_characters = _Py_bytes_characters_INIT, \
.strings = { \
.literals = _Py_str_literals_INIT, \
.identifiers = _Py_str_identifiers_INIT, \
.ascii = _Py_str_ascii_INIT, \
.latin1 = _Py_str_latin1_INIT, \
}, \
.tuple_empty = { \
.ob_base = _PyVarObject_IMMORTAL_INIT(&PyTuple_Type, 0) \
}, \
}, \
}, \
._main_interpreter = _PyInterpreterState_INIT, \
}

View File

@ -9,9 +9,7 @@ extern "C" {
#endif
/* The following is auto-generated by Tools/build/generate_global_objects.py. */
#define _Py_global_objects_INIT { \
.singletons = { \
.small_ints = { \
#define _Py_small_ints_INIT { \
_PyLong_DIGIT_INIT(-5), \
_PyLong_DIGIT_INIT(-4), \
_PyLong_DIGIT_INIT(-3), \
@ -274,10 +272,9 @@ extern "C" {
_PyLong_DIGIT_INIT(254), \
_PyLong_DIGIT_INIT(255), \
_PyLong_DIGIT_INIT(256), \
}, \
\
.bytes_empty = _PyBytes_SIMPLE_INIT(0, 0), \
.bytes_characters = { \
}
#define _Py_bytes_characters_INIT { \
_PyBytes_CHAR_INIT(0), \
_PyBytes_CHAR_INIT(1), \
_PyBytes_CHAR_INIT(2), \
@ -534,10 +531,9 @@ extern "C" {
_PyBytes_CHAR_INIT(253), \
_PyBytes_CHAR_INIT(254), \
_PyBytes_CHAR_INIT(255), \
}, \
\
.strings = { \
.literals = { \
}
#define _Py_str_literals_INIT { \
INIT_STR(anon_dictcomp, "<dictcomp>"), \
INIT_STR(anon_genexpr, "<genexpr>"), \
INIT_STR(anon_lambda, "<lambda>"), \
@ -560,8 +556,9 @@ extern "C" {
INIT_STR(percent, "%"), \
INIT_STR(shim_name, "<shim>"), \
INIT_STR(utf_8, "utf-8"), \
}, \
.identifiers = { \
}
#define _Py_str_identifiers_INIT { \
INIT_ID(CANCELLED), \
INIT_ID(FINISHED), \
INIT_ID(False), \
@ -1209,8 +1206,9 @@ extern "C" {
INIT_ID(x), \
INIT_ID(year), \
INIT_ID(zdict), \
}, \
.ascii = { \
}
#define _Py_str_ascii_INIT { \
_PyASCIIObject_INIT("\x00"), \
_PyASCIIObject_INIT("\x01"), \
_PyASCIIObject_INIT("\x02"), \
@ -1339,8 +1337,9 @@ extern "C" {
_PyASCIIObject_INIT("\x7d"), \
_PyASCIIObject_INIT("\x7e"), \
_PyASCIIObject_INIT("\x7f"), \
}, \
.latin1 = { \
}
#define _Py_str_latin1_INIT { \
_PyUnicode_LATIN1_INIT("\x80", "\xc2\x80"), \
_PyUnicode_LATIN1_INIT("\x81", "\xc2\x81"), \
_PyUnicode_LATIN1_INIT("\x82", "\xc2\x82"), \
@ -1469,15 +1468,9 @@ extern "C" {
_PyUnicode_LATIN1_INIT("\xfd", "\xc3\xbd"), \
_PyUnicode_LATIN1_INIT("\xfe", "\xc3\xbe"), \
_PyUnicode_LATIN1_INIT("\xff", "\xc3\xbf"), \
}, \
}, \
\
.tuple_empty = { \
.ob_base = _PyVarObject_IMMORTAL_INIT(&PyTuple_Type, 0) \
}, \
}, \
}
/* End auto-generated code */
#ifdef __cplusplus
}
#endif

View File

@ -123,6 +123,12 @@ IDENTIFIERS = [
'__rdivmod__',
]
NON_GENERATED_IMMORTAL_OBJECTS = [
# The generated ones come from generate_runtime_init().
'(PyObject *)&_Py_SINGLETON(bytes_empty)',
'(PyObject *)&_Py_SINGLETON(tuple_empty)',
]
#######################################
# helpers
@ -287,38 +293,33 @@ def generate_runtime_init(identifiers, strings):
printer = Printer(outfile)
printer.write(before)
printer.write(START)
with printer.block('#define _Py_global_objects_INIT', continuation=True):
with printer.block('.singletons =', ','):
# Global int objects.
with printer.block('.small_ints =', ','):
with printer.block('#define _Py_small_ints_INIT', continuation=True):
for i in range(-nsmallnegints, nsmallposints):
printer.write(f'_PyLong_DIGIT_INIT({i}),')
immortal_objects.append(f'(PyObject *)&_Py_SINGLETON(small_ints)[_PY_NSMALLNEGINTS + {i}]')
printer.write('')
# Global bytes objects.
printer.write('.bytes_empty = _PyBytes_SIMPLE_INIT(0, 0),')
immortal_objects.append(f'(PyObject *)&_Py_SINGLETON(bytes_empty)')
with printer.block('.bytes_characters =', ','):
with printer.block('#define _Py_bytes_characters_INIT', continuation=True):
for i in range(256):
printer.write(f'_PyBytes_CHAR_INIT({i}),')
immortal_objects.append(f'(PyObject *)&_Py_SINGLETON(bytes_characters)[{i}]')
printer.write('')
# Global strings.
with printer.block('.strings =', ','):
with printer.block('.literals =', ','):
with printer.block('#define _Py_str_literals_INIT', continuation=True):
for literal, name in sorted(strings.items(), key=lambda x: x[1]):
printer.write(f'INIT_STR({name}, "{literal}"),')
immortal_objects.append(f'(PyObject *)&_Py_STR({name})')
with printer.block('.identifiers =', ','):
printer.write('')
with printer.block('#define _Py_str_identifiers_INIT', continuation=True):
for name in sorted(identifiers):
assert name.isidentifier(), name
printer.write(f'INIT_ID({name}),')
immortal_objects.append(f'(PyObject *)&_Py_ID({name})')
with printer.block('.ascii =', ','):
printer.write('')
with printer.block('#define _Py_str_ascii_INIT', continuation=True):
for i in range(128):
printer.write(f'_PyASCIIObject_INIT("\\x{i:02x}"),')
immortal_objects.append(f'(PyObject *)&_Py_SINGLETON(strings).ascii[{i}]')
with printer.block('.latin1 =', ','):
printer.write('')
with printer.block('#define _Py_str_latin1_INIT', continuation=True):
for i in range(128, 256):
utf8 = ['"']
for c in chr(i).encode('utf-8'):
@ -326,10 +327,6 @@ def generate_runtime_init(identifiers, strings):
utf8.append('"')
printer.write(f'_PyUnicode_LATIN1_INIT("\\x{i:02x}", {"".join(utf8)}),')
immortal_objects.append(f'(PyObject *)&_Py_SINGLETON(strings).latin1[{i} - 128]')
printer.write('')
with printer.block('.tuple_empty =', ','):
printer.write('.ob_base = _PyVarObject_IMMORTAL_INIT(&PyTuple_Type, 0)')
immortal_objects.append(f'(PyObject *)&_Py_SINGLETON(tuple_empty)')
printer.write(END)
printer.write(after)
return immortal_objects
@ -366,7 +363,7 @@ def generate_static_strings_initializer(identifiers, strings):
printer.write(after)
def generate_global_object_finalizers(immortal_objects):
def generate_global_object_finalizers(generated_immortal_objects):
# Target the runtime initializer.
filename = os.path.join(INTERNAL, 'pycore_global_objects_fini_generated.h')
@ -387,8 +384,12 @@ def generate_global_object_finalizers(immortal_objects):
printer.write('#ifdef Py_DEBUG')
printer.write("static inline void")
with printer.block("_PyStaticObjects_CheckRefcnt(void)"):
for i in immortal_objects:
printer.write(f'_PyStaticObject_CheckRefcnt({i});')
printer.write('/* generated (see pycore_runtime_init_generated.h) */')
for ref in generated_immortal_objects:
printer.write(f'_PyStaticObject_CheckRefcnt({ref});')
printer.write('/* non-generated */')
for ref in NON_GENERATED_IMMORTAL_OBJECTS:
printer.write(f'_PyStaticObject_CheckRefcnt({ref});')
printer.write('#endif // Py_DEBUG')
printer.write(END)
printer.write(after)
@ -416,9 +417,9 @@ def main() -> None:
identifiers, strings = get_identifiers_and_strings()
generate_global_strings(identifiers, strings)
immortal_objects = generate_runtime_init(identifiers, strings)
generated_immortal_objects = generate_runtime_init(identifiers, strings)
generate_static_strings_initializer(identifiers, strings)
generate_global_object_finalizers(immortal_objects)
generate_global_object_finalizers(generated_immortal_objects)
if __name__ == '__main__':