diff --git a/environment.yml b/environment.yml index 89ac8d1..8d0ab7c 100644 --- a/environment.yml +++ b/environment.yml @@ -11,6 +11,8 @@ dependencies: - simpeg==0.25.2 - pymatsolver==0.4.0 - discretize==0.12.0 + # Optional dependencies + - python-xxhash # Required by notebooks - harmonica - verde diff --git a/pyproject.toml b/pyproject.toml index 6a78028..8fdc8c5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,6 +14,9 @@ dependencies = [ "discretize==0.12.0", ] +[project.optional-dependencies] +xxhash = ["xxhash"] + [build-system] requires = ["setuptools>=61", "wheel", "setuptools_scm[toml]>=6.2"] build-backend = "setuptools.build_meta" diff --git a/requirements-dev.txt b/requirements-dev.txt index c150ef9..1e1b46f 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -10,3 +10,4 @@ scipy-stubs discretize==0.12.0 simpeg==0.25.2 pymatsolver==0.4.0 +xxhash diff --git a/src/inversion_ideas/utils.py b/src/inversion_ideas/utils.py index 773ef01..9d798ec 100644 --- a/src/inversion_ideas/utils.py +++ b/src/inversion_ideas/utils.py @@ -3,7 +3,6 @@ """ import functools -import hashlib import logging import numpy as np @@ -12,6 +11,16 @@ from ._utils import array_to_str from .typing import SparseArray +try: + import xxhash +except ImportError: + import hashlib + + HASHING_FUNCTION = hashlib.sha256 +else: + HASHING_FUNCTION = xxhash.xxh32 + + __all__ = [ "cache_on_model", "get_logger", @@ -110,7 +119,7 @@ def wrapper(self, model, *args, **kwargs): raise AttributeError(msg) if self.cache: - model_hash = hashlib.sha256(model) + model_hash = HASHING_FUNCTION(model) # Return cached object if the model hash matches with the cached one if hasattr(self, cache_attr): @@ -136,7 +145,8 @@ def wrapper(self, model, *args, **kwargs): # -- Debug log -- msg = ( f"Computed new result '{array_to_str(result)}' after " - f"calling '{func}' with model with hash '{model_hash.hexdigest()}'. " + f"calling '{func}' with model with hash " + f"'{model_hash.name}:{model_hash.hexdigest()}'. " "Cached the result into the object." ) if args: