Skip to main content

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 using functools.cache

  • Large inputs (> cutoff) are either:

    • not cached (default when lru_size is None/0), or
    • cached with a bounded LRU using functools.lru_cache(maxsize=...)

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 where cutoff_value(*args, **kwargs) <= cutoff. Must be > 0.

  • lru_size (optional): LRU size for calls above the cutoff.

    • None or 0 ⇒ no cache above cutoff
    • > 0functools.lru_cache(maxsize=lru_size) above cutoff
  • cutoff_value (optional): function that chooses which numeric value to compare against cutoff. Defaults to default_cutoff_value.

Cache controls

split_cache exposes a few helpers on the wrapped function:

  • fn.small_cache_clear(): clear the unbounded “small” cache
  • fn.big_cache_clear(): clear the “big” LRU cache if enabled
  • fn.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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

splitcache-0.0.1.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

splitcache-0.0.1-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

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

Hashes for splitcache-0.0.1.tar.gz
Algorithm Hash digest
SHA256 6f8efb9d8588e864c86cfa338c62c3cf1865a0cabcf711eec21b0f7bfb10f30c
MD5 203bb1e1b9e4a40440ff13cc53f09d9f
BLAKE2b-256 767e41ff134f9219b5fceab29c6b105941e83aebe878fb0bc17fdb264de8645b

See more details on using hashes here.

Provenance

The following attestation bundles were made for splitcache-0.0.1.tar.gz:

Publisher: ci.yml on rheard/splitcache

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for splitcache-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8d552b13005fd774ebe3a223eb9cbe893c2d94a8855ef8e965c4416b1d1532a9
MD5 e784bc10e60a11cdbe8b93aeb3c7db8e
BLAKE2b-256 0066002cdf8f19bf998932ff3191e45a061501f5183d26fad24fde1b08e53935

See more details on using hashes here.

Provenance

The following attestation bundles were made for splitcache-0.0.1-py3-none-any.whl:

Publisher: ci.yml on rheard/splitcache

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page