A type of split cache for number computation problems, caching low numbers and optionally some higher ones.
Project description
splitcache
A tiny “two-tier” caching decorator for integer-keyed functions:
-
Small inputs (≤
cutoff) are cached entirely usingfunctools.cache -
Large inputs (>
cutoff) are either:- not cached (default when
lru_sizeisNone/0), or - cached with a bounded LRU using
functools.lru_cache(maxsize=...)
- not cached (default when
This is useful when you have hot paths that repeatedly hit a dense “small” region, but also wander into huge integers (e.g., > $10^{20}$) where unbounded caching would explode memory.
Installation
pip install splitcache
Quick start
from splitcache import split_cache
@split_cache(cutoff=10**9, lru_size=10**6)
def nextprime(p: int) -> int:
...
Behavior:
- If
p <= 10**9: cached completely - If
p > 10**9: cached in an LRU of size $10^6$
To disable caching above the cutoff:
@split_cache(cutoff=10**9, lru_size=None)
def nextprime(p: int) -> int:
...
When lru_size is None (or 0), values above the cutoff are recomputed every time.
Note about memory size
For a method with a signature (int, ) -> int with numbers below $10^{45}$ or so, each cache entry
below the cutoff is measured at approximately 130 bytes,
and above the cutoff each cache entry is measured at approximately 190 bytes.
If any number below $10^9$ is provided to this method, that would be a cache size of over 120GB in memory. If restricted to just primes: there are approximately 48 million primes below $10^9$, which would be a cache size of nearly 6GB. The primes can be further restricted, eg primes $\equiv 1 \bmod 4$.
The point of all this is to express the importance of being aware of the potential size of the caches you are creating.
API
split_cache
-
cutoff(required): cache all calls wherecutoff_value(*args, **kwargs) <= cutoff. Must be> 0. -
lru_size(optional): LRU size for calls above the cutoff.Noneor0⇒ no cache above cutoff> 0⇒functools.lru_cache(maxsize=lru_size)above cutoff
-
cutoff_value(optional): function that chooses which numeric value to compare againstcutoff. Defaults todefault_cutoff_value.
Cache controls
split_cache exposes a few helpers on the wrapped function:
fn.small_cache_clear(): clear the unbounded “small” cachefn.big_cache_clear(): clear the “big” LRU cache if enabledfn.small_cache_info/fn.big_cache_info: cache info functions when available
Example:
next_prime_1mod4.small_cache_clear()
next_prime_1mod4.big_cache_clear()
# If big cache is enabled:
info = next_prime_1mod4.big_cache_info()
print(info)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file splitcache-0.0.1.tar.gz.
File metadata
- Download URL: splitcache-0.0.1.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6f8efb9d8588e864c86cfa338c62c3cf1865a0cabcf711eec21b0f7bfb10f30c
|
|
| MD5 |
203bb1e1b9e4a40440ff13cc53f09d9f
|
|
| BLAKE2b-256 |
767e41ff134f9219b5fceab29c6b105941e83aebe878fb0bc17fdb264de8645b
|
Provenance
The following attestation bundles were made for splitcache-0.0.1.tar.gz:
Publisher:
ci.yml on rheard/splitcache
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
splitcache-0.0.1.tar.gz -
Subject digest:
6f8efb9d8588e864c86cfa338c62c3cf1865a0cabcf711eec21b0f7bfb10f30c - Sigstore transparency entry: 929174812
- Sigstore integration time:
-
Permalink:
rheard/splitcache@6346caf71494e8848271db3def9a2f17f176fbad -
Branch / Tag:
refs/heads/main - Owner: https://github.com/rheard
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@6346caf71494e8848271db3def9a2f17f176fbad -
Trigger Event:
push
-
Statement type:
File details
Details for the file splitcache-0.0.1-py3-none-any.whl.
File metadata
- Download URL: splitcache-0.0.1-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8d552b13005fd774ebe3a223eb9cbe893c2d94a8855ef8e965c4416b1d1532a9
|
|
| MD5 |
e784bc10e60a11cdbe8b93aeb3c7db8e
|
|
| BLAKE2b-256 |
0066002cdf8f19bf998932ff3191e45a061501f5183d26fad24fde1b08e53935
|
Provenance
The following attestation bundles were made for splitcache-0.0.1-py3-none-any.whl:
Publisher:
ci.yml on rheard/splitcache
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
splitcache-0.0.1-py3-none-any.whl -
Subject digest:
8d552b13005fd774ebe3a223eb9cbe893c2d94a8855ef8e965c4416b1d1532a9 - Sigstore transparency entry: 929174820
- Sigstore integration time:
-
Permalink:
rheard/splitcache@6346caf71494e8848271db3def9a2f17f176fbad -
Branch / Tag:
refs/heads/main - Owner: https://github.com/rheard
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@6346caf71494e8848271db3def9a2f17f176fbad -
Trigger Event:
push
-
Statement type: