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:
tobymao 2026-03-21 23:24:26 -07:00
parent 5e110ec0d9
commit 6e1496ef8b
No known key found for this signature in database
GPG Key ID: 5B3B30F3BACB48DC
2 changed files with 7 additions and 2 deletions

View File

@ -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)

View File

@ -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__