Skip to content

Commit 0df48e1

Browse files
committed
cache for all_atoms, to help optimize parsing tasks
1 parent 5f132fb commit 0df48e1

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

src/hyperbase/hyperedge.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ def __len__(self) -> int:
4444
return len(self._edges)
4545

4646
def __hash__(self) -> int:
47-
return hash(self._edges)
47+
h = self._cache.get("_hash")
48+
if h is None:
49+
h = hash(self._edges)
50+
self._cache["_hash"] = h
51+
return h
4852

4953
def __eq__(self, other: object) -> bool:
5054
if isinstance(other, Hyperedge):
@@ -183,9 +187,13 @@ def all_atoms(self) -> list[Atom]:
183187
in this case, edge.all_atoms() returns:
184188
[the/md, of/br, mayor/cc, the/md, city/cs]
185189
"""
190+
cached = self._cache.get("all_atoms")
191+
if cached is not None:
192+
return cached
186193
atoms: list[Atom] = []
187194
for item in self:
188195
atoms += item.all_atoms()
196+
self._cache["all_atoms"] = atoms
189197
return atoms
190198

191199
def size(self) -> int:

0 commit comments

Comments
 (0)