euv-core 0.53.1

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
use super::*;

/// Generation-stamped handle preventing ABA aliasing after asset eviction or release.
#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, New, Ord, PartialEq, PartialOrd)]
pub struct AssetHandle {
    /// Zero-based slot index inside `AssetManager`.
    #[get(type(copy))]
    pub(crate) slot_id: u32,
    /// Monotonic slot generation stamped when the handle was issued.
    #[get(type(copy))]
    pub(crate) generation: u32,
}

/// Explicit RAM and VRAM memory ceiling configuration for `AssetManager`.
#[derive(Clone, Copy, Data, Debug, Eq, New, PartialEq)]
pub struct AssetBudgetConfig {
    /// Maximum resident CPU RAM bytes permitted before LRU eviction triggers.
    #[get(type(copy))]
    pub(crate) ram_budget_bytes: usize,
    /// Maximum resident GPU VRAM bytes permitted before LRU eviction triggers.
    #[get(type(copy))]
    pub(crate) vram_budget_bytes: usize,
}

/// Telemetry snapshot summarizing residency, budgets, and cache efficiency for `AssetManager`.
#[derive(Clone, Copy, Data, Debug, Default, Eq, PartialEq)]
pub struct AssetManagerStats {
    /// Count of resident `Ready` assets.
    #[get(type(copy))]
    pub(crate) live_assets: usize,
    /// Count of in-flight cancelable `Pending` loads.
    #[get(type(copy))]
    pub(crate) pending_assets: usize,
    /// Currently resident CPU RAM bytes across live assets.
    #[get(type(copy))]
    pub(crate) ram_used_bytes: usize,
    /// Configured CPU RAM byte ceiling.
    #[get(type(copy))]
    pub(crate) ram_budget_bytes: usize,
    /// Currently resident GPU VRAM bytes across live assets.
    #[get(type(copy))]
    pub(crate) vram_used_bytes: usize,
    /// Configured GPU VRAM byte ceiling.
    #[get(type(copy))]
    pub(crate) vram_budget_bytes: usize,
    /// Cumulative cache lookup hits.
    #[get(type(copy))]
    pub(crate) hits: u64,
    /// Cumulative cache lookup misses.
    #[get(type(copy))]
    pub(crate) misses: u64,
    /// Cumulative deterministic LRU evictions performed.
    #[get(type(copy))]
    pub(crate) evictions: u64,
    /// Cumulative in-flight load cancellations completed.
    #[get(type(copy))]
    pub(crate) cancellations: u64,
}

/// Internal per-slot record tracking ownership, bytes, LRU recency, and `ResourceAccountant` handles.
#[derive(Clone, Data, Debug)]
pub(crate) struct AssetSlotEntry {
    /// Generation-stamped handle identifying this live slot.
    #[get(type(copy))]
    pub(crate) handle: AssetHandle,
    /// Logical lookup key (URI, asset path, or content hash).
    pub(crate) key: String,
    /// Single authoritative resource owner recorded in `ResourceAccountant`.
    #[get(type(copy))]
    pub(crate) owner: ResourceOwnerId,
    /// Memory residency domain (`Ram`, `Vram`, or `Hybrid`).
    #[get(type(copy))]
    pub(crate) storage: AssetStorageKind,
    /// Current load lifecycle state (`Pending` or `Ready`).
    #[get(type(copy))]
    pub(crate) state: AssetLoadState,
    /// Resident CPU RAM bytes charged to this entry.
    #[get(type(copy))]
    pub(crate) ram_bytes: usize,
    /// Resident GPU VRAM bytes charged to this entry.
    #[get(type(copy))]
    pub(crate) vram_bytes: usize,
    /// Monotonic access tick used for deterministic LRU eviction ordering.
    #[get(type(copy))]
    pub(crate) last_access_tick: u64,
    /// When `true`, entry is protected from automatic LRU eviction until unpinned or released.
    #[get(type(copy))]
    pub(crate) pinned: bool,
    /// Decoded CPU payload bytes (empty for pure `Vram` or `Pending` entries).
    pub(crate) payload: Vec<u8>,
    /// `ResourceAccountant` handle for in-flight `AsyncTask` tracking (`0` when none).
    #[get(type(copy))]
    pub(crate) task_account_handle: u64,
    /// `ResourceAccountant` handle for CPU `CacheAllocation` tracking (`0` when none).
    #[get(type(copy))]
    pub(crate) ram_account_handle: u64,
    /// `ResourceAccountant` handle for GPU `GpuTexture` tracking (`0` when none).
    #[get(type(copy))]
    pub(crate) vram_account_handle: u64,
}

/// Cancelable budgeted LRU asset cache wired directly to `ResourceAccountant` for leak attribution.
#[derive(Clone, Data, Debug)]
pub struct AssetManager {
    /// Active RAM and VRAM byte ceiling configuration.
    #[get(type(copy))]
    pub(crate) budget: AssetBudgetConfig,
    /// Slot table storing active or pending asset entries.
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(skip)]
    pub(crate) entries: Vec<Option<AssetSlotEntry>>,
    /// Per-slot monotonic generation counters preventing ABA handle reuse.
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(skip)]
    pub(crate) generations: Vec<u32>,
    /// LIFO free-list of reusable slot indices.
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(skip)]
    pub(crate) free_slots: Vec<u32>,
    /// Fast string-key to live `AssetHandle` index.
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(skip)]
    pub(crate) key_index: HashMap<String, AssetHandle>,
    /// Current aggregate resident CPU RAM bytes.
    #[get(type(copy))]
    pub(crate) ram_used_bytes: usize,
    /// Current aggregate resident GPU VRAM bytes.
    #[get(type(copy))]
    pub(crate) vram_used_bytes: usize,
    /// Monotonic logical clock stamped on asset insertion and hits.
    #[get(pub(crate), type(copy))]
    #[get_mut(skip)]
    #[set(pub(crate))]
    pub(crate) access_clock: u64,
    /// Cumulative lookup hits.
    #[get(type(copy))]
    pub(crate) hits: u64,
    /// Cumulative lookup misses.
    #[get(type(copy))]
    pub(crate) misses: u64,
    /// Cumulative LRU evictions.
    #[get(type(copy))]
    pub(crate) evictions: u64,
    /// Cumulative pending load cancellations.
    #[get(type(copy))]
    pub(crate) cancellations: u64,
}