From 53013fa01f45114c1b8ee87527eab4fbeef8a113 Mon Sep 17 00:00:00 2001 From: "Eevee (Alex Munroe)" Date: Mon, 24 Mar 2014 18:00:17 -0700 Subject: Speed up rebuilder considerably by computing line numbers lazily. Fetching the last child of each of hundreds of thousands of nodes is relatively expensive, and most of the time this information is never used, so don't actually figure it out until it's asked for. Saves the overhead of quite a few function calls, too. --- node_classes.py | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) (limited to 'node_classes.py') diff --git a/node_classes.py b/node_classes.py index e247392e..45a00ba9 100644 --- a/node_classes.py +++ b/node_classes.py @@ -20,6 +20,8 @@ import sys +from logilab.common.decorators import cachedproperty + from astroid.exceptions import NoDefault from astroid.bases import (NodeNG, Statement, Instance, InferenceContext, _infer_stmts, YES, BUILTINS) @@ -267,6 +269,11 @@ class Arguments(NodeNG, AssignTypeMixin): return name return None + @cachedproperty + def fromlineno(self): + lineno = super(Arguments, self).fromlineno + return max(lineno, self.parent.fromlineno) + def format_args(self): """return arguments formatted as string""" result = [] @@ -560,7 +567,8 @@ class ExceptHandler(Statement, AssignTypeMixin): name = None body = None - def _blockstart_toline(self): + @cachedproperty + def blockstart_tolineno(self): if self.name: return self.name.tolineno elif self.type: @@ -568,11 +576,6 @@ class ExceptHandler(Statement, AssignTypeMixin): else: return self.lineno - def set_line_info(self, lastchild): - self.fromlineno = self.lineno - self.tolineno = lastchild.tolineno - self.blockstart_tolineno = self._blockstart_toline() - def catch(self, exceptions): if self.type is None or exceptions is None: return True @@ -603,7 +606,8 @@ class For(BlockRangeMixIn, AssignTypeMixin, Statement): orelse = None optional_assign = True - def _blockstart_toline(self): + @cachedproperty + def blockstart_tolineno(self): return self.iter.tolineno @@ -638,7 +642,8 @@ class If(BlockRangeMixIn, Statement): body = None orelse = None - def _blockstart_toline(self): + @cachedproperty + def blockstart_tolineno(self): return self.test.tolineno def block_range(self, lineno): @@ -789,9 +794,6 @@ class TryExcept(BlockRangeMixIn, Statement): def _infer_name(self, frame, name): return name - def _blockstart_toline(self): - return self.lineno - def block_range(self, lineno): """handle block line numbers range for try/except statements""" last = None @@ -811,9 +813,6 @@ class TryFinally(BlockRangeMixIn, Statement): body = None finalbody = None - def _blockstart_toline(self): - return self.lineno - def block_range(self, lineno): """handle block line numbers range for try/finally statements""" child = self.body[0] @@ -857,7 +856,8 @@ class While(BlockRangeMixIn, Statement): body = None orelse = None - def _blockstart_toline(self): + @cachedproperty + def blockstart_tolineno(self): return self.test.tolineno def block_range(self, lineno): @@ -871,7 +871,8 @@ class With(BlockRangeMixIn, AssignTypeMixin, Statement): items = None body = None - def _blockstart_toline(self): + @cachedproperty + def blockstart_tolineno(self): return self.items[-1][0].tolineno def get_children(self): -- cgit v1.2.1