speed up constant folding comparison in optimizeconst
The optimizeconst decorator compared old and new nodes with != on every expression visit (~24K calls). Since the optimizer usually returns the same object, an identity check (is not) short-circuits most of these. Also make Node.__eq__ compare fields via __dict__ directly instead of materializing an iter_fields tuple.
This commit is contained in:
parent
5e110ec0d9
commit
6e1496ef8b
@ -50,7 +50,7 @@ def optimizeconst(f: F) -> F:
|
||||
if self.optimizer is not None and not frame.eval_ctx.volatile:
|
||||
new_node = self.optimizer.visit(node, frame.eval_ctx)
|
||||
|
||||
if new_node != node:
|
||||
if new_node is not node and new_node != node:
|
||||
return self.visit(new_node, frame)
|
||||
|
||||
return f(self, node, frame, **kwargs)
|
||||
|
||||
@ -335,7 +335,12 @@ class Node(metaclass=NodeType):
|
||||
if type(self) is not type(other):
|
||||
return NotImplemented
|
||||
|
||||
return tuple(self.iter_fields()) == tuple(other.iter_fields())
|
||||
sd = self.__dict__
|
||||
od = other.__dict__
|
||||
for name in self.fields:
|
||||
if sd.get(name) != od.get(name):
|
||||
return False
|
||||
return True
|
||||
|
||||
__hash__ = object.__hash__
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user