Author: Armin Rigo <[email protected]>
Branch:
Changeset: r45922:70abd7de1363
Date: 2011-07-24 00:16 +0200
http://bitbucket.org/pypy/pypy/changeset/70abd7de1363/
Log: Moving code around. The goal is to reduce the cost of
space.isinstance(), probably by a large factor: according to
valgrind, for every second we spend running
W_TypeObject.issubtype(), we spend almost 4 extra seconds in the
rest of space.isinstance(), because of the levels and levels of
indirections.
diff --git a/pypy/module/__builtin__/abstractinst.py
b/pypy/module/__builtin__/abstractinst.py
--- a/pypy/module/__builtin__/abstractinst.py
+++ b/pypy/module/__builtin__/abstractinst.py
@@ -58,7 +58,10 @@
# -- case (anything, type)
try:
- w_result = space.isinstance(w_obj, w_klass_or_tuple, allow_override)
+ if allow_override:
+ w_result = space.isinstance_allow_override(w_obj, w_klass_or_tuple)
+ else:
+ w_result = space.isinstance(w_obj, w_klass_or_tuple)
except OperationError, e: # if w_klass_or_tuple was not a type, ignore it
if not e.match(space, space.w_TypeError):
raise # propagate other errors
@@ -72,8 +75,11 @@
w_pretendtype = space.getattr(w_obj, space.wrap('__class__'))
if space.is_w(w_pretendtype, space.type(w_obj)):
return False # common case: obj.__class__ is type(obj)
- w_result = space.issubtype(w_pretendtype, w_klass_or_tuple,
- allow_override)
+ if allow_override:
+ w_result = space.issubtype_allow_override(w_pretendtype,
+ w_klass_or_tuple)
+ else:
+ w_result = space.issubtype(w_pretendtype, w_klass_or_tuple)
except OperationError, e:
if e.async(space):
raise
@@ -134,7 +140,11 @@
# -- case (type, type)
try:
- w_result = space.issubtype(w_derived, w_klass_or_tuple, allow_override)
+ if allow_override:
+ w_result = space.issubtype_allow_override(w_derived,
+ w_klass_or_tuple)
+ else:
+ w_result = space.issubtype(w_derived, w_klass_or_tuple)
except OperationError, e: # if one of the args was not a type, ignore it
if not e.match(space, space.w_TypeError):
raise # propagate other errors
diff --git a/pypy/objspace/descroperation.py b/pypy/objspace/descroperation.py
--- a/pypy/objspace/descroperation.py
+++ b/pypy/objspace/descroperation.py
@@ -497,22 +497,25 @@
space.wrap("coercion should return None or
2-tuple"))
return w_res
- def issubtype(space, w_sub, w_type, allow_override=False):
- if allow_override:
+ def issubtype(space, w_sub, w_type):
+ return space._type_issubtype(w_sub, w_type)
+
+ def isinstance(space, w_inst, w_type):
+ return space._type_isinstance(w_inst, w_type)
+
+ def issubtype_allow_override(space, w_sub, w_type):
w_check = space.lookup(w_type, "__subclasscheck__")
if w_check is None:
raise OperationError(space.w_TypeError,
space.wrap("issubclass not supported
here"))
return space.get_and_call_function(w_check, w_type, w_sub)
- return space._type_issubtype(w_sub, w_type)
- def isinstance(space, w_inst, w_type, allow_override=False):
- if allow_override:
+ def isinstance_allow_override(space, w_inst, w_type):
w_check = space.lookup(w_type, "__instancecheck__")
if w_check is not None:
return space.get_and_call_function(w_check, w_type, w_inst)
- return space.issubtype(space.type(w_inst), w_type, allow_override)
-
+ else:
+ return space.isinstance(w_inst, w_type)
# helpers
diff --git a/pypy/objspace/std/objspace.py b/pypy/objspace/std/objspace.py
--- a/pypy/objspace/std/objspace.py
+++ b/pypy/objspace/std/objspace.py
@@ -568,3 +568,8 @@
if isinstance(w_sub, W_TypeObject) and isinstance(w_type,
W_TypeObject):
return self.wrap(w_sub.issubtype(w_type))
raise OperationError(self.w_TypeError, self.wrap("need type objects"))
+
+ def _type_isinstance(self, w_inst, w_type):
+ if isinstance(w_type, W_TypeObject):
+ return self.wrap(self.type(w_inst).issubtype(w_type))
+ raise OperationError(self.w_TypeError, self.wrap("need type object"))
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit