Commit 294f23d8 authored by Stefan Behnel's avatar Stefan Behnel

avoid useless metaclass searching overhead when creating Python class without base classes

parent 21021a78
...@@ -6966,10 +6966,14 @@ class Py3ClassNode(ExprNode): ...@@ -6966,10 +6966,14 @@ class Py3ClassNode(ExprNode):
mkw = self.mkw.py_result() mkw = self.mkw.py_result()
else: else:
mkw = 'NULL' mkw = 'NULL'
if self.metaclass:
metaclass = self.metaclass.result()
else:
metaclass = "((PyObject*)&__Pyx_DefaultClassType)"
code.putln( code.putln(
'%s = __Pyx_Py3ClassCreate(%s, %s, %s, %s, %s, %d, %d); %s' % ( '%s = __Pyx_Py3ClassCreate(%s, %s, %s, %s, %s, %d, %d); %s' % (
self.result(), self.result(),
self.metaclass.result(), metaclass,
cname, cname,
self.bases.py_result(), self.bases.py_result(),
self.dict.py_result(), self.dict.py_result(),
...@@ -7163,10 +7167,14 @@ class PyClassNamespaceNode(ExprNode, ModuleNameMixin): ...@@ -7163,10 +7167,14 @@ class PyClassNamespaceNode(ExprNode, ModuleNameMixin):
mkw = self.mkw.py_result() mkw = self.mkw.py_result()
else: else:
mkw = '(PyObject *) NULL' mkw = '(PyObject *) NULL'
if self.metaclass:
metaclass = self.metaclass.result()
else:
metaclass = "(PyObject *) NULL"
code.putln( code.putln(
"%s = __Pyx_Py3MetaclassPrepare(%s, %s, %s, %s, %s, %s, %s); %s" % ( "%s = __Pyx_Py3MetaclassPrepare(%s, %s, %s, %s, %s, %s, %s); %s" % (
self.result(), self.result(),
self.metaclass.result(), metaclass,
self.bases.result(), self.bases.result(),
cname, cname,
qualname, qualname,
......
...@@ -4044,8 +4044,13 @@ class PyClassDefNode(ClassDefNode): ...@@ -4044,8 +4044,13 @@ class PyClassDefNode(ClassDefNode):
mkdict = self.mkw mkdict = self.mkw
else: else:
mkdict = None mkdict = None
self.metaclass = ExprNodes.PyClassMetaclassNode( if (not mkdict and
pos, mkw=mkdict, bases=self.bases) self.bases.is_sequence_constructor and
not self.bases.args):
pass # no base classes => no inherited metaclass
else:
self.metaclass = ExprNodes.PyClassMetaclassNode(
pos, mkw=mkdict, bases=self.bases)
needs_metaclass_calculation = False needs_metaclass_calculation = False
else: else:
needs_metaclass_calculation = True needs_metaclass_calculation = True
......
...@@ -109,10 +109,12 @@ ...@@ -109,10 +109,12 @@
#define __Pyx_BUILTIN_MODULE_NAME "__builtin__" #define __Pyx_BUILTIN_MODULE_NAME "__builtin__"
#define __Pyx_PyCode_New(a, k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos) \ #define __Pyx_PyCode_New(a, k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos) \
PyCode_New(a, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos) PyCode_New(a, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos)
#define __Pyx_DefaultClassType PyClass_Type
#else #else
#define __Pyx_BUILTIN_MODULE_NAME "builtins" #define __Pyx_BUILTIN_MODULE_NAME "builtins"
#define __Pyx_PyCode_New(a, k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos) \ #define __Pyx_PyCode_New(a, k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos) \
PyCode_New(a, k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos) PyCode_New(a, k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos)
#define __Pyx_DefaultClassType PyType_Type
#endif #endif
#if PY_VERSION_HEX < 0x02060000 #if PY_VERSION_HEX < 0x02060000
......
...@@ -830,25 +830,26 @@ static PyObject *__Pyx_Py3ClassCreate(PyObject *metaclass, PyObject *name, PyObj ...@@ -830,25 +830,26 @@ static PyObject *__Pyx_Py3ClassCreate(PyObject *metaclass, PyObject *name, PyObj
static PyObject *__Pyx_Py3MetaclassPrepare(PyObject *metaclass, PyObject *bases, PyObject *name, static PyObject *__Pyx_Py3MetaclassPrepare(PyObject *metaclass, PyObject *bases, PyObject *name,
PyObject *qualname, PyObject *mkw, PyObject *modname, PyObject *doc) { PyObject *qualname, PyObject *mkw, PyObject *modname, PyObject *doc) {
PyObject *prep;
PyObject *pargs;
PyObject *ns; PyObject *ns;
if (metaclass) {
prep = __Pyx_PyObject_GetAttrStr(metaclass, PYIDENT("__prepare__")); PyObject *prep = __Pyx_PyObject_GetAttrStr(metaclass, PYIDENT("__prepare__"));
if (!prep) { if (prep) {
if (unlikely(!PyErr_ExceptionMatches(PyExc_AttributeError))) PyObject *pargs = PyTuple_Pack(2, name, bases);
return NULL; if (unlikely(!pargs)) {
PyErr_Clear(); Py_DECREF(prep);
ns = PyDict_New(); return NULL;
} else { }
pargs = PyTuple_Pack(2, name, bases); ns = PyObject_Call(prep, pargs, mkw);
if (unlikely(!pargs)) {
Py_DECREF(prep); Py_DECREF(prep);
return NULL; Py_DECREF(pargs);
} else {
if (unlikely(!PyErr_ExceptionMatches(PyExc_AttributeError)))
return NULL;
PyErr_Clear();
ns = PyDict_New();
} }
ns = PyObject_Call(prep, pargs, mkw); } else {
Py_DECREF(prep); ns = PyDict_New();
Py_DECREF(pargs);
} }
if (unlikely(!ns)) if (unlikely(!ns))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment