cache.rs


Overview

This file implements a specialized caching mechanism designed to optimize JSON deserialization performance by efficiently reusing Python string objects (`PyStr`) representing JSON keys. Given that JSON keys often repeat within objects or nested structures, this cache reduces overhead caused by frequent Python string allocation and reference counting.

The core of the file defines:

This caching layer is integral to the deserialization subsystem, providing a fast lookup and reuse mechanism for Python strings representing JSON keys, substantially improving throughput and memory efficiency.


Detailed Explanation of Entities

CachedKey Struct

#[repr(transparent)]
pub(crate) struct CachedKey {
    ptr: PyStr,
}

Purpose

`CachedKey` acts as a safe wrapper around a `PyStr` Python string pointer, managing its Python reference count explicitly to ensure that cached keys remain valid and properly owned.

Safety Traits

Methods

Drop Implementation

impl Drop for CachedKey {
    fn drop(&mut self) {
        ffi!(Py_DECREF(self.ptr.as_ptr().cast::<pyo3_ffi::PyObject>()));
    }
}

This ensures that cached Python strings are properly released when evicted from the cache or when the cache is destroyed.


KeyMap Type Alias

pub(crate) type KeyMap =
    AssociativeCache<u64, CachedKey, Capacity2048, HashDirectMapped, RoundRobinReplacement>;

Purpose

`KeyMap` is a fixed-capacity associative cache mapping 64-bit hash keys (`u64`) to `CachedKey` instances.

Implementation Details

This design offers a balance between lookup speed and memory footprint, suitable for high-frequency key reuse during deserialization.


KEY_MAP Static Instance

pub(crate) static mut KEY_MAP: OnceCell<KeyMap> = OnceCell::new();

Purpose

Usage

Before usage, `KEY_MAP` should be initialized exactly once. Subsequent accesses can retrieve the cache for lookups or insertions.

Example (hypothetical usage pattern):

unsafe {
    if KEY_MAP.get().is_none() {
        KEY_MAP.set(KeyMap::new());
    }
    let cache = KEY_MAP.get_mut().unwrap();
    // Use cache for lookups and insertions
}

Important Implementation Details & Algorithms


Interaction with Other Components


Usage Example

// Assuming a PyStr 'py_key' representing a Python string key

// Create a CachedKey wrapper
let mut cached_key = CachedKey::new(py_key);

// Retrieve the cached PyStr with incremented reference count
let pystr_for_use = cached_key.get();

// Use pystr_for_use safely in Python FFI calls or Rust-Python interop

// When cached_key goes out of scope, Py_DECREF is called automatically

Mermaid Class Diagram

classDiagram
    class CachedKey {
        -ptr: PyStr
        +new(ptr: PyStr) CachedKey
        +get() PyStr
        <<Drop>>
    }

    class AssociativeCache~K, V, Capacity, Hashing, Replacement~ {
        +insert(k: K, v: V)
        +get_mut(k: &K) Option<&mut V>
        +new() Self
    }

    CachedKey ..> PyStr : contains
    KeyMap "type alias" <|-- AssociativeCache~u64, CachedKey, Capacity2048, HashDirectMapped, RoundRobinReplacement~

Summary

This file is a critical performance optimization component for JSON deserialization in Python-Rust integration contexts.