trimesh.caching
caching.py
Functions and classes that help with tracking changes in numpy.ndarray and clearing cached values based on those changes.
You should really pip install xxhash:
``` In [23]: %timeit int(blake2b(d).hexdigest(), 16) 102 us +/- 684 ns per loop
In [24]: %timeit int(sha256(d).hexdigest(), 16) 142 us +/- 3.73 us
In [25]: %timeit xxh3_64_intdigest(d) 3.37 us +/- 116 ns per loop ```
- class trimesh.caching.Cache(id_function, force_immutable=False)
Bases:
object
Class to cache values which will be stored until the result of an ID function changes.
- __init__(id_function, force_immutable=False)
Create a cache object.
- Parameters
id_function (function) – Returns hashable value
force_immutable (bool) – If set will make all numpy arrays read-only
- clear(exclude=None)
Remove elements in the cache.
- Parameters
exclude (list) – List of keys in cache to not clear.
- delete(key)
Remove a key from the cache.
- id_set()
Set the current ID to the value of the ID function.
- update(items)
Update the cache with a set of key, value pairs without checking id_function.
- verify()
Verify that the cached values are still for the same value of id_function and delete all stored items if the value of id_function has changed.
- class trimesh.caching.DataStore
Bases:
Mapping
A class to store multiple numpy arrays and track them all for changes.
Operates like a dict that only stores numpy.ndarray
- __init__()
- clear()
Remove all data from the DataStore.
- crc()
Get a CRC reflecting everything in the DataStore.
- Returns
crc – CRC of data
- Return type
int
- fast_hash()
Get a CRC32 or xxhash.xxh64 reflecting the DataStore.
- Returns
hashed – Checksum of data
- Return type
int
- hash()
- is_empty()
Is the current DataStore empty or not.
- Returns
empty – False if there are items in the DataStore
- Return type
bool
- property mutable
Is data allowed to be altered or not.
- Returns
is_mutable – Can data be altered in the DataStore
- Return type
bool
- pop(key)
- update(values)
- class trimesh.caching.DiskCache(path, expire_days=30)
Bases:
object
Store results of expensive operations on disk with an option to expire the results. This is used to cache the multi-gigabyte test corpuses in tests/corpus.py
- __init__(path, expire_days=30)
Create a cache on disk for storing expensive results.
- Parameters
path (str) – A writeable location on the current file path.
expire_days (int or float) – How old should results be considered expired.
- get(key, fetch)
Get a key from the cache or run a calculation.
- Parameters
key (str) – Key to reference item with
fetch (function) – If key isn’t stored and recent run this function and store its result on disk.
- class trimesh.caching.TrackedArray
Bases:
ndarray
Subclass of numpy.ndarray that provides hash methods to track changes.
General method is to aggressively set ‘modified’ flags on operations which might (but don’t necessarily) alter the array, ideally we sometimes compute hashes when we don’t need to, but we don’t return wrong hashes ever.
We store boolean modified flag for each hash type to make checks fast even for queries of different hashes.
- __hash__ : int
- Runs the fastest available hash in this order:
xxh3_64, xxh_64, blake2b, sha256
- crc()
- hash()
- md5()
- property mutable
- trimesh.caching.cache_decorator(function)
A decorator for class methods, replaces @property but will store and retrieve function return values in object cache.
- trimesh.caching.hash_fallback(item)
- trimesh.caching.sha256(item)
- trimesh.caching.tracked_array(array, dtype=None)
Properly subclass a numpy ndarray to track changes.
Avoids some pitfalls of subclassing by forcing contiguous arrays and does a view into a TrackedArray.
- Parameters
array (array- like object) – To be turned into a TrackedArray
dtype (np.dtype) – Which dtype to use for the array
- Returns
tracked – Contains input array data.
- Return type