summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmile Anclin <emile.anclin@logilab.fr>2010-12-14 10:52:13 +0100
committerEmile Anclin <emile.anclin@logilab.fr>2010-12-14 10:52:13 +0100
commitb4533c8894410ad0ba088b15017e89d3b32b2708 (patch)
treea96c45a81c7ab4d7493f5b5ae8947a5468fa88b8
parent9c0e2aa622a8d8f85fae48d6795b4bfabe1ccaa4 (diff)
downloadastroid-git-b4533c8894410ad0ba088b15017e89d3b32b2708.tar.gz
performance : a little cash for visit methods
remember that visit is called tousands of time for each module; remove print from a test
-rw-r--r--rebuilder.py16
-rw-r--r--test/unittest_nodes.py1
2 files changed, 12 insertions, 5 deletions
diff --git a/rebuilder.py b/rebuilder.py
index d423d308..d86013e3 100644
--- a/rebuilder.py
+++ b/rebuilder.py
@@ -125,9 +125,12 @@ def _set_infos(oldnode, newnode, parent):
newnode.set_line_info(newnode.last_child()) # set_line_info accepts None
+
+
class TreeRebuilder(object):
"""Rebuilds the _ast tree to become an ASTNG tree"""
+ _visit_meths = {}
def __init__(self):
self.init()
@@ -139,10 +142,15 @@ class TreeRebuilder(object):
self._delayed_assattr = []
def visit(self, node, parent):
- cls_name = node.__class__.__name__
- visit_name = 'visit_' + REDIRECT.get(cls_name, cls_name).lower()
- visit_method = getattr(self, visit_name)
- return visit_method(node, parent)
+ cls = node.__class__
+ if cls in self._visit_meths:
+ return self._visit_meths[cls](node, parent)
+ else:
+ cls_name = cls.__name__
+ visit_name = 'visit_' + REDIRECT.get(cls_name, cls_name).lower()
+ visit_method = getattr(self, visit_name)
+ self._visit_meths[cls] = visit_method
+ return visit_method(node, parent)
def _save_assignment(self, node, name=None):
"""save assignement situation since node.parent is not available yet"""
diff --git a/test/unittest_nodes.py b/test/unittest_nodes.py
index 944f8a80..8af5f790 100644
--- a/test/unittest_nodes.py
+++ b/test/unittest_nodes.py
@@ -333,7 +333,6 @@ del True
self.assertIsInstance(ass_true, nodes.AssName)
self.assertEqual(ass_true.name, "True")
del_true = ast.body[2].targets[0]
- print ast.body[2], ast.body[2].targets
self.assertIsInstance(del_true, nodes.DelName)
self.assertEqual(del_true.name, "True")