Skip to content

Commit 2322834

Browse files
authored
[mypyc] Intern a few more strings (#20537)
Followup to #20460
1 parent 37f35db commit 2322834

File tree

5 files changed

+33
-12
lines changed

5 files changed

+33
-12
lines changed

mypyc/lib-rt/exc_ops.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,11 @@ static PyObject *CPy_GetTypeName(PyObject *type) {
123123
PyObject *module = NULL, *name = NULL;
124124
PyObject *full = NULL;
125125

126-
module = PyObject_GetAttrString(type, "__module__");
126+
module = PyObject_GetAttr(type, mypyc_interned_str.__module__);
127127
if (!module || !PyUnicode_Check(module)) {
128128
goto out;
129129
}
130-
name = PyObject_GetAttrString(type, "__qualname__");
130+
name = PyObject_GetAttr(type, mypyc_interned_str.__qualname__);
131131
if (!name || !PyUnicode_Check(name)) {
132132
goto out;
133133
}

mypyc/lib-rt/librt_strings.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,5 +449,6 @@ static PyModuleDef librt_strings_module = {
449449
PyMODINIT_FUNC
450450
PyInit_strings(void)
451451
{
452+
intern_strings();
452453
return PyModuleDef_Init(&librt_strings_module);
453454
}

mypyc/lib-rt/misc_ops.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ static bool _CPy_IsSafeMetaClass(PyTypeObject *metaclass) {
109109
// manage to work with TypingMeta and its friends.
110110
if (metaclass == &PyType_Type)
111111
return true;
112-
PyObject *module = PyObject_GetAttrString((PyObject *)metaclass, "__module__");
112+
PyObject *module = PyObject_GetAttr((PyObject *)metaclass, mypyc_interned_str.__module__);
113113
if (!module) {
114114
PyErr_Clear();
115115
return false;
@@ -244,7 +244,7 @@ PyObject *CPyType_FromTemplate(PyObject *template,
244244
sizeof(PyTypeObject) - sizeof(PyVarObject));
245245

246246
if (bases != orig_bases) {
247-
if (PyObject_SetAttrString((PyObject *)t, "__orig_bases__", orig_bases) < 0)
247+
if (PyObject_SetAttr((PyObject *)t, mypyc_interned_str.__orig_bases__, orig_bases) < 0)
248248
goto error;
249249
}
250250

@@ -287,7 +287,7 @@ PyObject *CPyType_FromTemplate(PyObject *template,
287287

288288
// Reject anything that would give us a nontrivial __slots__,
289289
// because the layout will conflict
290-
slots = PyObject_GetAttrString((PyObject *)t, "__slots__");
290+
slots = PyObject_GetAttr((PyObject *)t, mypyc_interned_str.__slots__);
291291
if (slots) {
292292
// don't fail on an empty __slots__
293293
int is_true = PyObject_IsTrue(slots);
@@ -300,7 +300,7 @@ PyObject *CPyType_FromTemplate(PyObject *template,
300300
PyErr_Clear();
301301
}
302302

303-
if (PyObject_SetAttrString((PyObject *)t, "__module__", modname) < 0)
303+
if (PyObject_SetAttr((PyObject *)t, mypyc_interned_str.__module__, modname) < 0)
304304
goto error;
305305

306306
if (init_subclass((PyTypeObject *)t, NULL))
@@ -460,7 +460,7 @@ CPyPickle_GetState(PyObject *obj)
460460
{
461461
PyObject *attrs = NULL, *state = NULL;
462462

463-
attrs = PyObject_GetAttrString((PyObject *)Py_TYPE(obj), "__mypyc_attrs__");
463+
attrs = PyObject_GetAttr((PyObject *)Py_TYPE(obj), mypyc_interned_str.__mypyc_attrs__);
464464
if (!attrs) {
465465
goto fail;
466466
}
@@ -736,7 +736,7 @@ int CPyStatics_Initialize(PyObject **statics,
736736
// Call super(type(self), self)
737737
PyObject *
738738
CPy_Super(PyObject *builtins, PyObject *self) {
739-
PyObject *super_type = PyObject_GetAttrString(builtins, "super");
739+
PyObject *super_type = PyObject_GetAttr(builtins, mypyc_interned_str.super);
740740
if (!super_type)
741741
return NULL;
742742
PyObject *result = PyObject_CallFunctionObjArgs(
@@ -891,7 +891,7 @@ CPy_CallReverseOpMethod(PyObject *left,
891891
PyObject *CPySingledispatch_RegisterFunction(PyObject *singledispatch_func,
892892
PyObject *cls,
893893
PyObject *func) {
894-
PyObject *registry = PyObject_GetAttrString(singledispatch_func, "registry");
894+
PyObject *registry = PyObject_GetAttr(singledispatch_func, mypyc_interned_str.registry);
895895
PyObject *register_func = NULL;
896896
PyObject *typing = NULL;
897897
PyObject *get_type_hints = NULL;
@@ -904,7 +904,7 @@ PyObject *CPySingledispatch_RegisterFunction(PyObject *singledispatch_func,
904904
// passed a class
905905
// bind cls to the first argument so that register gets called again with both the
906906
// class and the function
907-
register_func = PyObject_GetAttrString(singledispatch_func, "register");
907+
register_func = PyObject_GetAttr(singledispatch_func, mypyc_interned_str.register_);
908908
if (register_func == NULL) goto fail;
909909
return PyMethod_New(register_func, cls);
910910
}
@@ -925,7 +925,7 @@ PyObject *CPySingledispatch_RegisterFunction(PyObject *singledispatch_func,
925925
func = cls;
926926
typing = PyImport_ImportModule("typing");
927927
if (typing == NULL) goto fail;
928-
get_type_hints = PyObject_GetAttrString(typing, "get_type_hints");
928+
get_type_hints = PyObject_GetAttr(typing, mypyc_interned_str.get_type_hints);
929929

930930
type_hints = PyObject_CallOneArg(get_type_hints, func);
931931
PyObject *argname;
@@ -946,7 +946,7 @@ PyObject *CPySingledispatch_RegisterFunction(PyObject *singledispatch_func,
946946
}
947947

948948
// clear the cache so we consider the newly added function when dispatching
949-
PyObject *dispatch_cache = PyObject_GetAttrString(singledispatch_func, "dispatch_cache");
949+
PyObject *dispatch_cache = PyObject_GetAttr(singledispatch_func, mypyc_interned_str.dispatch_cache);
950950
if (dispatch_cache == NULL) goto fail;
951951
PyDict_Clear(dispatch_cache);
952952

mypyc/lib-rt/static_data.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@ intern_strings(void) {
2121
return 0;
2222
}
2323
INTERN_STRING(__init_subclass__, "__init_subclass__");
24+
INTERN_STRING(__module__, "__module__");
2425
INTERN_STRING(__mro_entries__, "__mro_entries__");
26+
INTERN_STRING(__mypyc_attrs__, "__mypyc_attrs__");
2527
INTERN_STRING(__name__, "__name__");
28+
INTERN_STRING(__orig_bases__, "__orig_bases__");
29+
INTERN_STRING(__qualname__, "__qualname__");
30+
INTERN_STRING(__slots__, "__slots__");
2631
INTERN_STRING(__radd__, "__radd__");
2732
INTERN_STRING(__rsub__, "__rsub__");
2833
INTERN_STRING(__rmul__, "__rmul__");
@@ -46,12 +51,17 @@ intern_strings(void) {
4651
INTERN_STRING(clear, "clear");
4752
INTERN_STRING(close_, "close");
4853
INTERN_STRING(copy, "copy");
54+
INTERN_STRING(dispatch_cache, "dispatch_cache");
55+
INTERN_STRING(get_type_hints, "get_type_hints");
4956
INTERN_STRING(keys, "keys");
5057
INTERN_STRING(items, "items");
5158
INTERN_STRING(join, "join");
59+
INTERN_STRING(register_, "register");
60+
INTERN_STRING(registry, "registry");
5261
INTERN_STRING(send, "send");
5362
INTERN_STRING(setdefault, "setdefault");
5463
INTERN_STRING(startswith, "startswith");
64+
INTERN_STRING(super, "super");
5565
INTERN_STRING(throw_, "throw");
5666
INTERN_STRING(translate, "translate");
5767
INTERN_STRING(update, "update");

mypyc/lib-rt/static_data.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ int intern_strings(void);
1313

1414
typedef struct mypyc_interned_str_struct {
1515
PyObject *__init_subclass__;
16+
PyObject *__module__;
1617
PyObject *__mro_entries__;
18+
PyObject *__mypyc_attrs__;
19+
PyObject *__orig_bases__;
20+
PyObject *__qualname__;
21+
PyObject *__slots__;
1722
PyObject *__name__;
1823
PyObject *__radd__;
1924
PyObject *__rsub__;
@@ -38,12 +43,17 @@ typedef struct mypyc_interned_str_struct {
3843
PyObject *clear;
3944
PyObject *close_;
4045
PyObject *copy;
46+
PyObject *dispatch_cache;
47+
PyObject *get_type_hints;
4148
PyObject *keys;
4249
PyObject *items;
4350
PyObject *join;
51+
PyObject *register_;
52+
PyObject *registry;
4453
PyObject *send;
4554
PyObject *setdefault;
4655
PyObject *startswith;
56+
PyObject *super;
4757
PyObject *throw_;
4858
PyObject *translate;
4959
PyObject *update;

0 commit comments

Comments
 (0)