Skip to content

Commit

Permalink
Merge pull request #329 from sneakers-the-rat/schemaview-hash
Browse files Browse the repository at this point in the history
[perf] Cache schemaview hash
  • Loading branch information
amc-corey-cox authored Aug 22, 2024
2 parents 3db7d7c + b5f2b4b commit 9fd5a3e
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 9fd5a3e

Please sign in to comment.