cython
changeset 1582:13d4c8b44618
fixed bug in Visitor cache, reduces lxml compile time by ~20%
| author | Stefan Behnel <scoder@users.berlios.de> |
|---|---|
| date | Wed Nov 12 08:02:56 2008 +0100 (4 years ago) |
| parents | 04e83ffd8ea2 |
| children | 7c198901ec9f |
| files | Cython/Compiler/Visitor.py |
line diff
1.1 --- a/Cython/Compiler/Visitor.py Fri Nov 07 06:55:37 2008 +0100
1.2 +++ b/Cython/Compiler/Visitor.py Wed Nov 12 08:02:56 2008 +0100
1.3 @@ -14,26 +14,33 @@
1.4 # instance.
1.5 def __init__(self):
1.6 self.dispatch_table = {}
1.7 -
1.8 +
1.9 def visit(self, obj):
1.10 - cls = obj.__class__
1.11 - m = self.dispatch_table.get(cls.__name__)
1.12 - if m is None:
1.13 + cls = type(obj)
1.14 + try:
1.15 + handler_method = self.dispatch_table[cls]
1.16 + except KeyError:
1.17 + #print "Cache miss for class %s in visitor %s" % (
1.18 + # cls.__name__, type(self).__name__)
1.19 # Must resolve, try entire hierarchy
1.20 pattern = "visit_%s"
1.21 mro = inspect.getmro(cls)
1.22 - for cls in mro:
1.23 - m = getattr(self, pattern % cls.__name__, None)
1.24 - if m is not None:
1.25 + for mro_cls in mro:
1.26 + try:
1.27 + handler_method = getattr(self, pattern % mro_cls.__name__)
1.28 break
1.29 + except AttributeError:
1.30 + pass
1.31 else:
1.32 print type(self), type(obj)
1.33 - print self.access_path
1.34 - print self.access_path[-1][0].pos
1.35 - print self.access_path[-1][0].__dict__
1.36 + if hasattr(self, 'access_path'):
1.37 + print self.access_path
1.38 + print self.access_path[-1][0].pos
1.39 + print self.access_path[-1][0].__dict__
1.40 raise RuntimeError("Visitor does not accept object: %s" % obj)
1.41 - self.dispatch_table[cls.__name__] = m
1.42 - return m(obj)
1.43 + #print "Caching " + cls.__name__
1.44 + self.dispatch_table[cls] = handler_method
1.45 + return handler_method(obj)
1.46
1.47 class TreeVisitor(BasicVisitor):
1.48 """
