Skip to content

Commit f782627

Browse files
committed
[mypyc] Intern a few more strings
1 parent 7fee02c commit f782627

5 files changed

Lines changed: 33 additions & 12 deletions

File tree

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
@@ -107,7 +107,7 @@ static bool _CPy_IsSafeMetaClass(PyTypeObject *metaclass) {
107107
// manage to work with TypingMeta and its friends.
108108
if (metaclass == &PyType_Type)
109109
return true;
110-
PyObject *module = PyObject_GetAttrString((PyObject *)metaclass, "__module__");
110+
PyObject *module = PyObject_GetAttr((PyObject *)metaclass, mypyc_interned_str.__module__);
111111
if (!module) {
112112
PyErr_Clear();
113113
return false;
@@ -242,7 +242,7 @@ PyObject *CPyType_FromTemplate(PyObject *template,
242242
sizeof(PyTypeObject) - sizeof(PyVarObject));
243243

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

@@ -285,7 +285,7 @@ PyObject *CPyType_FromTemplate(PyObject *template,
285285

286286
// Reject anything that would give us a nontrivial __slots__,
287287
// because the layout will conflict
288-
slots = PyObject_GetAttrString((PyObject *)t, "__slots__");
288+
slots = PyObject_GetAttr((PyObject *)t, mypyc_interned_str.__slots__);
289289
if (slots) {
290290
// don't fail on an empty __slots__
291291
int is_true = PyObject_IsTrue(slots);
@@ -298,7 +298,7 @@ PyObject *CPyType_FromTemplate(PyObject *template,
298298
PyErr_Clear();
299299
}
300300

301-
if (PyObject_SetAttrString((PyObject *)t, "__module__", modname) < 0)
301+
if (PyObject_SetAttr((PyObject *)t, mypyc_interned_str.__module__, modname) < 0)
302302
goto error;
303303

304304
if (init_subclass((PyTypeObject *)t, NULL))
@@ -458,7 +458,7 @@ CPyPickle_GetState(PyObject *obj)
458458
{
459459
PyObject *attrs = NULL, *state = NULL;
460460

461-
attrs = PyObject_GetAttrString((PyObject *)Py_TYPE(obj), "__mypyc_attrs__");
461+
attrs = PyObject_GetAttr((PyObject *)Py_TYPE(obj), mypyc_interned_str.__mypyc_attrs__);
462462
if (!attrs) {
463463
goto fail;
464464
}
@@ -734,7 +734,7 @@ int CPyStatics_Initialize(PyObject **statics,
734734
// Call super(type(self), self)
735735
PyObject *
736736
CPy_Super(PyObject *builtins, PyObject *self) {
737-
PyObject *super_type = PyObject_GetAttrString(builtins, "super");
737+
PyObject *super_type = PyObject_GetAttr(builtins, mypyc_interned_str.super);
738738
if (!super_type)
739739
return NULL;
740740
PyObject *result = PyObject_CallFunctionObjArgs(
@@ -889,7 +889,7 @@ CPy_CallReverseOpMethod(PyObject *left,
889889
PyObject *CPySingledispatch_RegisterFunction(PyObject *singledispatch_func,
890890
PyObject *cls,
891891
PyObject *func) {
892-
PyObject *registry = PyObject_GetAttrString(singledispatch_func, "registry");
892+
PyObject *registry = PyObject_GetAttr(singledispatch_func, mypyc_interned_str.registry);
893893
PyObject *register_func = NULL;
894894
PyObject *typing = NULL;
895895
PyObject *get_type_hints = NULL;
@@ -902,7 +902,7 @@ PyObject *CPySingledispatch_RegisterFunction(PyObject *singledispatch_func,
902902
// passed a class
903903
// bind cls to the first argument so that register gets called again with both the
904904
// class and the function
905-
register_func = PyObject_GetAttrString(singledispatch_func, "register");
905+
register_func = PyObject_GetAttr(singledispatch_func, mypyc_interned_str.register_);
906906
if (register_func == NULL) goto fail;
907907
return PyMethod_New(register_func, cls);
908908
}
@@ -923,7 +923,7 @@ PyObject *CPySingledispatch_RegisterFunction(PyObject *singledispatch_func,
923923
func = cls;
924924
typing = PyImport_ImportModule("typing");
925925
if (typing == NULL) goto fail;
926-
get_type_hints = PyObject_GetAttrString(typing, "get_type_hints");
926+
get_type_hints = PyObject_GetAttr(typing, mypyc_interned_str.get_type_hints);
927927

928928
type_hints = PyObject_CallOneArg(get_type_hints, func);
929929
PyObject *argname;
@@ -944,7 +944,7 @@ PyObject *CPySingledispatch_RegisterFunction(PyObject *singledispatch_func,
944944
}
945945

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

mypyc/lib-rt/static_data.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@ mypyc_interned_str_struct mypyc_interned_str;
1717
int
1818
intern_strings(void) {
1919
INTERN_STRING(__init_subclass__, "__init_subclass__");
20+
INTERN_STRING(__module__, "__module__");
2021
INTERN_STRING(__mro_entries__, "__mro_entries__");
22+
INTERN_STRING(__mypyc_attrs__, "__mypyc_attrs__");
2123
INTERN_STRING(__name__, "__name__");
24+
INTERN_STRING(__orig_bases__, "__orig_bases__");
25+
INTERN_STRING(__qualname__, "__qualname__");
26+
INTERN_STRING(__slots__, "__slots__");
2227
INTERN_STRING(__radd__, "__radd__");
2328
INTERN_STRING(__rsub__, "__rsub__");
2429
INTERN_STRING(__rmul__, "__rmul__");
@@ -42,12 +47,17 @@ intern_strings(void) {
4247
INTERN_STRING(clear, "clear");
4348
INTERN_STRING(close_, "close");
4449
INTERN_STRING(copy, "copy");
50+
INTERN_STRING(dispatch_cache, "dispatch_cache");
51+
INTERN_STRING(get_type_hints, "get_type_hints");
4552
INTERN_STRING(keys, "keys");
4653
INTERN_STRING(items, "items");
4754
INTERN_STRING(join, "join");
55+
INTERN_STRING(register_, "register");
56+
INTERN_STRING(registry, "registry");
4857
INTERN_STRING(send, "send");
4958
INTERN_STRING(setdefault, "setdefault");
5059
INTERN_STRING(startswith, "startswith");
60+
INTERN_STRING(super, "super");
5161
INTERN_STRING(throw_, "throw");
5262
INTERN_STRING(translate, "translate");
5363
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)