Kaydet (Commit) 66d2c1f7 authored tarafından Jeremy Hylton's avatar Jeremy Hylton

Small optimizations in dispatch method: 1) lookup node's __class__ once

and store in local; 2) define _preorder to be dispatch (rather than
method that called dispatch).
üst 821eee33
import sys
from compiler import ast from compiler import ast
class ASTVisitor: class ASTVisitor:
...@@ -40,15 +41,6 @@ class ASTVisitor: ...@@ -40,15 +41,6 @@ class ASTVisitor:
self.node = None self.node = None
self._cache = {} self._cache = {}
def preorder(self, tree, visitor):
"""Do preorder walk of tree using visitor"""
self.visitor = visitor
visitor.visit = self._preorder
self._preorder(tree)
def _preorder(self, node, *args):
return apply(self.dispatch, (node,) + args)
def default(self, node, *args): def default(self, node, *args):
for child in node.getChildren(): for child in node.getChildren():
if isinstance(child, ast.Node): if isinstance(child, ast.Node):
...@@ -56,12 +48,14 @@ class ASTVisitor: ...@@ -56,12 +48,14 @@ class ASTVisitor:
def dispatch(self, node, *args): def dispatch(self, node, *args):
self.node = node self.node = node
meth = self._cache.get(node.__class__, None) klass = node.__class__
className = node.__class__.__name__ meth = self._cache.get(klass, None)
if meth is None: if meth is None:
className = klass.__name__
meth = getattr(self.visitor, 'visit' + className, self.default) meth = getattr(self.visitor, 'visit' + className, self.default)
self._cache[node.__class__] = meth self._cache[klass] = meth
if self.VERBOSE > 0: if self.VERBOSE > 0:
className = klass.__name__
if self.VERBOSE == 1: if self.VERBOSE == 1:
if meth == 0: if meth == 0:
print "dispatch", className print "dispatch", className
...@@ -69,6 +63,14 @@ class ASTVisitor: ...@@ -69,6 +63,14 @@ class ASTVisitor:
print "dispatch", className, (meth and meth.__name__ or '') print "dispatch", className, (meth and meth.__name__ or '')
return apply(meth, (node,) + args) return apply(meth, (node,) + args)
def preorder(self, tree, visitor):
"""Do preorder walk of tree using visitor"""
self.visitor = visitor
visitor.visit = self._preorder
self._preorder(tree)
_preorder = dispatch
class ExampleASTVisitor(ASTVisitor): class ExampleASTVisitor(ASTVisitor):
"""Prints examples of the nodes that aren't visited """Prints examples of the nodes that aren't visited
......
import sys
from compiler import ast from compiler import ast
class ASTVisitor: class ASTVisitor:
...@@ -40,15 +41,6 @@ class ASTVisitor: ...@@ -40,15 +41,6 @@ class ASTVisitor:
self.node = None self.node = None
self._cache = {} self._cache = {}
def preorder(self, tree, visitor):
"""Do preorder walk of tree using visitor"""
self.visitor = visitor
visitor.visit = self._preorder
self._preorder(tree)
def _preorder(self, node, *args):
return apply(self.dispatch, (node,) + args)
def default(self, node, *args): def default(self, node, *args):
for child in node.getChildren(): for child in node.getChildren():
if isinstance(child, ast.Node): if isinstance(child, ast.Node):
...@@ -56,12 +48,14 @@ class ASTVisitor: ...@@ -56,12 +48,14 @@ class ASTVisitor:
def dispatch(self, node, *args): def dispatch(self, node, *args):
self.node = node self.node = node
meth = self._cache.get(node.__class__, None) klass = node.__class__
className = node.__class__.__name__ meth = self._cache.get(klass, None)
if meth is None: if meth is None:
className = klass.__name__
meth = getattr(self.visitor, 'visit' + className, self.default) meth = getattr(self.visitor, 'visit' + className, self.default)
self._cache[node.__class__] = meth self._cache[klass] = meth
if self.VERBOSE > 0: if self.VERBOSE > 0:
className = klass.__name__
if self.VERBOSE == 1: if self.VERBOSE == 1:
if meth == 0: if meth == 0:
print "dispatch", className print "dispatch", className
...@@ -69,6 +63,14 @@ class ASTVisitor: ...@@ -69,6 +63,14 @@ class ASTVisitor:
print "dispatch", className, (meth and meth.__name__ or '') print "dispatch", className, (meth and meth.__name__ or '')
return apply(meth, (node,) + args) return apply(meth, (node,) + args)
def preorder(self, tree, visitor):
"""Do preorder walk of tree using visitor"""
self.visitor = visitor
visitor.visit = self._preorder
self._preorder(tree)
_preorder = dispatch
class ExampleASTVisitor(ASTVisitor): class ExampleASTVisitor(ASTVisitor):
"""Prints examples of the nodes that aren't visited """Prints examples of the nodes that aren't visited
......
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