Skip to main content

Performance

The measured numbers against python-diskcache 5.6.3, where the speed comes from, the known slow spots and what’s next.

A microsecond is nothing until you multiply it. Web apps call their cache on almost every request, often straight from the asyncio event loop, where every microsecond the cache spends is a microsecond every other request on that loop waits. So diskcache-oxide’s performance work starts with read latency, then throughput under many threads, then predictable resource use. Correctness always comes first: every speedup has to keep the conformance suite green, or it doesn’t ship. The numbers below are the ones that survived.

The numbers

Measured with bench/gate.py, which runs python-diskcache 5.6.3 (its last release, from August 2023; the project has had no commits since) and diskcache-oxide in the same session on the same machine, in four interleaved rounds. These numbers come from an 18-core Apple M5 Max on macOS 27.2, on AC power with nothing else heavy running, measuring the released 0.3.5 PGO wheel on 2026-11-16 (bench/results/macos/gate-3.14.txt and gate-3.14t.txt in the repository). Both libraries ran in the same session, so the ratios compare like with like; the absolute numbers on an x86-64 Linux server will differ. Latencies are p50 in microseconds. Ratios are oxide’s time divided by diskcache’s, so lower is better.

Workload 3.14 3.14t (free-threaded)
get, small value, hit 0.84 µs (0.47×) 0.86 µs (0.52×)
get, miss 0.73 µs (0.52×) 0.76 µs (0.56×)
get, small dict 1.16 µs (0.46×) 1.24 µs (0.52×)
get, random keys over a large database 2.51 µs (0.76×) 2.59 µs (0.82×)
set, small value 18.2 µs (0.75×) 18.3 µs (0.76×)

Threaded throughput, every thread sharing one Cache, in operations per second at 1/2/4/8 threads (ratios are oxide’s throughput over diskcache’s, so higher is better):

Mix 3.14 3.14t (free-threaded)
99:1 reads to writes 718k / 715k / 711k / 709k (1.7-37.9×) 710k / 802k / 981k / 766k (1.6-2.2×)
1,000:1 reads to writes 820k / 812k / 810k / 805k (1.7-41.2×) 803k / 1,086k / 1,313k / 932k (1.6-2.6×)

Two things stand out:

  • On GIL builds, diskcache collapses as threads are added and oxide doesn’t. At 99:1, diskcache drops from about 433k to 19k operations per second going from 1 to 8 threads; oxide stays flat around 710-720k. diskcache’s per-thread connections end up fighting over the GIL and SQLite’s locks.
  • On free-threaded builds, oxide is faster at every thread count, but it doesn’t scale yet. At 1,000:1 throughput peaks at about 1.3M operations per second at 4 threads, 1.6× one thread, and falls back to about 0.9M at 8. That’s what the free-threaded work below is for.

Run them yourself with python -m bench.gate. Numbers vary by machine; the ratios are what the gate enforces.

Where the speed comes from

  • Rust from the call to the disk. A get goes from Python straight into compiled Rust: one prepared SQLite statement, decode, done. There’s no Python wrapper in between.
  • Statements prepared once. Each pooled connection caches its prepared statements, so a lookup never recompiles SQL.
  • The clock is read in Rust. Checking expiry doesn’t call back into Python’s time.time().
  • Fast codecs. msgpack and tagged JSON are encoded and decoded in Rust, building Python objects directly.
  • Reads never write. Least-recently-stored eviction means a get never updates anything, so any number of readers proceed in parallel.
  • A smarter write lock. Writers in one process queue on an in-process lock per database file, instead of every thread retrying against SQLite’s busy handler.
  • Maintenance off the request path. Background maintenance never waits for a lock and holds the write lock only for short chunks: a request in the same process waits at most one chunk (about 130 µs for 100 small rows); a writer in another process can wait for a few chunks, never past the turn’s 50 ms.
  • SQLite tuned for it. The bundled SQLite is built with its global memory statistics turned off, which removes a process-wide mutex from every allocation: 2-10% more threaded throughput.

Prefix scans at scale

diskcache’s iterkeys() gets slower faster than the cache grows: about 1.7 seconds at 100,000 keys, and over 25 seconds at 400,000. diskcache-oxide’s iterkeys(prefix=...) is an indexed range scan, paged in batches so no read transaction is held between them. 400,000 keys scan in about 10 ms.

Memory that stays flat

Memory is bounded by configuration, not by how many threads touch the cache: at most max_connections connections per database file (default 8), each with a 1 MiB page cache. A FanoutCache defaults to two per shard for the whole root, about 16 MiB at 8 shards. A separate memory-instrumented build runs bench/memory.py and the memory tests under heavy load to hold that bound. See Storage and WAL.

Known slow spots

Two oversubscribed cases measure slower than before the SQLite mutex spin and the connection-slot spin went in. Both are known, and neither is in the path of a web app running a few threads per process.

  • A plain Cache with far more threads than cores, all sharing one handle: 7% slower on an M-series Mac at 36 threads, and 5% in a Linux VM at 32. Faster hand-off lets more threads into SQLite at once.
  • A FanoutCache at max_connections=32 with 8 threads, on macOS only: 5-12% slower. The same case on Linux is 41-56% faster.

Next: free-threaded scaling

Profiling a cached get shows that about 60% of its time is one system call: SQLite taking its shared-memory read lock on the -shm file, under a process-wide mutex. It’s the biggest single cost on every read, and it’s also what keeps free-threaded throughput from scaling. The free-threaded phase, next on the roadmap, goes after it, along with:

  • a per-process lock-caching layer under SQLite, or longer-lived read snapshots;
  • thread-affine connections;
  • cheaper reference counting on shared Cache objects in free-threaded builds;
  • making threads that share one handle scale. FanoutCache is built: it removes the multi-process ceiling (a plain Cache falls past two processes even when they only read: the operating system serializes this same lock when several processes take it on one file, measured in bench/results/read-sentinel-2026-11-24.md) and lifts this read ceiling part of the way (1.7x a plain Cache at 8 threads on 3.14t with hot keys, about 10% on GIL builds), and the shared per-handle path is still being worked on.

Past that, the network server can go further still: as the only owner of its files, it can use SQLite’s exclusive locking mode, which keeps that lock in ordinary memory. In a 2026-10-28 measurement with Python’s own sqlite3 module, reads dropped from 1.18 µs to 0.69 µs (a different harness from the gate above, so compare the two figures with each other, not with the gate’s 0.84 µs).