[pypy-svn] r17952 - in pypy/dist/pypy: interpreter objspace/std
arigo at codespeak.net
arigo at codespeak.net
Wed Sep 28 23:32:14 CEST 2005
Author: arigo
Date: Wed Sep 28 23:32:14 2005
New Revision: 17952
Modified:
pypy/dist/pypy/interpreter/baseobjspace.py
pypy/dist/pypy/interpreter/mixedmodule.py
pypy/dist/pypy/objspace/std/objspace.py
Log:
Shortcut for performance: getdictvalue() can return None faster, using a
space.finditem() that is a non-raising version of space.getitem().
finditem() is implemented as a try:except: around a space.getitem(), with a
shortcut for the common case of W_DictObject.
Modified: pypy/dist/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/dist/pypy/interpreter/baseobjspace.py (original)
+++ pypy/dist/pypy/interpreter/baseobjspace.py Wed Sep 28 23:32:14 2005
@@ -21,11 +21,7 @@
def getdictvalue(self, space, attr):
w_dict = self.getdict()
if w_dict is not None:
- try:
- return space.getitem(w_dict, space.wrap(attr))
- except OperationError, e:
- if not e.match(space, space.w_KeyError):
- raise
+ return space.finditem(w_dict, space.wrap(attr))
return None
def setdict(self, space, w_dict):
@@ -373,6 +369,14 @@
"""shortcut for space.int_w(space.hash(w_obj))"""
return self.int_w(self.hash(w_obj))
+ def finditem(self, w_obj, w_key):
+ try:
+ return self.getitem(w_obj, w_key)
+ except OperationError, e:
+ if e.match(self, self.w_KeyError):
+ return None
+ raise
+
def newbool(self, b):
if b:
return self.w_True
Modified: pypy/dist/pypy/interpreter/mixedmodule.py
==============================================================================
--- pypy/dist/pypy/interpreter/mixedmodule.py (original)
+++ pypy/dist/pypy/interpreter/mixedmodule.py Wed Sep 28 23:32:14 2005
@@ -31,13 +31,8 @@
return self.space.call_function(w_builtin, *args_w)
def getdictvalue(self, space, name):
- try:
- return space.getitem(self.w_dict, space.wrap(name))
- except OperationError, e:
- if not e.match(space, space.w_KeyError):
- raise
- if not self.lazy:
- return None
+ w_value = space.finditem(self.w_dict, space.wrap(name))
+ if self.lazy and w_value is None:
try:
loader = self.loaders[name]
except KeyError:
@@ -57,7 +52,7 @@
func._builtinversion_ = bltin
w_value = space.wrap(bltin)
space.setitem(self.w_dict, space.wrap(name), w_value)
- return w_value
+ return w_value
def getdict(self):
if self.lazy:
Modified: pypy/dist/pypy/objspace/std/objspace.py
==============================================================================
--- pypy/dist/pypy/objspace/std/objspace.py (original)
+++ pypy/dist/pypy/objspace/std/objspace.py Wed Sep 28 23:32:14 2005
@@ -397,11 +397,18 @@
def is_true(self, w_obj):
# XXX don't look!
- if isinstance(w_obj, W_DictObject):
+ if type(w_obj) is W_DictObject:
return len(w_obj.content) != 0
else:
return DescrOperation.is_true(self, w_obj)
+ def finditem(self, w_obj, w_key):
+ # performance shortcut to avoid creating the OperationError(KeyError)
+ if type(w_obj) is W_DictObject:
+ return w_obj.content.get(w_key, None)
+ else:
+ return ObjSpace.finditem(self, w_obj, w_key)
+
# support for the deprecated __getslice__, __setslice__, __delslice__
def getslice(self, w_obj, w_start, w_stop):
More information about the Pypy-commit
mailing list