reorganize plumbing

This commit is contained in:
Niko Matsakis 2024-07-12 07:07:11 -04:00
parent 65118a0fe6
commit 15106ff8ea
20 changed files with 145 additions and 101 deletions

View file

@ -84,21 +84,18 @@ impl DbMacro {
fn has_storage_impl(&self, input: &syn::ItemStruct) -> syn::Result<TokenStream> {
let storage = self.find_storage_field(input)?;
let db = &input.ident;
let SalsaHasStorage = self.hygiene.ident("SalsaHasStorage");
let SalsaStorage = self.hygiene.ident("SalsaStorage");
let zalsa = self.hygiene.ident("zalsa");
Ok(quote! {
const _: () = {
use salsa::storage::HasStorage as #SalsaHasStorage;
use salsa::storage::Storage as #SalsaStorage;
use salsa::plumbing as #zalsa;
unsafe impl #SalsaHasStorage for #db {
fn storage(&self) -> &#SalsaStorage<Self> {
unsafe impl #zalsa::HasStorage for #db {
fn storage(&self) -> &#zalsa::Storage<Self> {
&self.#storage
}
fn storage_mut(&mut self) -> &mut #SalsaStorage<Self> {
fn storage_mut(&mut self) -> &mut #zalsa::Storage<Self> {
&mut self.#storage
}
}
@ -115,17 +112,21 @@ impl DbMacro {
}
fn add_salsa_view_method_impl(&self, input: &mut syn::ItemImpl) -> syn::Result<()> {
let zalsa = self.hygiene.ident("zalsa");
let Some((_, TraitPath, _)) = &input.trait_ else {
return Err(syn::Error::new_spanned(
&input.self_ty,
"impl must be on a trait",
));
};
input.items.push(parse_quote! {
#[doc(hidden)]
#[allow(uncommon_codepoins)]
fn zalsa_add_view(&self) {
salsa::storage::views(self).add::<Self, dyn #TraitPath>(|t| t, |t| t);
use salsa::plumbing as #zalsa;
#zalsa::views(self).add::<Self, dyn #TraitPath>(|t| t, |t| t);
}
});
Ok(())

View file

@ -82,7 +82,7 @@ pub trait Configuration: 'static {
/// * the `specify` method, which can only be used when the key is an entity created by the active query.
/// It sets the value of the function imperatively, so that when later fetches occur, they'll return this value.
/// * the `store` method, which can only be invoked with an `&mut` reference, and is to set input fields.
pub struct FunctionIngredient<C: Configuration> {
pub struct IngredientImpl<C: Configuration> {
/// The ingredient index we were assigned in the database.
/// Used to construct `DatabaseKeyIndex` values.
index: IngredientIndex,
@ -127,7 +127,7 @@ pub fn should_backdate_value<V: Eq>(old_value: &V, new_value: &V) -> bool {
/// e.g. you can do `Config<X, 0>` and `Config<X, 1>`.
pub struct Config<const C: usize>(std::marker::PhantomData<[(); C]>);
impl<C> FunctionIngredient<C>
impl<C> IngredientImpl<C>
where
C: Configuration,
{
@ -199,7 +199,7 @@ where
}
}
impl<C> Ingredient for FunctionIngredient<C>
impl<C> Ingredient for IngredientImpl<C>
where
C: Configuration,
{
@ -276,7 +276,7 @@ where
}
}
impl<C> std::fmt::Debug for FunctionIngredient<C>
impl<C> std::fmt::Debug for IngredientImpl<C>
where
C: Configuration,
{
@ -287,7 +287,7 @@ where
}
}
impl<C> IngredientRequiresReset for FunctionIngredient<C>
impl<C> IngredientRequiresReset for IngredientImpl<C>
where
C: Configuration,
{

View file

@ -3,10 +3,10 @@ use crate::{
storage::DatabaseGen, DatabaseKeyIndex, Id,
};
use super::{Configuration, FunctionIngredient};
use super::{Configuration, IngredientImpl};
use crate::accumulator::Accumulator;
impl<C> FunctionIngredient<C>
impl<C> IngredientImpl<C>
where
C: Configuration,
{

View file

@ -1,8 +1,8 @@
use crate::runtime::local_state::QueryRevisions;
use super::{memo::Memo, Configuration, FunctionIngredient};
use super::{memo::Memo, Configuration, IngredientImpl};
impl<C> FunctionIngredient<C>
impl<C> IngredientImpl<C>
where
C: Configuration,
{

View file

@ -3,9 +3,9 @@ use crossbeam::queue::SegQueue;
use crate::{runtime::local_state::QueryOrigin, Id};
use super::{memo, Configuration, FunctionIngredient};
use super::{memo, Configuration, IngredientImpl};
impl<C> FunctionIngredient<C>
impl<C> IngredientImpl<C>
where
C: Configuration,
{

View file

@ -3,9 +3,9 @@ use crate::{
storage::DatabaseGen, Database, DatabaseKeyIndex, Event, EventKind,
};
use super::{memo::Memo, Configuration, FunctionIngredient};
use super::{memo::Memo, Configuration, IngredientImpl};
impl<C> FunctionIngredient<C>
impl<C> IngredientImpl<C>
where
C: Configuration,
{

View file

@ -6,9 +6,9 @@ use crate::{
Cycle, Database, Event, EventKind,
};
use super::{memo::Memo, Configuration, FunctionIngredient};
use super::{memo::Memo, Configuration, IngredientImpl};
impl<C> FunctionIngredient<C>
impl<C> IngredientImpl<C>
where
C: Configuration,
{

View file

@ -2,9 +2,9 @@ use arc_swap::Guard;
use crate::{runtime::StampedValue, storage::DatabaseGen, Id};
use super::{Configuration, FunctionIngredient};
use super::{Configuration, IngredientImpl};
impl<C> FunctionIngredient<C>
impl<C> IngredientImpl<C>
where
C: Configuration,
{

View file

@ -1,8 +1,8 @@
use crate::{runtime::local_state::QueryOrigin, Id};
use super::{Configuration, FunctionIngredient};
use super::{Configuration, IngredientImpl};
impl<C> FunctionIngredient<C>
impl<C> IngredientImpl<C>
where
C: Configuration,
{

View file

@ -10,9 +10,9 @@ use crate::{
Id, Revision, Runtime,
};
use super::{memo::Memo, Configuration, FunctionIngredient};
use super::{memo::Memo, Configuration, IngredientImpl};
impl<C> FunctionIngredient<C>
impl<C> IngredientImpl<C>
where
C: Configuration,
{

View file

@ -7,9 +7,9 @@ use crate::{
Database, DatabaseKeyIndex, Id,
};
use super::{memo::Memo, Configuration, FunctionIngredient};
use super::{memo::Memo, Configuration, IngredientImpl};
impl<C> FunctionIngredient<C>
impl<C> IngredientImpl<C>
where
C: Configuration,
{

View file

@ -8,9 +8,9 @@ use crate::{
Id, Runtime,
};
use super::{memo::Memo, Configuration, FunctionIngredient};
use super::{memo::Memo, Configuration, IngredientImpl};
impl<C> FunctionIngredient<C>
impl<C> IngredientImpl<C>
where
C: Configuration,
{

View file

@ -18,14 +18,14 @@ pub trait Configuration: Any {
type Id: FromId + 'static + Send + Sync;
}
pub struct InputIngredient<C: Configuration> {
pub struct IngredientImpl<C: Configuration> {
ingredient_index: IngredientIndex,
counter: AtomicU32,
debug_name: &'static str,
_phantom: std::marker::PhantomData<C::Id>,
}
impl<C: Configuration> InputIngredient<C> {
impl<C: Configuration> IngredientImpl<C> {
pub fn new(index: IngredientIndex, debug_name: &'static str) -> Self {
Self {
ingredient_index: index,
@ -62,7 +62,7 @@ impl<C: Configuration> InputIngredient<C> {
}
}
impl<C: Configuration> Ingredient for InputIngredient<C> {
impl<C: Configuration> Ingredient for IngredientImpl<C> {
fn ingredient_index(&self) -> IngredientIndex {
self.ingredient_index
}
@ -125,11 +125,11 @@ impl<C: Configuration> Ingredient for InputIngredient<C> {
}
}
impl<C: Configuration> IngredientRequiresReset for InputIngredient<C> {
impl<C: Configuration> IngredientRequiresReset for IngredientImpl<C> {
const RESET_ON_NEW_REVISION: bool = false;
}
impl<C: Configuration> std::fmt::Debug for InputIngredient<C> {
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)

View file

@ -2,7 +2,6 @@ use crate::cycle::CycleRecoveryStrategy;
use crate::id::{AsId, FromId};
use crate::ingredient::{fmt_index, Ingredient, IngredientRequiresReset};
use crate::input::Configuration;
use crate::plumbing::transmute_lifetime;
use crate::runtime::local_state::QueryOrigin;
use crate::runtime::StampedValue;
use crate::storage::IngredientIndex;
@ -23,13 +22,13 @@ impl<T: Send + Sync + 'static> InputFieldData for T {}
/// a shared reference, so some locking is required.
/// Altogether this makes the implementation somewhat simpler than tracked
/// structs.
pub struct InputFieldIngredient<C: Configuration, F: InputFieldData> {
pub struct FieldIngredientImpl<C: Configuration, F: InputFieldData> {
index: IngredientIndex,
map: DashMap<C::Id, Box<StampedValue<F>>>,
debug_name: &'static str,
}
impl<C, F> InputFieldIngredient<C, F>
impl<C, F> FieldIngredientImpl<C, F>
where
C: Configuration,
F: InputFieldData,
@ -110,7 +109,16 @@ where
}
}
impl<C, F> Ingredient for InputFieldIngredient<C, F>
/// More limited wrapper around transmute that copies lifetime from `a` to `b`.
///
/// # Safety condition
///
/// `b` must be owned by `a`
unsafe fn transmute_lifetime<'a, 'b, A, B>(_a: &'a A, b: &'b B) -> &'a B {
std::mem::transmute(b)
}
impl<C, F> Ingredient for FieldIngredientImpl<C, F>
where
C: Configuration,
F: InputFieldData,
@ -166,7 +174,7 @@ where
}
}
impl<C, F> IngredientRequiresReset for InputFieldIngredient<C, F>
impl<C, F> IngredientRequiresReset for FieldIngredientImpl<C, F>
where
C: Configuration,
F: InputFieldData,
@ -174,7 +182,7 @@ where
const RESET_ON_NEW_REVISION: bool = false;
}
impl<C, F> std::fmt::Debug for InputFieldIngredient<C, F>
impl<C, F> std::fmt::Debug for FieldIngredientImpl<C, F>
where
C: Configuration,
F: InputFieldData,

View file

@ -53,7 +53,7 @@ impl<T: Eq + Hash + Clone> InternedData for T {}
/// The interned ingredient has the job of hashing values of type `Data` to produce an `Id`.
/// It used to store interned structs but also to store the id fields of a tracked struct.
/// Interned values endure until they are explicitly removed in some way.
pub struct InternedIngredient<C: Configuration> {
pub struct IngredientImpl<C: Configuration> {
/// Index of this ingredient in the database (used to construct database-ids, etc).
ingredient_index: IngredientIndex,
@ -86,7 +86,7 @@ where
fields: C::Data<'static>,
}
impl<C> InternedIngredient<C>
impl<C> IngredientImpl<C>
where
C: Configuration,
{
@ -182,7 +182,7 @@ where
}
}
impl<C> Ingredient for InternedIngredient<C>
impl<C> Ingredient for IngredientImpl<C>
where
C: Configuration,
{
@ -247,14 +247,14 @@ where
}
}
impl<C> IngredientRequiresReset for InternedIngredient<C>
impl<C> IngredientRequiresReset for IngredientImpl<C>
where
C: Configuration,
{
const RESET_ON_NEW_REVISION: bool = false;
}
impl<C> std::fmt::Debug for InternedIngredient<C>
impl<C> std::fmt::Debug for IngredientImpl<C>
where
C: Configuration,
{

View file

@ -1,28 +1,27 @@
pub mod accumulator;
mod accumulator;
mod alloc;
pub mod cancelled;
pub mod cycle;
pub mod database;
pub mod durability;
pub mod event;
pub mod function;
pub mod hash;
pub mod id;
pub mod ingredient;
pub mod ingredient_list;
pub mod input;
pub mod input_field;
pub mod interned;
pub mod key;
mod cancelled;
mod cycle;
mod database;
mod durability;
mod event;
mod function;
mod hash;
mod id;
mod ingredient;
mod ingredient_list;
mod input;
mod input_field;
mod interned;
mod key;
mod nonce;
pub mod plumbing;
pub mod revision;
pub mod runtime;
pub mod salsa_struct;
pub mod setter;
pub mod storage;
pub mod tracked_struct;
pub mod update;
mod revision;
mod runtime;
mod salsa_struct;
mod setter;
mod storage;
mod tracked_struct;
mod update;
mod views;
pub use self::cancelled::Cancelled;
@ -45,3 +44,47 @@ pub use salsa_macros::interned;
pub use salsa_macros::tracked;
pub use salsa_macros::DebugWithDb;
pub use salsa_macros::Update;
/// Internal names used by salsa macros.
///
/// # WARNING
///
/// The contents of this module are NOT subject to semver.
pub mod plumbing {
pub use crate::cycle::Cycle;
pub use crate::cycle::CycleRecoveryStrategy;
pub use crate::database::Database;
pub use crate::id::AsId;
pub use crate::id::FromId;
pub use crate::id::Id;
pub use crate::ingredient::Ingredient;
pub use crate::ingredient::Jar;
pub use crate::salsa_struct::SalsaStructInDb;
pub use crate::storage::views;
pub use crate::storage::IngredientCache;
pub use crate::storage::IngredientIndex;
pub mod input {
pub use crate::input::Configuration;
pub use crate::input::IngredientImpl;
pub use crate::input_field::FieldIngredientImpl;
}
pub mod interned {
pub use crate::interned::Configuration;
pub use crate::interned::IngredientImpl;
pub use crate::interned::ValueStruct;
}
pub mod function {
pub use crate::function::Configuration;
pub use crate::function::IngredientImpl;
}
pub mod tracked_struct {
pub use crate::tracked_struct::tracked_field::FieldIngredientImpl;
pub use crate::tracked_struct::Configuration;
pub use crate::tracked_struct::IngredientImpl;
pub use crate::tracked_struct::JarImpl;
}
}

View file

@ -1,8 +0,0 @@
// Returns `u` but with the lifetime of `t`.
//
// Safe if you know that data at `u` will remain shared
// until the reference `t` expires.
#[allow(clippy::needless_lifetimes)]
pub(crate) unsafe fn transmute_lifetime<'t, 'u, T, U>(_t: &'t T, u: &'u U) -> &'t U {
std::mem::transmute(u)
}

View file

@ -1,6 +1,6 @@
use crate::id::AsId;
use crate::input::Configuration;
use crate::input_field::{InputFieldData, InputFieldIngredient};
use crate::input_field::{FieldIngredientImpl, InputFieldData};
use crate::{Durability, Runtime};
use std::hash::Hash;
@ -8,7 +8,7 @@ use std::hash::Hash;
pub struct Setter<'setter, C: Configuration, F: InputFieldData> {
runtime: &'setter mut Runtime,
key: C::Id,
ingredient: &'setter mut InputFieldIngredient<C, F>,
ingredient: &'setter mut FieldIngredientImpl<C, F>,
durability: Durability,
}
@ -20,7 +20,7 @@ where
pub fn new(
runtime: &'setter mut Runtime,
key: C::Id,
ingredient: &'setter mut InputFieldIngredient<C, F>,
ingredient: &'setter mut FieldIngredientImpl<C, F>,
) -> Self {
Setter {
runtime,

View file

@ -2,6 +2,7 @@ use std::{fmt, hash::Hash, marker::PhantomData, ptr::NonNull};
use crossbeam::atomic::AtomicCell;
use dashmap::mapref::entry::Entry;
use tracked_field::FieldIngredientImpl;
use crate::{
cycle::CycleRecoveryStrategy,
@ -17,10 +18,9 @@ use crate::{
};
use self::struct_map::{StructMap, Update};
pub use self::tracked_field::TrackedFieldIngredient;
mod struct_map;
mod tracked_field;
pub mod tracked_field;
// ANCHOR: Configuration
/// Trait that defines the key properties of a tracked struct.
@ -99,14 +99,14 @@ pub trait Configuration: Jar + Sized + 'static {
}
// ANCHOR_END: Configuration
pub struct TrackedStructJar<C>
pub struct JarImpl<C>
where
C: Configuration,
{
phantom: PhantomData<C>,
}
impl<C: Configuration> Default for TrackedStructJar<C> {
impl<C: Configuration> Default for JarImpl<C> {
fn default() -> Self {
Self {
phantom: Default::default(),
@ -114,18 +114,18 @@ impl<C: Configuration> Default for TrackedStructJar<C> {
}
}
impl<C: Configuration> Jar for TrackedStructJar<C> {
impl<C: Configuration> Jar for JarImpl<C> {
fn create_ingredients(
&self,
struct_index: crate::storage::IngredientIndex,
) -> Vec<Box<dyn Ingredient>> {
let struct_ingredient = TrackedStructIngredient::new(struct_index);
let struct_ingredient = IngredientImpl::new(struct_index);
let struct_map = &struct_ingredient.struct_map.view();
std::iter::once(Box::new(struct_ingredient) as _)
.chain(
(0..u32::try_from(C::FIELD_DEBUG_NAMES.len()).unwrap()).map(|field_index| {
Box::new(TrackedFieldIngredient::<C>::new(
Box::new(FieldIngredientImpl::<C>::new(
struct_index,
field_index,
struct_map,
@ -149,7 +149,7 @@ pub trait TrackedStructInDb<DB: ?Sized + Database>: SalsaStructInDb<DB> {
/// Unlike normal interners, tracked struct indices can be deleted and reused aggressively:
/// when a tracked function re-executes,
/// any tracked structs that it created before but did not create this time can be deleted.
pub struct TrackedStructIngredient<C>
pub struct IngredientImpl<C>
where
C: Configuration,
{
@ -236,7 +236,7 @@ where
#[derive(Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Copy, Clone)]
pub struct Disambiguator(pub u32);
impl<C> TrackedStructIngredient<C>
impl<C> IngredientImpl<C>
where
C: Configuration,
{
@ -420,7 +420,7 @@ where
}
}
impl<C> Ingredient for TrackedStructIngredient<C>
impl<C> Ingredient for IngredientImpl<C>
where
C: Configuration,
{
@ -482,7 +482,7 @@ where
}
}
impl<C> std::fmt::Debug for TrackedStructIngredient<C>
impl<C> std::fmt::Debug for IngredientImpl<C>
where
C: Configuration,
{
@ -493,7 +493,7 @@ where
}
}
impl<C> IngredientRequiresReset for TrackedStructIngredient<C>
impl<C> IngredientRequiresReset for IngredientImpl<C>
where
C: Configuration,
{

View file

@ -16,7 +16,7 @@ use super::{struct_map::StructMapView, Configuration};
/// Unlike normal interners, tracked struct indices can be deleted and reused aggressively:
/// when a tracked function re-executes,
/// any tracked structs that it created before but did not create this time can be deleted.
pub struct TrackedFieldIngredient<C>
pub struct FieldIngredientImpl<C>
where
C: Configuration,
{
@ -26,7 +26,7 @@ where
struct_map: StructMapView<C>,
}
impl<C> TrackedFieldIngredient<C>
impl<C> FieldIngredientImpl<C>
where
C: Configuration,
{
@ -68,7 +68,7 @@ where
}
}
impl<C> Ingredient for TrackedFieldIngredient<C>
impl<C> Ingredient for FieldIngredientImpl<C>
where
C: Configuration,
{
@ -139,7 +139,7 @@ where
}
}
impl<C> std::fmt::Debug for TrackedFieldIngredient<C>
impl<C> std::fmt::Debug for FieldIngredientImpl<C>
where
C: Configuration,
{
@ -151,7 +151,7 @@ where
}
}
impl<C> IngredientRequiresReset for TrackedFieldIngredient<C>
impl<C> IngredientRequiresReset for FieldIngredientImpl<C>
where
C: Configuration,
{