Skip to content

Commit 9fd5a3e

Browse files
Merge pull request #329 from sneakers-the-rat/schemaview-hash
[perf] Cache schemaview hash
2 parents 3db7d7c + b5f2b4b commit 9fd5a3e

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

linkml_runtime/utils/schemaview.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from copy import copy, deepcopy
77
from collections import defaultdict, deque
88
from pathlib import Path
9-
from typing import Mapping, Tuple, TypeVar
9+
from typing import Mapping, Optional, Tuple, TypeVar
1010
import warnings
1111

1212
from linkml_runtime.utils.namespaces import Namespaces
@@ -145,6 +145,11 @@ class SchemaView(object):
145145
modifications: int = 0
146146
uuid: str = None
147147

148+
## private vars --------
149+
# cached hash
150+
_hash: Optional[int] = None
151+
152+
148153
def __init__(self, schema: Union[str, Path, SchemaDefinition],
149154
importmap: Optional[Dict[str, str]] = None, merge_imports: bool = False, base_dir: str = None):
150155
if isinstance(schema, Path):
@@ -166,8 +171,10 @@ def __eq__(self, other):
166171
return self.__key() == other.__key()
167172
return NotImplemented
168173

169-
def __hash__(self):
170-
return hash(self.__key())
174+
def __hash__(self) -> int:
175+
if self._hash is None:
176+
self._hash = hash(self.__key())
177+
return self._hash
171178

172179
@lru_cache(None)
173180
def namespaces(self) -> Namespaces:
@@ -1850,6 +1857,7 @@ def copy_schema(self, new_name: str = None) -> SchemaDefinition:
18501857
return s2
18511858

18521859
def set_modified(self) -> None:
1860+
self._hash = None
18531861
self.modifications += 1
18541862

18551863
def materialize_patterns(self) -> None:

0 commit comments

Comments
 (0)