salsa/src/durability.rs
Ibraheem Ahmed a86db59e99
Enable Garbage Collection for Interned Values (#602)
* remove table-wide dependencies

* add plumbing to reuse interned slots

* record durabilities on interned values

* appease clippy

* remove immortal interned value logic

* pass correct revision when tracking interned reads

* force new revision when resetting interned values

* avoid unnecessary calls to `Database::zalsa`

* add log events for value internment

* Only log event kind because thread id can differ between runs/computers

* cargo fmt

---------

Co-authored-by: Micha Reiser <micha@reiser.io>
2025-03-17 09:23:14 +00:00

96 lines
3.1 KiB
Rust

/// Describes how likely a value is to change—how "durable" it is.
///
/// By default, inputs have `Durability::LOW` and interned values have
/// `Durability::HIGH`. But inputs can be explicitly set with other
/// durabilities.
///
/// We use durabilities to optimize the work of "revalidating" a query
/// after some input has changed. Ordinarily, in a new revision,
/// queries have to trace all their inputs back to the base inputs to
/// determine if any of those inputs have changed. But if we know that
/// the only changes were to inputs of low durability (the common
/// case), and we know that the query only used inputs of medium
/// durability or higher, then we can skip that enumeration.
///
/// Typically, one assigns low durabilities to inputs that the user is
/// frequently editing. Medium or high durabilities are used for
/// configuration, the source from library crates, or other things
/// that are unlikely to be edited.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Durability(DurabilityVal);
impl std::fmt::Debug for Durability {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("Durability")
.field(&(self.0 as usize))
.finish()
}
}
// We use an enum here instead of a u8 for niches.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
enum DurabilityVal {
Low = 0,
Medium = 1,
High = 2,
}
impl From<u8> for DurabilityVal {
fn from(value: u8) -> Self {
match value {
0 => DurabilityVal::Low,
1 => DurabilityVal::Medium,
2 => DurabilityVal::High,
_ => panic!("invalid durability"),
}
}
}
impl Durability {
/// Low durability: things that change frequently.
///
/// Example: part of the crate being edited
pub const LOW: Durability = Durability(DurabilityVal::Low);
/// Medium durability: things that change sometimes, but rarely.
///
/// Example: a Cargo.toml file
pub const MEDIUM: Durability = Durability(DurabilityVal::Medium);
/// High durability: things that are not expected to change under
/// common usage.
///
/// Example: the standard library or something from crates.io
pub const HIGH: Durability = Durability(DurabilityVal::High);
/// The minimum possible durability; equivalent to LOW but
/// "conceptually" distinct (i.e., if we add more durability
/// levels, this could change).
pub(crate) const MIN: Durability = Self::LOW;
/// The maximum possible durability; equivalent to HIGH but
/// "conceptually" distinct (i.e., if we add more durability
/// levels, this could change).
pub(crate) const MAX: Durability = Self::HIGH;
/// Number of durability levels.
pub(crate) const LEN: usize = Self::HIGH.0 as usize + 1;
pub(crate) fn index(self) -> usize {
self.0 as usize
}
pub(crate) fn as_u8(self) -> u8 {
self.0 as u8
}
pub(crate) fn from_u8(value: u8) -> Self {
Self(DurabilityVal::from(value))
}
}
impl Default for Durability {
fn default() -> Self {
Durability::LOW
}
}