Commit ee5b6d49 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Optimize some type-checking

parent 61a68d3d
......@@ -2558,11 +2558,11 @@ static int mro_internal(PyTypeObject* type) noexcept {
extern "C" int PyType_IsSubtype(PyTypeObject* a, PyTypeObject* b) noexcept {
PyObject* mro;
if (!(a->tp_flags & Py_TPFLAGS_HAVE_CLASS))
if (unlikely(!(a->tp_flags & Py_TPFLAGS_HAVE_CLASS)))
return b == a || b == &PyBaseObject_Type;
mro = a->tp_mro;
if (mro != NULL) {
if (likely(mro != NULL)) {
/* Deal with multiple inheritance without recursion
by walking the MRO tuple */
Py_ssize_t i, n;
......
......@@ -468,7 +468,7 @@ template <ExceptionStyle S> Box* getattrFunc(Box* obj, Box* _str, Box* default_v
throw e;
}
if (!isSubclass(_str->cls, str_cls)) {
if (!PyString_Check(_str)) {
if (S == CAPI) {
PyErr_SetString(TypeError, "getattr(): attribute name must be string");
return NULL;
......
......@@ -244,11 +244,6 @@ extern "C" void my_assert(bool b) {
assert(b);
}
extern "C" bool isSubclass(BoxedClass* child, BoxedClass* parent) {
STAT_TIMER(t0, "us_timer_isSubclass", 10);
return PyType_IsSubtype(child, parent);
}
extern "C" void assertFail(Box* assertion_type, Box* msg) {
RELEASE_ASSERT(assertion_type->cls == type_cls, "%s", assertion_type->cls->tp_name);
if (msg) {
......
......@@ -100,7 +100,11 @@ extern "C" Box** unpackIntoArray(Box* obj, int64_t expected_size);
extern "C" void assertNameDefined(bool b, const char* name, BoxedClass* exc_cls, bool local_var_msg);
extern "C" void assertFailDerefNameDefined(const char* name);
extern "C" void assertFail(Box* assertion_type, Box* msg);
extern "C" bool isSubclass(BoxedClass* child, BoxedClass* parent);
inline bool isSubclass(BoxedClass* child, BoxedClass* parent) {
return child == parent || PyType_IsSubtype(child, parent);
}
extern "C" BoxedClosure* createClosure(BoxedClosure* parent_closure, size_t size);
Box* getiter(Box* o);
......
......@@ -328,7 +328,7 @@ Box* tupleCount(BoxedTuple* self, Box* elt) {
}
extern "C" Box* tupleNew(Box* _cls, BoxedTuple* args, BoxedDict* kwargs) {
if (!isSubclass(_cls->cls, type_cls))
if (!PyType_Check(_cls))
raiseExcHelper(TypeError, "tuple.__new__(X): X is not a type object (%s)", getTypeName(_cls));
BoxedClass* cls = static_cast<BoxedClass*>(_cls);
......
......@@ -201,7 +201,7 @@ void* BoxVar::operator new(size_t size, BoxedClass* cls, size_t nitems) {
assert(cls);
// See definition of BoxedTuple for some notes on why we need this special case:
ASSERT(isSubclass(cls, tuple_cls) || cls->tp_basicsize >= size, "%s", cls->tp_name);
ASSERT(cls->tp_basicsize >= size || isSubclass(cls, tuple_cls), "%s", cls->tp_name);
assert(cls->tp_itemsize > 0);
assert(cls->tp_alloc);
......
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