Skip to content

Commit

Permalink
cache schemaview hash
Browse files Browse the repository at this point in the history
  • Loading branch information
sneakers-the-rat committed Aug 21, 2024
1 parent 3db7d7c commit b5f2b4b
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions linkml_runtime/utils/schemaview.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from copy import copy, deepcopy
from collections import defaultdict, deque
from pathlib import Path
from typing import Mapping, Tuple, TypeVar
from typing import Mapping, Optional, Tuple, TypeVar
import warnings

from linkml_runtime.utils.namespaces import Namespaces
Expand Down Expand Up @@ -145,6 +145,11 @@ class SchemaView(object):
modifications: int = 0
uuid: str = None

## private vars --------
# cached hash
_hash: Optional[int] = None


def __init__(self, schema: Union[str, Path, SchemaDefinition],
importmap: Optional[Dict[str, str]] = None, merge_imports: bool = False, base_dir: str = None):
if isinstance(schema, Path):
Expand All @@ -166,8 +171,10 @@ def __eq__(self, other):
return self.__key() == other.__key()
return NotImplemented

def __hash__(self):
return hash(self.__key())
def __hash__(self) -> int:
if self._hash is None:
self._hash = hash(self.__key())
return self._hash

@lru_cache(None)
def namespaces(self) -> Namespaces:
Expand Down Expand Up @@ -1850,6 +1857,7 @@ def copy_schema(self, new_name: str = None) -> SchemaDefinition:
return s2

def set_modified(self) -> None:
self._hash = None
self.modifications += 1

def materialize_patterns(self) -> None:
Expand Down

0 comments on commit b5f2b4b

Please sign in to comment.