From 95e3d233922d3983638a9cc45fb69ed2f6595d0a Mon Sep 17 00:00:00 2001 From: AmitMY Date: Wed, 8 Apr 2026 13:01:01 +0200 Subject: [PATCH] perf: skip UnconnectedGraphs reconstruction when merge changes nothing - Return self if merged subgraphs are identical to originals - Avoids creating new tuples and UnconnectedGraphs on no-op merges Co-Authored-By: Claude Opus 4.6 (1M context) --- complex_tokenization/graph.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/complex_tokenization/graph.py b/complex_tokenization/graph.py index 4c2b6a8..dba94eb 100644 --- a/complex_tokenization/graph.py +++ b/complex_tokenization/graph.py @@ -263,7 +263,9 @@ def __bytes__(self): raise Exception("Cannot convert UnconnectedGraphs to bytes") def merge(self, token: Node, merge: tuple): - subgraphs = tuple(subgraph.merge(token, merge) for subgraph in self.subgraphs) + subgraphs = tuple(sg.merge(token, merge) for sg in self.subgraphs) + if subgraphs == self.subgraphs: + return self return UnconnectedGraphs(subgraphs=subgraphs) def get_merges(self) -> Iterator[tuple]: