Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[perf] Cache schemaview hash #329

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading