1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
|
use std::any::{Any, TypeId};
use std::fmt;
use std::ops::IndexMut;
pub mod input_field;
pub mod setter;
pub mod singleton;
use input_field::FieldIngredientImpl;
use crate::cycle::CycleHeads;
use crate::function::VerifyResult;
use crate::id::{AsId, FromId, FromIdWithDb};
use crate::ingredient::Ingredient;
use crate::input::singleton::{Singleton, SingletonChoice};
use crate::key::DatabaseKeyIndex;
use crate::plumbing::Jar;
use crate::sync::Arc;
use crate::table::memo::{MemoTable, MemoTableTypes};
use crate::table::{Slot, Table};
use crate::zalsa::{IngredientIndex, Zalsa};
use crate::{Database, Durability, Id, Revision, Runtime};
pub trait Configuration: Any {
const DEBUG_NAME: &'static str;
const FIELD_DEBUG_NAMES: &'static [&'static str];
const LOCATION: crate::ingredient::Location;
/// The singleton state for this input if any.
type Singleton: SingletonChoice + Send + Sync;
/// The input struct (which wraps an `Id`)
type Struct: FromId + AsId + 'static + Send + Sync;
/// A (possibly empty) tuple of the fields for this struct.
type Fields: Send + Sync;
/// A array of [`Revision`], one per each of the value fields.
type Revisions: Send + Sync + fmt::Debug + IndexMut<usize, Output = Revision>;
/// A array of [`Durability`], one per each of the value fields.
type Durabilities: Send + Sync + fmt::Debug + IndexMut<usize, Output = Durability>;
}
pub struct JarImpl<C: Configuration> {
_phantom: std::marker::PhantomData<C>,
}
impl<C: Configuration> Default for JarImpl<C> {
fn default() -> Self {
Self {
_phantom: Default::default(),
}
}
}
impl<C: Configuration> Jar for JarImpl<C> {
fn create_ingredients(
_zalsa: &Zalsa,
struct_index: crate::zalsa::IngredientIndex,
_dependencies: crate::memo_ingredient_indices::IngredientIndices,
) -> Vec<Box<dyn Ingredient>> {
let struct_ingredient: IngredientImpl<C> = IngredientImpl::new(struct_index);
std::iter::once(Box::new(struct_ingredient) as _)
.chain((0..C::FIELD_DEBUG_NAMES.len()).map(|field_index| {
Box::new(<FieldIngredientImpl<C>>::new(struct_index, field_index)) as _
}))
.collect()
}
fn id_struct_type_id() -> TypeId {
TypeId::of::<C::Struct>()
}
}
pub struct IngredientImpl<C: Configuration> {
ingredient_index: IngredientIndex,
singleton: C::Singleton,
memo_table_types: Arc<MemoTableTypes>,
_phantom: std::marker::PhantomData<C::Struct>,
}
impl<C: Configuration> IngredientImpl<C> {
pub fn new(index: IngredientIndex) -> Self {
Self {
ingredient_index: index,
singleton: Default::default(),
memo_table_types: Arc::new(MemoTableTypes::default()),
_phantom: std::marker::PhantomData,
}
}
fn data(zalsa: &Zalsa, id: Id) -> &Value<C> {
zalsa.table().get(id)
}
fn data_raw(table: &Table, id: Id) -> *mut Value<C> {
table.get_raw(id)
}
pub fn database_key_index(&self, id: C::Struct) -> DatabaseKeyIndex {
DatabaseKeyIndex::new(self.ingredient_index, id.as_id())
}
pub fn new_input(
&self,
db: &dyn Database,
fields: C::Fields,
revisions: C::Revisions,
durabilities: C::Durabilities,
) -> C::Struct {
let (zalsa, zalsa_local) = db.zalsas();
let id = self.singleton.with_scope(|| {
zalsa_local.allocate(zalsa, self.ingredient_index, |_| Value::<C> {
fields,
revisions,
durabilities,
memos: Default::default(),
})
});
FromIdWithDb::from_id(id, zalsa)
}
/// Change the value of the field `field_index` to a new value.
///
/// # Parameters
///
/// * `runtime`, the salsa runtiem
/// * `id`, id of the input struct
/// * `field_index`, index of the field that will be changed
/// * `durability`, durability of the new value. If omitted, uses the durability of the previous value.
/// * `setter`, function that modifies the fields tuple; should only modify the element for `field_index`
pub fn set_field<R>(
&mut self,
runtime: &mut Runtime,
id: C::Struct,
field_index: usize,
durability: Option<Durability>,
setter: impl FnOnce(&mut C::Fields) -> R,
) -> R {
let id: Id = id.as_id();
let data_raw = Self::data_raw(runtime.table(), id);
// SAFETY: We hold `&mut` on the runtime so no `&`-references can be active.
// Also, we don't access any other data from the table while `r` is active.
let data = unsafe { &mut *data_raw };
data.revisions[field_index] = runtime.current_revision();
let field_durability = &mut data.durabilities[field_index];
if *field_durability != Durability::MIN {
runtime.report_tracked_write(*field_durability);
}
*field_durability = durability.unwrap_or(*field_durability);
setter(&mut data.fields)
}
/// Get the singleton input previously created (if any).
#[doc(hidden)]
pub fn get_singleton_input(&self, zalsa: &Zalsa) -> Option<C::Struct>
where
C: Configuration<Singleton = Singleton>,
{
self.singleton
.index()
.map(|id| FromIdWithDb::from_id(id, zalsa))
}
/// Access field of an input.
/// Note that this function returns the entire tuple of value fields.
/// The caller is responsible for selecting the appropriate element.
pub fn field<'db>(
&'db self,
db: &'db dyn crate::Database,
id: C::Struct,
field_index: usize,
) -> &'db C::Fields {
let (zalsa, zalsa_local) = db.zalsas();
let field_ingredient_index = self.ingredient_index.successor(field_index);
let id = id.as_id();
let value = Self::data(zalsa, id);
let durability = value.durabilities[field_index];
let revision = value.revisions[field_index];
zalsa_local.report_tracked_read_simple(
DatabaseKeyIndex::new(field_ingredient_index, id),
durability,
revision,
);
&value.fields
}
#[cfg(feature = "salsa_unstable")]
/// Returns all data corresponding to the input struct.
pub fn entries<'db>(
&'db self,
db: &'db dyn crate::Database,
) -> impl Iterator<Item = &'db Value<C>> {
db.zalsa().table().slots_of::<Value<C>>()
}
/// Peek at the field values without recording any read dependency.
/// Used for debug printouts.
pub fn leak_fields<'db>(&'db self, db: &'db dyn Database, id: C::Struct) -> &'db C::Fields {
let zalsa = db.zalsa();
let id = id.as_id();
let value = Self::data(zalsa, id);
&value.fields
}
}
impl<C: Configuration> Ingredient for IngredientImpl<C> {
fn location(&self) -> &'static crate::ingredient::Location {
&C::LOCATION
}
fn ingredient_index(&self) -> IngredientIndex {
self.ingredient_index
}
unsafe fn maybe_changed_after(
&self,
_db: &dyn Database,
_input: Id,
_revision: Revision,
_cycle_heads: &mut CycleHeads,
) -> VerifyResult {
// Input ingredients are just a counter, they store no data, they are immortal.
// Their *fields* are stored in function ingredients elsewhere.
VerifyResult::unchanged()
}
fn debug_name(&self) -> &'static str {
C::DEBUG_NAME
}
fn memo_table_types(&self) -> Arc<MemoTableTypes> {
self.memo_table_types.clone()
}
/// Returns memory usage information about any inputs.
#[cfg(feature = "salsa_unstable")]
fn memory_usage(&self, db: &dyn Database) -> Option<Vec<crate::database::SlotInfo>> {
let memory_usage = self
.entries(db)
// SAFETY: The memo table belongs to a value that we allocated, so it
// has the correct type.
.map(|value| unsafe { value.memory_usage(&self.memo_table_types) })
.collect();
Some(memory_usage)
}
}
impl<C: Configuration> std::fmt::Debug for IngredientImpl<C> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct(std::any::type_name::<Self>())
.field("index", &self.ingredient_index)
.finish()
}
}
#[derive(Debug)]
pub struct Value<C>
where
C: Configuration,
{
/// Fields of this input struct.
///
/// They can change across revisions, but they do not change within
/// a particular revision.
fields: C::Fields,
/// Revisions of the fields.
revisions: C::Revisions,
/// Durabilities of the fields.
durabilities: C::Durabilities,
/// Memos
memos: MemoTable,
}
impl<C> Value<C>
where
C: Configuration,
{
/// Fields of this tracked struct.
///
/// They can change across revisions, but they do not change within
/// a particular revision.
#[cfg(feature = "salsa_unstable")]
pub fn fields(&self) -> &C::Fields {
&self.fields
}
/// Returns memory usage information about the input.
///
/// # Safety
///
/// The `MemoTable` must belong to a `Value` of the correct type.
#[cfg(feature = "salsa_unstable")]
unsafe fn memory_usage(&self, memo_table_types: &MemoTableTypes) -> crate::database::SlotInfo {
// SAFETY: The caller guarantees this is the correct types table.
let memos = unsafe { memo_table_types.attach_memos(&self.memos) };
crate::database::SlotInfo {
debug_name: C::DEBUG_NAME,
size_of_metadata: std::mem::size_of::<Self>() - std::mem::size_of::<C::Fields>(),
size_of_fields: std::mem::size_of::<C::Fields>(),
memos: memos.memory_usage(),
}
}
}
pub trait HasBuilder {
type Builder;
}
// SAFETY: `Value<C>` is our private type branded over the unique configuration `C`.
unsafe impl<C> Slot for Value<C>
where
C: Configuration,
{
#[inline(always)]
unsafe fn memos(&self, _current_revision: Revision) -> &crate::table::memo::MemoTable {
&self.memos
}
#[inline(always)]
fn memos_mut(&mut self) -> &mut crate::table::memo::MemoTable {
&mut self.memos
}
}
|