mirror of
https://github.com/salsa-rs/salsa.git
synced 2025-08-05 03:18:19 +00:00
Compare commits
17 commits
salsa-macr
...
master
Author | SHA1 | Date | |
---|---|---|---|
![]() |
86ca4a9d70 | ||
![]() |
c3f86b8d02 | ||
![]() |
5b411a290c | ||
![]() |
679d82c4e7 | ||
![]() |
f303b6db56 | ||
![]() |
0ca4f4ff9a | ||
![]() |
211bc158df | ||
![]() |
f3dc2f30f9 | ||
![]() |
8b6d12b596 | ||
![]() |
bb0831a640 | ||
![]() |
53cd6b15ba | ||
![]() |
0e1df67ec4 | ||
![]() |
962e0b924e | ||
![]() |
dba66f1a37 | ||
![]() |
d28d66bf13 | ||
![]() |
fc00eba89e | ||
![]() |
7ab42086d1 |
149 changed files with 2386 additions and 1280 deletions
4
.github/workflows/test.yml
vendored
4
.github/workflows/test.yml
vendored
|
@ -55,10 +55,10 @@ jobs:
|
|||
run: cargo clippy --workspace --all-targets -- -D warnings
|
||||
- name: Test
|
||||
run: cargo nextest run --workspace --all-targets --no-fail-fast
|
||||
- name: Test Manual Registration / no-default-features
|
||||
run: cargo nextest run --workspace --tests --no-fail-fast --no-default-features --features macros
|
||||
- name: Test docs
|
||||
run: cargo test --workspace --doc
|
||||
- name: Check (without default features)
|
||||
run: cargo check --workspace --no-default-features
|
||||
|
||||
miri:
|
||||
name: Miri
|
||||
|
|
35
Cargo.toml
35
Cargo.toml
|
@ -13,31 +13,35 @@ salsa-macro-rules = { version = "0.23.0", path = "components/salsa-macro-rules"
|
|||
salsa-macros = { version = "0.23.0", path = "components/salsa-macros", optional = true }
|
||||
|
||||
boxcar = "0.2.13"
|
||||
crossbeam-queue = "0.3.11"
|
||||
crossbeam-queue = "0.3.12"
|
||||
crossbeam-utils = "0.8.21"
|
||||
hashbrown = "0.15"
|
||||
hashlink = "0.10"
|
||||
indexmap = "2"
|
||||
intrusive-collections = "0.9.7"
|
||||
papaya = "0.2.2"
|
||||
parking_lot = "0.12"
|
||||
portable-atomic = "1"
|
||||
rustc-hash = "2"
|
||||
smallvec = "1"
|
||||
tracing = { version = "0.1", default-features = false, features = ["std"] }
|
||||
|
||||
# Automatic ingredient registration.
|
||||
inventory = { version = "0.3.20", optional = true }
|
||||
|
||||
# parallel map
|
||||
rayon = { version = "1.10.0", optional = true }
|
||||
|
||||
# Stuff we want Update impls for by default
|
||||
compact_str = { version = "0.9", optional = true }
|
||||
thin-vec = "0.2.13"
|
||||
thin-vec = "0.2.14"
|
||||
|
||||
shuttle = { version = "0.8.0", optional = true }
|
||||
shuttle = { version = "0.8.1", optional = true }
|
||||
|
||||
[features]
|
||||
default = ["salsa_unstable", "rayon", "macros"]
|
||||
default = ["salsa_unstable", "rayon", "macros", "inventory", "accumulator"]
|
||||
inventory = ["dep:inventory"]
|
||||
shuttle = ["dep:shuttle"]
|
||||
accumulator = ["salsa-macro-rules/accumulator"]
|
||||
# FIXME: remove `salsa_unstable` before 1.0.
|
||||
salsa_unstable = []
|
||||
macros = ["dep:salsa-macros"]
|
||||
|
@ -51,18 +55,18 @@ salsa-macros = { version = "=0.23.0", path = "components/salsa-macros" }
|
|||
|
||||
[dev-dependencies]
|
||||
# examples
|
||||
crossbeam-channel = "0.5.14"
|
||||
crossbeam-channel = "0.5.15"
|
||||
dashmap = { version = "6", features = ["raw-api"] }
|
||||
eyre = "0.6.8"
|
||||
eyre = "0.6.12"
|
||||
notify-debouncer-mini = "0.4.1"
|
||||
ordered-float = "4.2.1"
|
||||
ordered-float = "5.0.0"
|
||||
|
||||
# tests/benches
|
||||
annotate-snippets = "0.11.5"
|
||||
codspeed-criterion-compat = { version = "2.6.0", default-features = false }
|
||||
expect-test = "1.5.0"
|
||||
codspeed-criterion-compat = { version = "3.0.5", default-features = false }
|
||||
expect-test = "1.5.1"
|
||||
rustversion = "1.0"
|
||||
test-log = { version = "0.2.11", features = ["trace"] }
|
||||
test-log = { version = "0.2.18", features = ["trace"] }
|
||||
trybuild = "1.0"
|
||||
|
||||
[target.'cfg(all(not(target_os = "windows"), not(target_os = "openbsd"), any(target_arch = "x86_64", target_arch = "aarch64", target_arch = "powerpc64")))'.dev-dependencies]
|
||||
|
@ -79,11 +83,20 @@ harness = false
|
|||
[[bench]]
|
||||
name = "accumulator"
|
||||
harness = false
|
||||
required-features = ["accumulator"]
|
||||
|
||||
[[bench]]
|
||||
name = "dataflow"
|
||||
harness = false
|
||||
|
||||
[[example]]
|
||||
name = "lazy-input"
|
||||
required-features = ["accumulator"]
|
||||
|
||||
[[example]]
|
||||
name = "calc"
|
||||
required-features = ["accumulator"]
|
||||
|
||||
[workspace]
|
||||
members = ["components/salsa-macro-rules", "components/salsa-macros"]
|
||||
|
||||
|
|
|
@ -9,3 +9,6 @@ rust-version.workspace = true
|
|||
description = "Declarative macros for the salsa crate"
|
||||
|
||||
[dependencies]
|
||||
|
||||
[features]
|
||||
accumulator = []
|
||||
|
|
13
components/salsa-macro-rules/src/gate_accumulated.rs
Normal file
13
components/salsa-macro-rules/src/gate_accumulated.rs
Normal file
|
@ -0,0 +1,13 @@
|
|||
#[cfg(feature = "accumulator")]
|
||||
#[macro_export]
|
||||
macro_rules! gate_accumulated {
|
||||
($($body:tt)*) => {
|
||||
$($body)*
|
||||
};
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "accumulator"))]
|
||||
#[macro_export]
|
||||
macro_rules! gate_accumulated {
|
||||
($($body:tt)*) => {};
|
||||
}
|
|
@ -12,10 +12,12 @@
|
|||
//! from a submodule is to use multiple crates, hence the existence
|
||||
//! of this crate.
|
||||
|
||||
mod gate_accumulated;
|
||||
mod macro_if;
|
||||
mod maybe_backdate;
|
||||
mod maybe_default;
|
||||
mod return_mode;
|
||||
#[cfg(feature = "accumulator")]
|
||||
mod setup_accumulator_impl;
|
||||
mod setup_input_struct;
|
||||
mod setup_interned_struct;
|
||||
|
|
|
@ -21,15 +21,26 @@ macro_rules! setup_accumulator_impl {
|
|||
use salsa::plumbing as $zalsa;
|
||||
use salsa::plumbing::accumulator as $zalsa_struct;
|
||||
|
||||
impl $zalsa::HasJar for $Struct {
|
||||
type Jar = $zalsa_struct::JarImpl<$Struct>;
|
||||
const KIND: $zalsa::JarKind = $zalsa::JarKind::Struct;
|
||||
}
|
||||
|
||||
$zalsa::register_jar! {
|
||||
$zalsa::ErasedJar::erase::<$Struct>()
|
||||
}
|
||||
|
||||
fn $ingredient(zalsa: &$zalsa::Zalsa) -> &$zalsa_struct::IngredientImpl<$Struct> {
|
||||
static $CACHE: $zalsa::IngredientCache<$zalsa_struct::IngredientImpl<$Struct>> =
|
||||
$zalsa::IngredientCache::new();
|
||||
|
||||
$CACHE.get_or_create(zalsa, || {
|
||||
zalsa
|
||||
.lookup_jar_by_type::<$zalsa_struct::JarImpl<$Struct>>()
|
||||
.get_or_create()
|
||||
})
|
||||
// SAFETY: `lookup_jar_by_type` returns a valid ingredient index, and the only
|
||||
// ingredient created by our jar is the struct ingredient.
|
||||
unsafe {
|
||||
$CACHE.get_or_create(zalsa, || {
|
||||
zalsa.lookup_jar_by_type::<$zalsa_struct::JarImpl<$Struct>>()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl $zalsa::Accumulator for $Struct {
|
||||
|
|
|
@ -74,6 +74,15 @@ macro_rules! setup_input_struct {
|
|||
|
||||
type $Configuration = $Struct;
|
||||
|
||||
impl $zalsa::HasJar for $Struct {
|
||||
type Jar = $zalsa_struct::JarImpl<$Configuration>;
|
||||
const KIND: $zalsa::JarKind = $zalsa::JarKind::Struct;
|
||||
}
|
||||
|
||||
$zalsa::register_jar! {
|
||||
$zalsa::ErasedJar::erase::<$Struct>()
|
||||
}
|
||||
|
||||
impl $zalsa_struct::Configuration for $Configuration {
|
||||
const LOCATION: $zalsa::Location = $zalsa::Location {
|
||||
file: file!(),
|
||||
|
@ -100,15 +109,18 @@ macro_rules! setup_input_struct {
|
|||
static CACHE: $zalsa::IngredientCache<$zalsa_struct::IngredientImpl<$Configuration>> =
|
||||
$zalsa::IngredientCache::new();
|
||||
|
||||
CACHE.get_or_create(zalsa, || {
|
||||
zalsa.lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>().get_or_create()
|
||||
})
|
||||
// SAFETY: `lookup_jar_by_type` returns a valid ingredient index, and the only
|
||||
// ingredient created by our jar is the struct ingredient.
|
||||
unsafe {
|
||||
CACHE.get_or_create(zalsa, || {
|
||||
zalsa.lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ingredient_mut(db: &mut dyn $zalsa::Database) -> (&mut $zalsa_struct::IngredientImpl<Self>, &mut $zalsa::Runtime) {
|
||||
let zalsa_mut = db.zalsa_mut();
|
||||
pub fn ingredient_mut(zalsa_mut: &mut $zalsa::Zalsa) -> (&mut $zalsa_struct::IngredientImpl<Self>, &mut $zalsa::Runtime) {
|
||||
zalsa_mut.new_revision();
|
||||
let index = zalsa_mut.lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>().get_or_create();
|
||||
let index = zalsa_mut.lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>();
|
||||
let (ingredient, runtime) = zalsa_mut.lookup_ingredient_mut(index);
|
||||
let ingredient = ingredient.assert_type_mut::<$zalsa_struct::IngredientImpl<Self>>();
|
||||
(ingredient, runtime)
|
||||
|
@ -149,8 +161,8 @@ macro_rules! setup_input_struct {
|
|||
impl $zalsa::SalsaStructInDb for $Struct {
|
||||
type MemoIngredientMap = $zalsa::MemoIngredientSingletonIndex;
|
||||
|
||||
fn lookup_or_create_ingredient_index(aux: &$zalsa::Zalsa) -> $zalsa::IngredientIndices {
|
||||
aux.lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>().get_or_create().into()
|
||||
fn lookup_ingredient_index(aux: &$zalsa::Zalsa) -> $zalsa::IngredientIndices {
|
||||
aux.lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>().into()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
@ -161,6 +173,16 @@ macro_rules! setup_input_struct {
|
|||
$zalsa::None
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn memo_table(
|
||||
zalsa: &$zalsa::Zalsa,
|
||||
id: $zalsa::Id,
|
||||
current_revision: $zalsa::Revision,
|
||||
) -> $zalsa::MemoTableWithTypes<'_> {
|
||||
// SAFETY: Guaranteed by caller.
|
||||
unsafe { zalsa.table().memos::<$zalsa_struct::Value<$Configuration>>(id, current_revision) }
|
||||
}
|
||||
}
|
||||
|
||||
impl $Struct {
|
||||
|
@ -185,8 +207,10 @@ macro_rules! setup_input_struct {
|
|||
// FIXME(rust-lang/rust#65991): The `db` argument *should* have the type `dyn Database`
|
||||
$Db: ?Sized + $zalsa::Database,
|
||||
{
|
||||
let fields = $Configuration::ingredient_(db.zalsa()).field(
|
||||
db.as_dyn_database(),
|
||||
let (zalsa, zalsa_local) = db.zalsas();
|
||||
let fields = $Configuration::ingredient_(zalsa).field(
|
||||
zalsa,
|
||||
zalsa_local,
|
||||
self,
|
||||
$field_index,
|
||||
);
|
||||
|
@ -205,7 +229,8 @@ macro_rules! setup_input_struct {
|
|||
// FIXME(rust-lang/rust#65991): The `db` argument *should* have the type `dyn Database`
|
||||
$Db: ?Sized + $zalsa::Database,
|
||||
{
|
||||
let (ingredient, revision) = $Configuration::ingredient_mut(db.as_dyn_database_mut());
|
||||
let zalsa = db.zalsa_mut();
|
||||
let (ingredient, revision) = $Configuration::ingredient_mut(zalsa);
|
||||
$zalsa::input::SetterImpl::new(
|
||||
revision,
|
||||
self,
|
||||
|
@ -244,7 +269,8 @@ macro_rules! setup_input_struct {
|
|||
$(for<'__trivial_bounds> $field_ty: std::fmt::Debug),*
|
||||
{
|
||||
$zalsa::with_attached_database(|db| {
|
||||
let fields = $Configuration::ingredient(db).leak_fields(db, this);
|
||||
let zalsa = db.zalsa();
|
||||
let fields = $Configuration::ingredient_(zalsa).leak_fields(zalsa, this);
|
||||
let mut f = f.debug_struct(stringify!($Struct));
|
||||
let f = f.field("[salsa id]", &$zalsa::AsId::as_id(&this));
|
||||
$(
|
||||
|
@ -273,11 +299,11 @@ macro_rules! setup_input_struct {
|
|||
// FIXME(rust-lang/rust#65991): The `db` argument *should* have the type `dyn Database`
|
||||
$Db: ?Sized + salsa::Database
|
||||
{
|
||||
let zalsa = db.zalsa();
|
||||
let (zalsa, zalsa_local) = db.zalsas();
|
||||
let current_revision = zalsa.current_revision();
|
||||
let ingredient = $Configuration::ingredient_(zalsa);
|
||||
let (fields, revision, durabilities) = builder::builder_into_inner(self, current_revision);
|
||||
ingredient.new_input(db.as_dyn_database(), fields, revision, durabilities)
|
||||
ingredient.new_input(zalsa, zalsa_local, fields, revision, durabilities)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -81,7 +81,7 @@ macro_rules! setup_interned_struct {
|
|||
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
|
||||
$vis struct $Struct< $($db_lt_arg)? >(
|
||||
$Id,
|
||||
std::marker::PhantomData < & $interior_lt salsa::plumbing::interned::Value <$StructWithStatic> >
|
||||
std::marker::PhantomData<fn() -> &$interior_lt ()>
|
||||
);
|
||||
|
||||
#[allow(clippy::all)]
|
||||
|
@ -92,6 +92,15 @@ macro_rules! setup_interned_struct {
|
|||
|
||||
type $Configuration = $StructWithStatic;
|
||||
|
||||
impl<$($db_lt_arg)?> $zalsa::HasJar for $Struct<$($db_lt_arg)?> {
|
||||
type Jar = $zalsa_struct::JarImpl<$Configuration>;
|
||||
const KIND: $zalsa::JarKind = $zalsa::JarKind::Struct;
|
||||
}
|
||||
|
||||
$zalsa::register_jar! {
|
||||
$zalsa::ErasedJar::erase::<$StructWithStatic>()
|
||||
}
|
||||
|
||||
type $StructDataIdent<$db_lt> = ($($field_ty,)*);
|
||||
|
||||
/// Key to use during hash lookups. Each field is some type that implements `Lookup<T>`
|
||||
|
@ -140,17 +149,18 @@ macro_rules! setup_interned_struct {
|
|||
}
|
||||
|
||||
impl $Configuration {
|
||||
pub fn ingredient<Db>(db: &Db) -> &$zalsa_struct::IngredientImpl<Self>
|
||||
where
|
||||
Db: ?Sized + $zalsa::Database,
|
||||
pub fn ingredient(zalsa: &$zalsa::Zalsa) -> &$zalsa_struct::IngredientImpl<Self>
|
||||
{
|
||||
static CACHE: $zalsa::IngredientCache<$zalsa_struct::IngredientImpl<$Configuration>> =
|
||||
$zalsa::IngredientCache::new();
|
||||
|
||||
let zalsa = db.zalsa();
|
||||
CACHE.get_or_create(zalsa, || {
|
||||
zalsa.lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>().get_or_create()
|
||||
})
|
||||
// SAFETY: `lookup_jar_by_type` returns a valid ingredient index, and the only
|
||||
// ingredient created by our jar is the struct ingredient.
|
||||
unsafe {
|
||||
CACHE.get_or_create(zalsa, || {
|
||||
zalsa.lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>()
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -181,8 +191,8 @@ macro_rules! setup_interned_struct {
|
|||
impl< $($db_lt_arg)? > $zalsa::SalsaStructInDb for $Struct< $($db_lt_arg)? > {
|
||||
type MemoIngredientMap = $zalsa::MemoIngredientSingletonIndex;
|
||||
|
||||
fn lookup_or_create_ingredient_index(aux: &$zalsa::Zalsa) -> $zalsa::IngredientIndices {
|
||||
aux.lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>().get_or_create().into()
|
||||
fn lookup_ingredient_index(aux: &$zalsa::Zalsa) -> $zalsa::IngredientIndices {
|
||||
aux.lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>().into()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
@ -193,6 +203,16 @@ macro_rules! setup_interned_struct {
|
|||
$zalsa::None
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn memo_table(
|
||||
zalsa: &$zalsa::Zalsa,
|
||||
id: $zalsa::Id,
|
||||
current_revision: $zalsa::Revision,
|
||||
) -> $zalsa::MemoTableWithTypes<'_> {
|
||||
// SAFETY: Guaranteed by caller.
|
||||
unsafe { zalsa.table().memos::<$zalsa_struct::Value<$Configuration>>(id, current_revision) }
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl< $($db_lt_arg)? > $zalsa::Update for $Struct< $($db_lt_arg)? > {
|
||||
|
@ -215,7 +235,8 @@ macro_rules! setup_interned_struct {
|
|||
$field_ty: $zalsa::interned::HashEqLike<$indexed_ty>,
|
||||
)*
|
||||
{
|
||||
$Configuration::ingredient(db).intern(db.as_dyn_database(),
|
||||
let (zalsa, zalsa_local) = db.zalsas();
|
||||
$Configuration::ingredient(zalsa).intern(zalsa, zalsa_local,
|
||||
StructKey::<$db_lt>($($field_id,)* std::marker::PhantomData::default()), |_, data| ($($zalsa::interned::Lookup::into_owned(data.$field_index),)*))
|
||||
}
|
||||
|
||||
|
@ -226,7 +247,8 @@ macro_rules! setup_interned_struct {
|
|||
// FIXME(rust-lang/rust#65991): The `db` argument *should* have the type `dyn Database`
|
||||
$Db: ?Sized + $zalsa::Database,
|
||||
{
|
||||
let fields = $Configuration::ingredient(db).fields(db.as_dyn_database(), self);
|
||||
let zalsa = db.zalsa();
|
||||
let fields = $Configuration::ingredient(zalsa).fields(zalsa, self);
|
||||
$zalsa::return_mode_expression!(
|
||||
$field_option,
|
||||
$field_ty,
|
||||
|
@ -238,7 +260,8 @@ macro_rules! setup_interned_struct {
|
|||
/// Default debug formatting for this struct (may be useful if you define your own `Debug` impl)
|
||||
pub fn default_debug_fmt(this: Self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
$zalsa::with_attached_database(|db| {
|
||||
let fields = $Configuration::ingredient(db).fields(db.as_dyn_database(), this);
|
||||
let zalsa = db.zalsa();
|
||||
let fields = $Configuration::ingredient(zalsa).fields(zalsa, this);
|
||||
let mut f = f.debug_struct(stringify!($Struct));
|
||||
$(
|
||||
let f = f.field(stringify!($field_id), &fields.$field_index);
|
||||
|
|
|
@ -91,6 +91,16 @@ macro_rules! setup_tracked_fn {
|
|||
|
||||
struct $Configuration;
|
||||
|
||||
$zalsa::register_jar! {
|
||||
$zalsa::ErasedJar::erase::<$fn_name>()
|
||||
}
|
||||
|
||||
#[allow(non_local_definitions)]
|
||||
impl $zalsa::HasJar for $fn_name {
|
||||
type Jar = $fn_name;
|
||||
const KIND: $zalsa::JarKind = $zalsa::JarKind::TrackedFn;
|
||||
}
|
||||
|
||||
static $FN_CACHE: $zalsa::IngredientCache<$zalsa::function::IngredientImpl<$Configuration>> =
|
||||
$zalsa::IngredientCache::new();
|
||||
|
||||
|
@ -99,7 +109,7 @@ macro_rules! setup_tracked_fn {
|
|||
#[derive(Copy, Clone)]
|
||||
struct $InternedData<$db_lt>(
|
||||
salsa::Id,
|
||||
std::marker::PhantomData<&$db_lt $zalsa::interned::Value<$Configuration>>,
|
||||
std::marker::PhantomData<fn() -> &$db_lt ()>,
|
||||
);
|
||||
|
||||
static $INTERN_CACHE: $zalsa::IngredientCache<$zalsa::interned::IngredientImpl<$Configuration>> =
|
||||
|
@ -108,7 +118,7 @@ macro_rules! setup_tracked_fn {
|
|||
impl $zalsa::SalsaStructInDb for $InternedData<'_> {
|
||||
type MemoIngredientMap = $zalsa::MemoIngredientSingletonIndex;
|
||||
|
||||
fn lookup_or_create_ingredient_index(aux: &$zalsa::Zalsa) -> $zalsa::IngredientIndices {
|
||||
fn lookup_ingredient_index(aux: &$zalsa::Zalsa) -> $zalsa::IngredientIndices {
|
||||
$zalsa::IngredientIndices::empty()
|
||||
}
|
||||
|
||||
|
@ -120,6 +130,16 @@ macro_rules! setup_tracked_fn {
|
|||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn memo_table(
|
||||
zalsa: &$zalsa::Zalsa,
|
||||
id: $zalsa::Id,
|
||||
current_revision: $zalsa::Revision,
|
||||
) -> $zalsa::MemoTableWithTypes<'_> {
|
||||
// SAFETY: Guaranteed by caller.
|
||||
unsafe { zalsa.table().memos::<$zalsa::interned::Value<$Configuration>>(id, current_revision) }
|
||||
}
|
||||
}
|
||||
|
||||
impl $zalsa::AsId for $InternedData<'_> {
|
||||
|
@ -155,27 +175,27 @@ macro_rules! setup_tracked_fn {
|
|||
impl $Configuration {
|
||||
fn fn_ingredient(db: &dyn $Db) -> &$zalsa::function::IngredientImpl<$Configuration> {
|
||||
let zalsa = db.zalsa();
|
||||
$FN_CACHE.get_or_create(zalsa, || {
|
||||
let jar_entry = zalsa.lookup_jar_by_type::<$Configuration>();
|
||||
Self::fn_ingredient_(db, zalsa)
|
||||
}
|
||||
|
||||
// If the ingredient has already been inserted, we know that the downcaster
|
||||
// has also been registered. This is a fast-path for multi-database use cases
|
||||
// that bypass the ingredient cache and will always execute this closure.
|
||||
if let Some(index) = jar_entry.get() {
|
||||
return index;
|
||||
}
|
||||
|
||||
<dyn $Db as $Db>::zalsa_register_downcaster(db);
|
||||
jar_entry.get_or_create()
|
||||
})
|
||||
#[inline]
|
||||
fn fn_ingredient_<'z>(db: &dyn $Db, zalsa: &'z $zalsa::Zalsa) -> &'z $zalsa::function::IngredientImpl<$Configuration> {
|
||||
// SAFETY: `lookup_jar_by_type` returns a valid ingredient index, and the first
|
||||
// ingredient created by our jar is the function ingredient.
|
||||
unsafe {
|
||||
$FN_CACHE.get_or_create(zalsa, || zalsa.lookup_jar_by_type::<$fn_name>())
|
||||
}
|
||||
.get_or_init(|| *<dyn $Db as $Db>::zalsa_register_downcaster(db))
|
||||
}
|
||||
|
||||
pub fn fn_ingredient_mut(db: &mut dyn $Db) -> &mut $zalsa::function::IngredientImpl<Self> {
|
||||
<dyn $Db as $Db>::zalsa_register_downcaster(db);
|
||||
let view = *<dyn $Db as $Db>::zalsa_register_downcaster(db);
|
||||
let zalsa_mut = db.zalsa_mut();
|
||||
let index = zalsa_mut.lookup_jar_by_type::<$Configuration>().get_or_create();
|
||||
let index = zalsa_mut.lookup_jar_by_type::<$fn_name>();
|
||||
let (ingredient, _) = zalsa_mut.lookup_ingredient_mut(index);
|
||||
ingredient.assert_type_mut::<$zalsa::function::IngredientImpl<Self>>()
|
||||
let ingredient = ingredient.assert_type_mut::<$zalsa::function::IngredientImpl<Self>>();
|
||||
ingredient.get_or_init(|| view);
|
||||
ingredient
|
||||
}
|
||||
|
||||
$zalsa::macro_if! { $needs_interner =>
|
||||
|
@ -183,10 +203,19 @@ macro_rules! setup_tracked_fn {
|
|||
db: &dyn $Db,
|
||||
) -> &$zalsa::interned::IngredientImpl<$Configuration> {
|
||||
let zalsa = db.zalsa();
|
||||
$INTERN_CACHE.get_or_create(zalsa, || {
|
||||
<dyn $Db as $Db>::zalsa_register_downcaster(db);
|
||||
zalsa.lookup_jar_by_type::<$Configuration>().get_or_create().successor(0)
|
||||
})
|
||||
Self::intern_ingredient_(zalsa)
|
||||
}
|
||||
#[inline]
|
||||
fn intern_ingredient_<'z>(
|
||||
zalsa: &'z $zalsa::Zalsa
|
||||
) -> &'z $zalsa::interned::IngredientImpl<$Configuration> {
|
||||
// SAFETY: `lookup_jar_by_type` returns a valid ingredient index, and the second
|
||||
// ingredient created by our jar is the interned ingredient (given `needs_interner`).
|
||||
unsafe {
|
||||
$INTERN_CACHE.get_or_create(zalsa, || {
|
||||
zalsa.lookup_jar_by_type::<$fn_name>().successor(0)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -237,53 +266,42 @@ macro_rules! setup_tracked_fn {
|
|||
$($cycle_recovery_fn)*(db, value, count, $($input_id),*)
|
||||
}
|
||||
|
||||
fn id_to_input<$db_lt>(db: &$db_lt Self::DbView, key: salsa::Id) -> Self::Input<$db_lt> {
|
||||
fn id_to_input<$db_lt>(zalsa: &$db_lt $zalsa::Zalsa, key: salsa::Id) -> Self::Input<$db_lt> {
|
||||
$zalsa::macro_if! {
|
||||
if $needs_interner {
|
||||
$Configuration::intern_ingredient(db).data(db.as_dyn_database(), key).clone()
|
||||
$Configuration::intern_ingredient_(zalsa).data(zalsa, key).clone()
|
||||
} else {
|
||||
$zalsa::FromIdWithDb::from_id(key, db.zalsa())
|
||||
$zalsa::FromIdWithDb::from_id(key, zalsa)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl $zalsa::Jar for $Configuration {
|
||||
fn create_dependencies(zalsa: &$zalsa::Zalsa) -> $zalsa::IngredientIndices
|
||||
where
|
||||
Self: Sized
|
||||
{
|
||||
$zalsa::macro_if! {
|
||||
if $needs_interner {
|
||||
$zalsa::IngredientIndices::empty()
|
||||
} else {
|
||||
<$InternedData as $zalsa::SalsaStructInDb>::lookup_or_create_ingredient_index(zalsa)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(non_local_definitions)]
|
||||
impl $zalsa::Jar for $fn_name {
|
||||
fn create_ingredients(
|
||||
zalsa: &$zalsa::Zalsa,
|
||||
zalsa: &mut $zalsa::Zalsa,
|
||||
first_index: $zalsa::IngredientIndex,
|
||||
struct_index: $zalsa::IngredientIndices,
|
||||
) -> Vec<Box<dyn $zalsa::Ingredient>> {
|
||||
let struct_index: $zalsa::IngredientIndices = $zalsa::macro_if! {
|
||||
if $needs_interner {
|
||||
first_index.successor(0).into()
|
||||
} else {
|
||||
struct_index
|
||||
// Note that struct ingredients are created before tracked functions,
|
||||
// so this cannot panic.
|
||||
<$InternedData as $zalsa::SalsaStructInDb>::lookup_ingredient_index(zalsa)
|
||||
}
|
||||
};
|
||||
|
||||
$zalsa::macro_if! { $needs_interner =>
|
||||
let intern_ingredient = <$zalsa::interned::IngredientImpl<$Configuration>>::new(
|
||||
let mut intern_ingredient = <$zalsa::interned::IngredientImpl<$Configuration>>::new(
|
||||
first_index.successor(0)
|
||||
);
|
||||
}
|
||||
|
||||
let intern_ingredient_memo_types = $zalsa::macro_if! {
|
||||
if $needs_interner {
|
||||
Some($zalsa::Ingredient::memo_table_types(&intern_ingredient))
|
||||
Some($zalsa::Ingredient::memo_table_types_mut(&mut intern_ingredient))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
@ -303,7 +321,6 @@ macro_rules! setup_tracked_fn {
|
|||
first_index,
|
||||
memo_ingredient_indices,
|
||||
$lru,
|
||||
zalsa.views().downcaster_for::<dyn $Db>(),
|
||||
);
|
||||
$zalsa::macro_if! {
|
||||
if $needs_interner {
|
||||
|
@ -326,20 +343,23 @@ macro_rules! setup_tracked_fn {
|
|||
|
||||
#[allow(non_local_definitions)]
|
||||
impl $fn_name {
|
||||
pub fn accumulated<$db_lt, A: salsa::Accumulator>(
|
||||
$db: &$db_lt dyn $Db,
|
||||
$($input_id: $interned_input_ty,)*
|
||||
) -> Vec<&$db_lt A> {
|
||||
use salsa::plumbing as $zalsa;
|
||||
let key = $zalsa::macro_if! {
|
||||
if $needs_interner {
|
||||
$Configuration::intern_ingredient($db).intern_id($db.as_dyn_database(), ($($input_id),*), |_, data| data)
|
||||
} else {
|
||||
$zalsa::AsId::as_id(&($($input_id),*))
|
||||
}
|
||||
};
|
||||
$zalsa::gate_accumulated! {
|
||||
pub fn accumulated<$db_lt, A: salsa::Accumulator>(
|
||||
$db: &$db_lt dyn $Db,
|
||||
$($input_id: $interned_input_ty,)*
|
||||
) -> Vec<&$db_lt A> {
|
||||
use salsa::plumbing as $zalsa;
|
||||
let key = $zalsa::macro_if! {
|
||||
if $needs_interner {{
|
||||
let (zalsa, zalsa_local) = $db.zalsas();
|
||||
$Configuration::intern_ingredient($db).intern_id(zalsa, zalsa_local, ($($input_id),*), |_, data| data)
|
||||
}} else {
|
||||
$zalsa::AsId::as_id(&($($input_id),*))
|
||||
}
|
||||
};
|
||||
|
||||
$Configuration::fn_ingredient($db).accumulated_by::<A>($db, key)
|
||||
$Configuration::fn_ingredient($db).accumulated_by::<A>($db, key)
|
||||
}
|
||||
}
|
||||
|
||||
$zalsa::macro_if! { $is_specifiable =>
|
||||
|
@ -372,20 +392,24 @@ macro_rules! setup_tracked_fn {
|
|||
}
|
||||
|
||||
$zalsa::attach($db, || {
|
||||
let (zalsa, zalsa_local) = $db.zalsas();
|
||||
let result = $zalsa::macro_if! {
|
||||
if $needs_interner {
|
||||
{
|
||||
let key = $Configuration::intern_ingredient($db).intern_id($db.as_dyn_database(), ($($input_id),*), |_, data| data);
|
||||
$Configuration::fn_ingredient($db).fetch($db, key)
|
||||
let key = $Configuration::intern_ingredient_(zalsa).intern_id(zalsa, zalsa_local, ($($input_id),*), |_, data| data);
|
||||
$Configuration::fn_ingredient_($db, zalsa).fetch($db, zalsa, zalsa_local, key)
|
||||
}
|
||||
} else {
|
||||
$Configuration::fn_ingredient($db).fetch($db, $zalsa::AsId::as_id(&($($input_id),*)))
|
||||
{
|
||||
$Configuration::fn_ingredient_($db, zalsa).fetch($db, zalsa, zalsa_local, $zalsa::AsId::as_id(&($($input_id),*)))
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
$zalsa::return_mode_expression!(($return_mode, __, __), $output_ty, result,)
|
||||
})
|
||||
}
|
||||
|
||||
// The struct needs be last in the macro expansion in order to make the tracked
|
||||
// function's ident be identified as a function, not a struct, during semantic highlighting.
|
||||
// for more details, see https://github.com/salsa-rs/salsa/pull/612.
|
||||
|
|
|
@ -104,11 +104,11 @@ macro_rules! setup_tracked_struct {
|
|||
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
|
||||
$vis struct $Struct<$db_lt>(
|
||||
salsa::Id,
|
||||
std::marker::PhantomData < & $db_lt salsa::plumbing::tracked_struct::Value < $Struct<'static> > >
|
||||
std::marker::PhantomData<fn() -> &$db_lt ()>
|
||||
);
|
||||
|
||||
#[allow(clippy::all)]
|
||||
#[allow(dead_code)]
|
||||
#[allow(clippy::all)]
|
||||
const _: () = {
|
||||
use salsa::plumbing as $zalsa;
|
||||
use $zalsa::tracked_struct as $zalsa_struct;
|
||||
|
@ -116,6 +116,15 @@ macro_rules! setup_tracked_struct {
|
|||
|
||||
type $Configuration = $Struct<'static>;
|
||||
|
||||
impl<$db_lt> $zalsa::HasJar for $Struct<$db_lt> {
|
||||
type Jar = $zalsa_struct::JarImpl<$Configuration>;
|
||||
const KIND: $zalsa::JarKind = $zalsa::JarKind::Struct;
|
||||
}
|
||||
|
||||
$zalsa::register_jar! {
|
||||
$zalsa::ErasedJar::erase::<$Struct<'static>>()
|
||||
}
|
||||
|
||||
impl $zalsa_struct::Configuration for $Configuration {
|
||||
const LOCATION: $zalsa::Location = $zalsa::Location {
|
||||
file: file!(),
|
||||
|
@ -187,9 +196,13 @@ macro_rules! setup_tracked_struct {
|
|||
static CACHE: $zalsa::IngredientCache<$zalsa_struct::IngredientImpl<$Configuration>> =
|
||||
$zalsa::IngredientCache::new();
|
||||
|
||||
CACHE.get_or_create(zalsa, || {
|
||||
zalsa.lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>().get_or_create()
|
||||
})
|
||||
// SAFETY: `lookup_jar_by_type` returns a valid ingredient index, and the only
|
||||
// ingredient created by our jar is the struct ingredient.
|
||||
unsafe {
|
||||
CACHE.get_or_create(zalsa, || {
|
||||
zalsa.lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>()
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -210,8 +223,8 @@ macro_rules! setup_tracked_struct {
|
|||
impl $zalsa::SalsaStructInDb for $Struct<'_> {
|
||||
type MemoIngredientMap = $zalsa::MemoIngredientSingletonIndex;
|
||||
|
||||
fn lookup_or_create_ingredient_index(aux: &$zalsa::Zalsa) -> $zalsa::IngredientIndices {
|
||||
aux.lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>().get_or_create().into()
|
||||
fn lookup_ingredient_index(aux: &$zalsa::Zalsa) -> $zalsa::IngredientIndices {
|
||||
aux.lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>().into()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
@ -222,6 +235,16 @@ macro_rules! setup_tracked_struct {
|
|||
$zalsa::None
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn memo_table(
|
||||
zalsa: &$zalsa::Zalsa,
|
||||
id: $zalsa::Id,
|
||||
current_revision: $zalsa::Revision,
|
||||
) -> $zalsa::MemoTableWithTypes<'_> {
|
||||
// SAFETY: Guaranteed by caller.
|
||||
unsafe { zalsa.table().memos::<$zalsa_struct::Value<$Configuration>>(id, current_revision) }
|
||||
}
|
||||
}
|
||||
|
||||
impl $zalsa::TrackedStructInDb for $Struct<'_> {
|
||||
|
@ -259,8 +282,9 @@ macro_rules! setup_tracked_struct {
|
|||
// FIXME(rust-lang/rust#65991): The `db` argument *should* have the type `dyn Database`
|
||||
$Db: ?Sized + $zalsa::Database,
|
||||
{
|
||||
$Configuration::ingredient(db.as_dyn_database()).new_struct(
|
||||
db.as_dyn_database(),
|
||||
let (zalsa, zalsa_local) = db.zalsas();
|
||||
$Configuration::ingredient_(zalsa).new_struct(
|
||||
zalsa,zalsa_local,
|
||||
($($field_id,)*)
|
||||
)
|
||||
}
|
||||
|
@ -272,8 +296,8 @@ macro_rules! setup_tracked_struct {
|
|||
// FIXME(rust-lang/rust#65991): The `db` argument *should* have the type `dyn Database`
|
||||
$Db: ?Sized + $zalsa::Database,
|
||||
{
|
||||
let db = db.as_dyn_database();
|
||||
let fields = $Configuration::ingredient(db).tracked_field(db, self, $relative_tracked_index);
|
||||
let (zalsa, zalsa_local) = db.zalsas();
|
||||
let fields = $Configuration::ingredient_(zalsa).tracked_field(zalsa, zalsa_local, self, $relative_tracked_index);
|
||||
$crate::return_mode_expression!(
|
||||
$tracked_option,
|
||||
$tracked_ty,
|
||||
|
@ -289,8 +313,8 @@ macro_rules! setup_tracked_struct {
|
|||
// FIXME(rust-lang/rust#65991): The `db` argument *should* have the type `dyn Database`
|
||||
$Db: ?Sized + $zalsa::Database,
|
||||
{
|
||||
let db = db.as_dyn_database();
|
||||
let fields = $Configuration::ingredient(db).untracked_field(db, self);
|
||||
let zalsa = db.zalsa();
|
||||
let fields = $Configuration::ingredient_(zalsa).untracked_field(zalsa, self);
|
||||
$crate::return_mode_expression!(
|
||||
$untracked_option,
|
||||
$untracked_ty,
|
||||
|
@ -312,7 +336,8 @@ macro_rules! setup_tracked_struct {
|
|||
$(for<$db_lt> $field_ty: std::fmt::Debug),*
|
||||
{
|
||||
$zalsa::with_attached_database(|db| {
|
||||
let fields = $Configuration::ingredient(db).leak_fields(db, this);
|
||||
let zalsa = db.zalsa();
|
||||
let fields = $Configuration::ingredient_(zalsa).leak_fields(zalsa, this);
|
||||
let mut f = f.debug_struct(stringify!($Struct));
|
||||
let f = f.field("[salsa id]", &$zalsa::AsId::as_id(&this));
|
||||
$(
|
||||
|
|
|
@ -14,5 +14,5 @@ proc-macro = true
|
|||
[dependencies]
|
||||
proc-macro2 = "1.0"
|
||||
quote = "1.0"
|
||||
syn = { version = "2.0.101", features = ["full", "visit-mut"] }
|
||||
syn = { version = "2.0.104", features = ["full", "visit-mut"] }
|
||||
synstructure = "0.13.2"
|
||||
|
|
|
@ -110,18 +110,14 @@ impl DbMacro {
|
|||
let trait_name = &input.ident;
|
||||
input.items.push(parse_quote! {
|
||||
#[doc(hidden)]
|
||||
fn zalsa_register_downcaster(&self);
|
||||
fn zalsa_register_downcaster(&self) -> &salsa::plumbing::DatabaseDownCaster<dyn #trait_name>;
|
||||
});
|
||||
|
||||
let comment = format!(" Downcast a [`dyn Database`] to a [`dyn {trait_name}`]");
|
||||
let comment = format!(" downcast `Self` to a [`dyn {trait_name}`]");
|
||||
input.items.push(parse_quote! {
|
||||
#[doc = #comment]
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The input database must be of type `Self`.
|
||||
#[doc(hidden)]
|
||||
unsafe fn downcast(db: &dyn salsa::plumbing::Database) -> &dyn #trait_name where Self: Sized;
|
||||
fn downcast(&self) -> &dyn #trait_name where Self: Sized;
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
@ -135,19 +131,20 @@ impl DbMacro {
|
|||
};
|
||||
|
||||
input.items.push(parse_quote! {
|
||||
#[cold]
|
||||
#[inline(never)]
|
||||
#[doc(hidden)]
|
||||
#[inline(always)]
|
||||
fn zalsa_register_downcaster(&self) {
|
||||
salsa::plumbing::views(self).add(<Self as #TraitPath>::downcast);
|
||||
fn zalsa_register_downcaster(&self) -> &salsa::plumbing::DatabaseDownCaster<dyn #TraitPath> {
|
||||
salsa::plumbing::views(self).add::<Self, dyn #TraitPath>(unsafe {
|
||||
::std::mem::transmute(<Self as #TraitPath>::downcast as fn(_) -> _)
|
||||
})
|
||||
}
|
||||
});
|
||||
input.items.push(parse_quote! {
|
||||
#[doc(hidden)]
|
||||
#[inline(always)]
|
||||
unsafe fn downcast(db: &dyn salsa::plumbing::Database) -> &dyn #TraitPath where Self: Sized {
|
||||
debug_assert_eq!(db.type_id(), ::core::any::TypeId::of::<Self>());
|
||||
// SAFETY: The input database must be of type `Self`.
|
||||
unsafe { &*salsa::plumbing::transmute_data_ptr::<dyn salsa::plumbing::Database, Self>(db) }
|
||||
fn downcast(&self) -> &dyn #TraitPath where Self: Sized {
|
||||
self
|
||||
}
|
||||
});
|
||||
Ok(())
|
||||
|
|
|
@ -15,7 +15,7 @@ pub fn input_ids(hygiene: &Hygiene, sig: &syn::Signature, skip: usize) -> Vec<sy
|
|||
}
|
||||
}
|
||||
|
||||
hygiene.ident(&format!("input{index}"))
|
||||
hygiene.ident(format!("input{index}"))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
|
|
@ -50,10 +50,10 @@ impl Hygiene {
|
|||
/// Generates an identifier similar to `text` but
|
||||
/// distinct from any identifiers that appear in the user's
|
||||
/// code.
|
||||
pub(crate) fn ident(&self, text: &str) -> syn::Ident {
|
||||
pub(crate) fn ident(&self, text: impl AsRef<str>) -> syn::Ident {
|
||||
// Make the default be `foo_` rather than `foo` -- this helps detect
|
||||
// cases where people wrote `foo` instead of `#foo` or `$foo` in the generated code.
|
||||
let mut buffer = format!("{text}_");
|
||||
let mut buffer = format!("{}_", text.as_ref());
|
||||
|
||||
while self.user_tokens.contains(&buffer) {
|
||||
buffer.push('_');
|
||||
|
@ -61,4 +61,12 @@ impl Hygiene {
|
|||
|
||||
syn::Ident::new(&buffer, proc_macro2::Span::call_site())
|
||||
}
|
||||
|
||||
/// Generates an identifier similar to `text` but distinct from any identifiers
|
||||
/// that appear in the user's code.
|
||||
///
|
||||
/// The identifier must be unique relative to the `scope` identifier.
|
||||
pub(crate) fn scoped_ident(&self, scope: &syn::Ident, text: &str) -> syn::Ident {
|
||||
self.ident(format!("{scope}_{text}"))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -527,7 +527,7 @@ impl<A: AllowedOptions> quote::ToTokens for Options<A> {
|
|||
tokens.extend(quote::quote! { revisions = #revisions, });
|
||||
}
|
||||
if let Some(heap_size_fn) = heap_size_fn {
|
||||
tokens.extend(quote::quote! { heap_size_fn = #heap_size_fn, });
|
||||
tokens.extend(quote::quote! { heap_size = #heap_size_fn, });
|
||||
}
|
||||
if let Some(self_ty) = self_ty {
|
||||
tokens.extend(quote::quote! { self_ty = #self_ty, });
|
||||
|
|
|
@ -72,8 +72,8 @@ fn enum_impl(enum_item: syn::ItemEnum) -> syn::Result<TokenStream> {
|
|||
type MemoIngredientMap = zalsa::MemoIngredientIndices;
|
||||
|
||||
#[inline]
|
||||
fn lookup_or_create_ingredient_index(__zalsa: &zalsa::Zalsa) -> zalsa::IngredientIndices {
|
||||
zalsa::IngredientIndices::merge([ #( <#variant_types as zalsa::SalsaStructInDb>::lookup_or_create_ingredient_index(__zalsa) ),* ])
|
||||
fn lookup_ingredient_index(__zalsa: &zalsa::Zalsa) -> zalsa::IngredientIndices {
|
||||
zalsa::IngredientIndices::merge([ #( <#variant_types as zalsa::SalsaStructInDb>::lookup_ingredient_index(__zalsa) ),* ])
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
@ -89,6 +89,19 @@ fn enum_impl(enum_item: syn::ItemEnum) -> syn::Result<TokenStream> {
|
|||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn memo_table(
|
||||
zalsa: &zalsa::Zalsa,
|
||||
id: zalsa::Id,
|
||||
current_revision: zalsa::Revision,
|
||||
) -> zalsa::MemoTableWithTypes<'_> {
|
||||
// Note that we need to use `dyn_memos` here, as the `Id` could map to any variant
|
||||
// of the supertype enum.
|
||||
//
|
||||
// SAFETY: Guaranteed by caller.
|
||||
unsafe { zalsa.table().dyn_memos(id, current_revision) }
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -132,10 +132,10 @@ impl Macro {
|
|||
inner_fn.sig.ident = self.hygiene.ident("inner");
|
||||
|
||||
let zalsa = self.hygiene.ident("zalsa");
|
||||
let Configuration = self.hygiene.ident("Configuration");
|
||||
let InternedData = self.hygiene.ident("InternedData");
|
||||
let FN_CACHE = self.hygiene.ident("FN_CACHE");
|
||||
let INTERN_CACHE = self.hygiene.ident("INTERN_CACHE");
|
||||
let Configuration = self.hygiene.scoped_ident(fn_name, "Configuration");
|
||||
let InternedData = self.hygiene.scoped_ident(fn_name, "InternedData");
|
||||
let FN_CACHE = self.hygiene.scoped_ident(fn_name, "FN_CACHE");
|
||||
let INTERN_CACHE = self.hygiene.scoped_ident(fn_name, "INTERN_CACHE");
|
||||
let inner = &inner_fn.sig.ident;
|
||||
|
||||
let function_type = function_type(&item);
|
||||
|
|
|
@ -99,7 +99,7 @@ impl Macro {
|
|||
});
|
||||
|
||||
let InnerTrait = self.hygiene.ident("InnerTrait");
|
||||
let inner_fn_name = self.hygiene.ident(&fn_item.sig.ident.to_string());
|
||||
let inner_fn_name = self.hygiene.ident(fn_item.sig.ident.to_string());
|
||||
|
||||
let AssociatedFunctionArguments {
|
||||
self_token,
|
||||
|
|
|
@ -7,10 +7,10 @@ use std::panic::UnwindSafe;
|
|||
|
||||
use accumulated::{Accumulated, AnyAccumulated};
|
||||
|
||||
use crate::cycle::CycleHeads;
|
||||
use crate::cycle::CycleHeadKeys;
|
||||
use crate::function::VerifyResult;
|
||||
use crate::ingredient::{Ingredient, Jar};
|
||||
use crate::plumbing::{IngredientIndices, ZalsaLocal};
|
||||
use crate::plumbing::ZalsaLocal;
|
||||
use crate::sync::Arc;
|
||||
use crate::table::memo::MemoTableTypes;
|
||||
use crate::zalsa::{IngredientIndex, Zalsa};
|
||||
|
@ -44,9 +44,8 @@ impl<A: Accumulator> Default for JarImpl<A> {
|
|||
|
||||
impl<A: Accumulator> Jar for JarImpl<A> {
|
||||
fn create_ingredients(
|
||||
_zalsa: &Zalsa,
|
||||
_zalsa: &mut Zalsa,
|
||||
first_index: IngredientIndex,
|
||||
_dependencies: IngredientIndices,
|
||||
) -> Vec<Box<dyn Ingredient>> {
|
||||
vec![Box::new(<IngredientImpl<A>>::new(first_index))]
|
||||
}
|
||||
|
@ -64,7 +63,7 @@ pub struct IngredientImpl<A: Accumulator> {
|
|||
impl<A: Accumulator> IngredientImpl<A> {
|
||||
/// Find the accumulator ingredient for `A` in the database, if any.
|
||||
pub fn from_zalsa(zalsa: &Zalsa) -> Option<&Self> {
|
||||
let index = zalsa.lookup_jar_by_type::<JarImpl<A>>().get_or_create();
|
||||
let index = zalsa.lookup_jar_by_type::<JarImpl<A>>();
|
||||
let ingredient = zalsa.lookup_ingredient(index).assert_type::<Self>();
|
||||
Some(ingredient)
|
||||
}
|
||||
|
@ -103,10 +102,11 @@ impl<A: Accumulator> Ingredient for IngredientImpl<A> {
|
|||
|
||||
unsafe fn maybe_changed_after(
|
||||
&self,
|
||||
_db: &dyn Database,
|
||||
_zalsa: &crate::zalsa::Zalsa,
|
||||
_db: crate::database::RawDatabase<'_>,
|
||||
_input: Id,
|
||||
_revision: Revision,
|
||||
_cycle_heads: &mut CycleHeads,
|
||||
_cycle_heads: &mut CycleHeadKeys,
|
||||
) -> VerifyResult {
|
||||
panic!("nothing should ever depend on an accumulator directly")
|
||||
}
|
||||
|
@ -115,7 +115,11 @@ impl<A: Accumulator> Ingredient for IngredientImpl<A> {
|
|||
A::DEBUG_NAME
|
||||
}
|
||||
|
||||
fn memo_table_types(&self) -> Arc<MemoTableTypes> {
|
||||
fn memo_table_types(&self) -> &Arc<MemoTableTypes> {
|
||||
unreachable!("accumulator does not allocate pages")
|
||||
}
|
||||
|
||||
fn memo_table_types_mut(&mut self) -> &mut Arc<MemoTableTypes> {
|
||||
unreachable!("accumulator does not allocate pages")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
use std::{fmt, mem, ops};
|
||||
|
||||
use crate::accumulator::accumulated_map::{
|
||||
AccumulatedMap, AtomicInputAccumulatedValues, InputAccumulatedValues,
|
||||
#[cfg(feature = "accumulator")]
|
||||
use crate::accumulator::{
|
||||
accumulated_map::{AccumulatedMap, AtomicInputAccumulatedValues, InputAccumulatedValues},
|
||||
Accumulator,
|
||||
};
|
||||
use crate::cycle::{CycleHeads, IterationCount};
|
||||
use crate::durability::Durability;
|
||||
|
@ -11,7 +13,7 @@ use crate::runtime::Stamp;
|
|||
use crate::sync::atomic::AtomicBool;
|
||||
use crate::tracked_struct::{Disambiguator, DisambiguatorMap, IdentityHash, IdentityMap};
|
||||
use crate::zalsa_local::{QueryEdge, QueryOrigin, QueryRevisions, QueryRevisionsExtra};
|
||||
use crate::{Accumulator, IngredientIndex, Revision};
|
||||
use crate::Revision;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct ActiveQuery {
|
||||
|
@ -51,10 +53,12 @@ pub(crate) struct ActiveQuery {
|
|||
|
||||
/// Stores the values accumulated to the given ingredient.
|
||||
/// The type of accumulated value is erased but known to the ingredient.
|
||||
#[cfg(feature = "accumulator")]
|
||||
accumulated: AccumulatedMap,
|
||||
|
||||
/// [`InputAccumulatedValues::Empty`] if any input read during the query's execution
|
||||
/// has any accumulated values.
|
||||
#[cfg(feature = "accumulator")]
|
||||
accumulated_inputs: InputAccumulatedValues,
|
||||
|
||||
/// Provisional cycle results that this query depends on.
|
||||
|
@ -73,7 +77,7 @@ impl ActiveQuery {
|
|||
untracked_read: bool,
|
||||
) {
|
||||
assert!(self.input_outputs.is_empty());
|
||||
self.input_outputs = edges.iter().cloned().collect();
|
||||
self.input_outputs.extend(edges.iter().cloned());
|
||||
self.durability = self.durability.min(durability);
|
||||
self.changed_at = self.changed_at.max(changed_at);
|
||||
self.untracked_read |= untracked_read;
|
||||
|
@ -84,18 +88,21 @@ impl ActiveQuery {
|
|||
input: DatabaseKeyIndex,
|
||||
durability: Durability,
|
||||
changed_at: Revision,
|
||||
has_accumulated: bool,
|
||||
accumulated_inputs: &AtomicInputAccumulatedValues,
|
||||
cycle_heads: &CycleHeads,
|
||||
#[cfg(feature = "accumulator")] has_accumulated: bool,
|
||||
#[cfg(feature = "accumulator")] accumulated_inputs: &AtomicInputAccumulatedValues,
|
||||
) {
|
||||
self.durability = self.durability.min(durability);
|
||||
self.changed_at = self.changed_at.max(changed_at);
|
||||
self.input_outputs.insert(QueryEdge::input(input));
|
||||
self.accumulated_inputs = self.accumulated_inputs.or_else(|| match has_accumulated {
|
||||
true => InputAccumulatedValues::Any,
|
||||
false => accumulated_inputs.load(),
|
||||
});
|
||||
self.cycle_heads.extend(cycle_heads);
|
||||
#[cfg(feature = "accumulator")]
|
||||
{
|
||||
self.accumulated_inputs = self.accumulated_inputs.or_else(|| match has_accumulated {
|
||||
true => InputAccumulatedValues::Any,
|
||||
false => accumulated_inputs.load(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn add_read_simple(
|
||||
|
@ -121,7 +128,8 @@ impl ActiveQuery {
|
|||
self.changed_at = self.changed_at.max(revision);
|
||||
}
|
||||
|
||||
pub(super) fn accumulate(&mut self, index: IngredientIndex, value: impl Accumulator) {
|
||||
#[cfg(feature = "accumulator")]
|
||||
pub(super) fn accumulate(&mut self, index: crate::IngredientIndex, value: impl Accumulator) {
|
||||
self.accumulated.accumulate(index, value);
|
||||
}
|
||||
|
||||
|
@ -169,10 +177,12 @@ impl ActiveQuery {
|
|||
untracked_read: false,
|
||||
disambiguator_map: Default::default(),
|
||||
tracked_struct_ids: Default::default(),
|
||||
accumulated: Default::default(),
|
||||
accumulated_inputs: Default::default(),
|
||||
cycle_heads: Default::default(),
|
||||
iteration_count,
|
||||
#[cfg(feature = "accumulator")]
|
||||
accumulated: Default::default(),
|
||||
#[cfg(feature = "accumulator")]
|
||||
accumulated_inputs: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -185,10 +195,12 @@ impl ActiveQuery {
|
|||
untracked_read,
|
||||
ref mut disambiguator_map,
|
||||
ref mut tracked_struct_ids,
|
||||
ref mut accumulated,
|
||||
accumulated_inputs,
|
||||
ref mut cycle_heads,
|
||||
iteration_count,
|
||||
#[cfg(feature = "accumulator")]
|
||||
ref mut accumulated,
|
||||
#[cfg(feature = "accumulator")]
|
||||
accumulated_inputs,
|
||||
} = self;
|
||||
|
||||
let origin = if untracked_read {
|
||||
|
@ -198,19 +210,22 @@ impl ActiveQuery {
|
|||
};
|
||||
disambiguator_map.clear();
|
||||
|
||||
#[cfg(feature = "accumulator")]
|
||||
let accumulated_inputs = AtomicInputAccumulatedValues::new(accumulated_inputs);
|
||||
let verified_final = cycle_heads.is_empty();
|
||||
let extra = QueryRevisionsExtra::new(
|
||||
#[cfg(feature = "accumulator")]
|
||||
mem::take(accumulated),
|
||||
mem::take(tracked_struct_ids),
|
||||
mem::take(cycle_heads),
|
||||
iteration_count,
|
||||
);
|
||||
let accumulated_inputs = AtomicInputAccumulatedValues::new(accumulated_inputs);
|
||||
|
||||
QueryRevisions {
|
||||
changed_at,
|
||||
durability,
|
||||
origin,
|
||||
#[cfg(feature = "accumulator")]
|
||||
accumulated_inputs,
|
||||
verified_final: AtomicBool::new(verified_final),
|
||||
extra,
|
||||
|
@ -226,17 +241,20 @@ impl ActiveQuery {
|
|||
untracked_read: _,
|
||||
disambiguator_map,
|
||||
tracked_struct_ids,
|
||||
accumulated,
|
||||
accumulated_inputs: _,
|
||||
cycle_heads,
|
||||
iteration_count,
|
||||
#[cfg(feature = "accumulator")]
|
||||
accumulated,
|
||||
#[cfg(feature = "accumulator")]
|
||||
accumulated_inputs: _,
|
||||
} = self;
|
||||
input_outputs.clear();
|
||||
disambiguator_map.clear();
|
||||
tracked_struct_ids.clear();
|
||||
accumulated.clear();
|
||||
*cycle_heads = Default::default();
|
||||
*iteration_count = IterationCount::initial();
|
||||
#[cfg(feature = "accumulator")]
|
||||
accumulated.clear();
|
||||
}
|
||||
|
||||
fn reset_for(
|
||||
|
@ -252,16 +270,17 @@ impl ActiveQuery {
|
|||
untracked_read,
|
||||
disambiguator_map,
|
||||
tracked_struct_ids,
|
||||
accumulated,
|
||||
accumulated_inputs,
|
||||
cycle_heads,
|
||||
iteration_count,
|
||||
#[cfg(feature = "accumulator")]
|
||||
accumulated,
|
||||
#[cfg(feature = "accumulator")]
|
||||
accumulated_inputs,
|
||||
} = self;
|
||||
*database_key_index = new_database_key_index;
|
||||
*durability = Durability::MAX;
|
||||
*changed_at = Revision::start();
|
||||
*untracked_read = false;
|
||||
*accumulated_inputs = Default::default();
|
||||
*iteration_count = new_iteration_count;
|
||||
debug_assert!(
|
||||
input_outputs.is_empty(),
|
||||
|
@ -279,10 +298,14 @@ impl ActiveQuery {
|
|||
cycle_heads.is_empty(),
|
||||
"`ActiveQuery::clear` or `ActiveQuery::into_revisions` should've been called"
|
||||
);
|
||||
debug_assert!(
|
||||
accumulated.is_empty(),
|
||||
"`ActiveQuery::clear` or `ActiveQuery::into_revisions` should've been called"
|
||||
);
|
||||
#[cfg(feature = "accumulator")]
|
||||
{
|
||||
*accumulated_inputs = Default::default();
|
||||
debug_assert!(
|
||||
accumulated.is_empty(),
|
||||
"`ActiveQuery::clear` or `ActiveQuery::into_revisions` should've been called"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
47
src/cycle.rs
47
src/cycle.rs
|
@ -178,22 +178,6 @@ impl CycleHeads {
|
|||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn push_initial(&mut self, database_key_index: DatabaseKeyIndex) {
|
||||
if let Some(existing) = self
|
||||
.0
|
||||
.iter()
|
||||
.find(|candidate| candidate.database_key_index == database_key_index)
|
||||
{
|
||||
assert_eq!(existing.iteration_count, IterationCount::initial());
|
||||
} else {
|
||||
self.0.push(CycleHead {
|
||||
database_key_index,
|
||||
iteration_count: IterationCount::initial(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn extend(&mut self, other: &Self) {
|
||||
self.0.reserve(other.0.len());
|
||||
|
@ -247,6 +231,37 @@ pub(crate) fn empty_cycle_heads() -> &'static CycleHeads {
|
|||
EMPTY_CYCLE_HEADS.get_or_init(|| CycleHeads(ThinVec::new()))
|
||||
}
|
||||
|
||||
/// Set of cycle head database keys.
|
||||
///
|
||||
/// Unlike [`CycleHeads`], this type doesn't track the iteration count
|
||||
/// of each cycle head.
|
||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||
pub struct CycleHeadKeys(Vec<DatabaseKeyIndex>);
|
||||
|
||||
impl CycleHeadKeys {
|
||||
pub(crate) fn new() -> Self {
|
||||
Self(Vec::new())
|
||||
}
|
||||
|
||||
pub(crate) fn insert(&mut self, database_key_index: DatabaseKeyIndex) {
|
||||
if !self.0.contains(&database_key_index) {
|
||||
self.0.push(database_key_index);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn remove(&mut self, value: &DatabaseKeyIndex) -> bool {
|
||||
let found = self.0.iter().position(|&head| head == *value);
|
||||
let Some(found) = found else { return false };
|
||||
|
||||
self.0.swap_remove(found);
|
||||
true
|
||||
}
|
||||
|
||||
pub(crate) fn is_empty(&self) -> bool {
|
||||
self.0.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum ProvisionalStatus {
|
||||
Provisional { iteration: IterationCount },
|
||||
|
|
|
@ -1,12 +1,39 @@
|
|||
use std::any::Any;
|
||||
use std::borrow::Cow;
|
||||
use std::ptr::NonNull;
|
||||
|
||||
use crate::views::DatabaseDownCaster;
|
||||
use crate::zalsa::{IngredientIndex, ZalsaDatabase};
|
||||
use crate::{Durability, Revision};
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct RawDatabase<'db> {
|
||||
pub(crate) ptr: NonNull<()>,
|
||||
_marker: std::marker::PhantomData<&'db dyn Database>,
|
||||
}
|
||||
|
||||
impl<'db, Db: Database + ?Sized> From<&'db Db> for RawDatabase<'db> {
|
||||
#[inline]
|
||||
fn from(db: &'db Db) -> Self {
|
||||
RawDatabase {
|
||||
ptr: NonNull::from(db).cast(),
|
||||
_marker: std::marker::PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'db, Db: Database + ?Sized> From<&'db mut Db> for RawDatabase<'db> {
|
||||
#[inline]
|
||||
fn from(db: &'db mut Db) -> Self {
|
||||
RawDatabase {
|
||||
ptr: NonNull::from(db).cast(),
|
||||
_marker: std::marker::PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The trait implemented by all Salsa databases.
|
||||
/// You can create your own subtraits of this trait using the `#[salsa::db]`(`crate::db`) procedural macro.
|
||||
pub trait Database: Send + AsDynDatabase + Any + ZalsaDatabase {
|
||||
pub trait Database: Send + ZalsaDatabase + AsDynDatabase {
|
||||
/// Enforces current LRU limits, evicting entries if necessary.
|
||||
///
|
||||
/// **WARNING:** Just like an ordinary write, this method triggers
|
||||
|
@ -32,6 +59,14 @@ pub trait Database: Send + AsDynDatabase + Any + ZalsaDatabase {
|
|||
zalsa_mut.runtime_mut().report_tracked_write(durability);
|
||||
}
|
||||
|
||||
/// This method triggers cancellation.
|
||||
/// If you invoke it while a snapshot exists, it
|
||||
/// will block until that snapshot is dropped -- if that snapshot
|
||||
/// is owned by the current thread, this could trigger deadlock.
|
||||
fn trigger_cancellation(&mut self) {
|
||||
let _ = self.zalsa_mut();
|
||||
}
|
||||
|
||||
/// Reports that the query depends on some state unknown to salsa.
|
||||
///
|
||||
/// Queries which report untracked reads will be re-executed in the next
|
||||
|
@ -80,29 +115,30 @@ pub trait Database: Send + AsDynDatabase + Any + ZalsaDatabase {
|
|||
crate::attach::attach(self, || op(self))
|
||||
}
|
||||
|
||||
#[cold]
|
||||
#[inline(never)]
|
||||
#[doc(hidden)]
|
||||
#[inline(always)]
|
||||
fn zalsa_register_downcaster(&self) {
|
||||
fn zalsa_register_downcaster(&self) -> &DatabaseDownCaster<dyn Database> {
|
||||
self.zalsa().views().downcaster_for::<dyn Database>()
|
||||
// The no-op downcaster is special cased in view caster construction.
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[inline(always)]
|
||||
unsafe fn downcast(db: &dyn Database) -> &dyn Database
|
||||
fn downcast(&self) -> &dyn Database
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
// No-op
|
||||
db
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// Upcast to a `dyn Database`.
|
||||
///
|
||||
/// Only required because upcasts not yet stabilized (*grr*).
|
||||
/// Only required because upcasting does not work for unsized generic parameters.
|
||||
pub trait AsDynDatabase {
|
||||
fn as_dyn_database(&self) -> &dyn Database;
|
||||
fn as_dyn_database_mut(&mut self) -> &mut dyn Database;
|
||||
}
|
||||
|
||||
impl<T: Database> AsDynDatabase for T {
|
||||
|
@ -110,30 +146,12 @@ impl<T: Database> AsDynDatabase for T {
|
|||
fn as_dyn_database(&self) -> &dyn Database {
|
||||
self
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn as_dyn_database_mut(&mut self) -> &mut dyn Database {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
pub fn current_revision<Db: ?Sized + Database>(db: &Db) -> Revision {
|
||||
db.zalsa().current_revision()
|
||||
}
|
||||
|
||||
impl dyn Database {
|
||||
/// Upcasts `self` to the given view.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// If the view has not been added to the database (see [`crate::views::Views`]).
|
||||
#[track_caller]
|
||||
pub fn as_view<DbView: ?Sized + Database>(&self) -> &DbView {
|
||||
let views = self.zalsa().views();
|
||||
views.downcaster_for().downcast(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "salsa_unstable")]
|
||||
pub use memory_usage::IngredientInfo;
|
||||
|
||||
|
|
|
@ -16,7 +16,7 @@ impl Default for DatabaseImpl {
|
|||
// Default behavior: tracing debug log the event.
|
||||
storage: Storage::new(if tracing::enabled!(Level::DEBUG) {
|
||||
Some(Box::new(|event| {
|
||||
tracing::debug!("salsa_event({:?})", event)
|
||||
crate::tracing::debug!("salsa_event({:?})", event)
|
||||
}))
|
||||
} else {
|
||||
None
|
||||
|
|
|
@ -3,12 +3,14 @@ use std::any::Any;
|
|||
use std::fmt;
|
||||
use std::ptr::NonNull;
|
||||
use std::sync::atomic::Ordering;
|
||||
use std::sync::OnceLock;
|
||||
pub(crate) use sync::SyncGuard;
|
||||
|
||||
use crate::accumulator::accumulated_map::{AccumulatedMap, InputAccumulatedValues};
|
||||
use crate::cycle::{
|
||||
empty_cycle_heads, CycleHeads, CycleRecoveryAction, CycleRecoveryStrategy, ProvisionalStatus,
|
||||
empty_cycle_heads, CycleHeadKeys, CycleHeads, CycleRecoveryAction, CycleRecoveryStrategy,
|
||||
ProvisionalStatus,
|
||||
};
|
||||
use crate::database::RawDatabase;
|
||||
use crate::function::delete::DeletedEntries;
|
||||
use crate::function::sync::{ClaimResult, SyncTable};
|
||||
use crate::ingredient::{Ingredient, WaitForResult};
|
||||
|
@ -21,8 +23,9 @@ use crate::table::Table;
|
|||
use crate::views::DatabaseDownCaster;
|
||||
use crate::zalsa::{IngredientIndex, MemoIngredientIndex, Zalsa};
|
||||
use crate::zalsa_local::QueryOriginRef;
|
||||
use crate::{Database, Id, Revision};
|
||||
use crate::{Id, Revision};
|
||||
|
||||
#[cfg(feature = "accumulator")]
|
||||
mod accumulated;
|
||||
mod backdate;
|
||||
mod delete;
|
||||
|
@ -67,10 +70,9 @@ pub trait Configuration: Any {
|
|||
/// This invokes user code in form of the `Eq` impl.
|
||||
fn values_equal<'db>(old_value: &Self::Output<'db>, new_value: &Self::Output<'db>) -> bool;
|
||||
|
||||
// FIXME: This should take a `&Zalsa`
|
||||
/// Convert from the id used internally to the value that execute is expecting.
|
||||
/// This is a no-op if the input to the function is a salsa struct.
|
||||
fn id_to_input(db: &Self::DbView, key: Id) -> Self::Input<'_>;
|
||||
fn id_to_input(zalsa: &Zalsa, key: Id) -> Self::Input<'_>;
|
||||
|
||||
/// Returns the size of any heap allocations in the output value, in bytes.
|
||||
fn heap_size(_value: &Self::Output<'_>) -> usize {
|
||||
|
@ -123,13 +125,13 @@ pub struct IngredientImpl<C: Configuration> {
|
|||
/// Used to find memos to throw out when we have too many memoized values.
|
||||
lru: lru::Lru,
|
||||
|
||||
/// A downcaster from `dyn Database` to `C::DbView`.
|
||||
/// An downcaster to `C::DbView`.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The supplied database must be be the same as the database used to construct the [`Views`]
|
||||
/// instances that this downcaster was derived from.
|
||||
view_caster: DatabaseDownCaster<C::DbView>,
|
||||
view_caster: OnceLock<DatabaseDownCaster<C::DbView>>,
|
||||
|
||||
sync_table: SyncTable,
|
||||
|
||||
|
@ -156,18 +158,30 @@ where
|
|||
index: IngredientIndex,
|
||||
memo_ingredient_indices: <C::SalsaStruct<'static> as SalsaStructInDb>::MemoIngredientMap,
|
||||
lru: usize,
|
||||
view_caster: DatabaseDownCaster<C::DbView>,
|
||||
) -> Self {
|
||||
Self {
|
||||
index,
|
||||
memo_ingredient_indices,
|
||||
lru: lru::Lru::new(lru),
|
||||
deleted_entries: Default::default(),
|
||||
view_caster,
|
||||
view_caster: OnceLock::new(),
|
||||
sync_table: SyncTable::new(index),
|
||||
}
|
||||
}
|
||||
|
||||
/// Set the view-caster for this tracked function ingredient, if it has
|
||||
/// not already been initialized.
|
||||
#[inline]
|
||||
pub fn get_or_init(
|
||||
&self,
|
||||
view_caster: impl FnOnce() -> DatabaseDownCaster<C::DbView>,
|
||||
) -> &Self {
|
||||
// Note that we must set this lazily as we don't have access to the database
|
||||
// type when ingredients are registered into the `Zalsa`.
|
||||
self.view_caster.get_or_init(view_caster);
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn database_key_index(&self, key: Id) -> DatabaseKeyIndex {
|
||||
DatabaseKeyIndex::new(self.index, key)
|
||||
|
@ -226,6 +240,12 @@ where
|
|||
fn memo_ingredient_index(&self, zalsa: &Zalsa, id: Id) -> MemoIngredientIndex {
|
||||
self.memo_ingredient_indices.get_zalsa_id(zalsa, id)
|
||||
}
|
||||
|
||||
fn view_caster(&self) -> &DatabaseDownCaster<C::DbView> {
|
||||
self.view_caster
|
||||
.get()
|
||||
.expect("tracked function ingredients cannot be accessed before calling `init`")
|
||||
}
|
||||
}
|
||||
|
||||
impl<C> Ingredient for IngredientImpl<C>
|
||||
|
@ -242,13 +262,14 @@ where
|
|||
|
||||
unsafe fn maybe_changed_after(
|
||||
&self,
|
||||
db: &dyn Database,
|
||||
_zalsa: &crate::zalsa::Zalsa,
|
||||
db: RawDatabase<'_>,
|
||||
input: Id,
|
||||
revision: Revision,
|
||||
cycle_heads: &mut CycleHeads,
|
||||
cycle_heads: &mut CycleHeadKeys,
|
||||
) -> VerifyResult {
|
||||
// SAFETY: The `db` belongs to the ingredient as per caller invariant
|
||||
let db = unsafe { self.view_caster.downcast_unchecked(db) };
|
||||
let db = unsafe { self.view_caster().downcast_unchecked(db) };
|
||||
self.maybe_changed_after(db, input, revision, cycle_heads)
|
||||
}
|
||||
|
||||
|
@ -339,7 +360,11 @@ where
|
|||
C::DEBUG_NAME
|
||||
}
|
||||
|
||||
fn memo_table_types(&self) -> Arc<MemoTableTypes> {
|
||||
fn memo_table_types(&self) -> &Arc<MemoTableTypes> {
|
||||
unreachable!("function does not allocate pages")
|
||||
}
|
||||
|
||||
fn memo_table_types_mut(&mut self) -> &mut Arc<MemoTableTypes> {
|
||||
unreachable!("function does not allocate pages")
|
||||
}
|
||||
|
||||
|
@ -347,12 +372,17 @@ where
|
|||
C::CYCLE_STRATEGY
|
||||
}
|
||||
|
||||
fn accumulated<'db>(
|
||||
#[cfg(feature = "accumulator")]
|
||||
unsafe fn accumulated<'db>(
|
||||
&'db self,
|
||||
db: &'db dyn Database,
|
||||
db: RawDatabase<'db>,
|
||||
key_index: Id,
|
||||
) -> (Option<&'db AccumulatedMap>, InputAccumulatedValues) {
|
||||
let db = self.view_caster.downcast(db);
|
||||
) -> (
|
||||
Option<&'db crate::accumulator::accumulated_map::AccumulatedMap>,
|
||||
crate::accumulator::accumulated_map::InputAccumulatedValues,
|
||||
) {
|
||||
// SAFETY: The `db` belongs to the ingredient as per caller invariant
|
||||
let db = unsafe { self.view_caster().downcast_unchecked(db) };
|
||||
self.accumulated_map(db, key_index)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ use crate::function::{Configuration, IngredientImpl};
|
|||
use crate::hash::FxHashSet;
|
||||
use crate::zalsa::ZalsaDatabase;
|
||||
use crate::zalsa_local::QueryOriginRef;
|
||||
use crate::{AsDynDatabase, DatabaseKeyIndex, Id};
|
||||
use crate::{DatabaseKeyIndex, Id};
|
||||
|
||||
impl<C> IngredientImpl<C>
|
||||
where
|
||||
|
@ -37,9 +37,8 @@ where
|
|||
let mut output = vec![];
|
||||
|
||||
// First ensure the result is up to date
|
||||
self.fetch(db, key);
|
||||
self.fetch(db, zalsa, zalsa_local, key);
|
||||
|
||||
let db = db.as_dyn_database();
|
||||
let db_key = self.database_key_index(key);
|
||||
let mut visited: FxHashSet<DatabaseKeyIndex> = FxHashSet::default();
|
||||
let mut stack: Vec<DatabaseKeyIndex> = vec![db_key];
|
||||
|
@ -54,7 +53,9 @@ where
|
|||
|
||||
let ingredient = zalsa.lookup_ingredient(k.ingredient_index());
|
||||
// Extend `output` with any values accumulated by `k`.
|
||||
let (accumulated_map, input) = ingredient.accumulated(db, k.key_index());
|
||||
// SAFETY: `db` owns the `ingredient`
|
||||
let (accumulated_map, input) =
|
||||
unsafe { ingredient.accumulated(db.into(), k.key_index()) };
|
||||
if let Some(accumulated_map) = accumulated_map {
|
||||
accumulated_map.extend_with_accumulated(accumulator.index(), &mut output);
|
||||
}
|
||||
|
|
|
@ -34,7 +34,7 @@ where
|
|||
if revisions.durability >= old_memo.revisions.durability
|
||||
&& C::values_equal(old_value, value)
|
||||
{
|
||||
tracing::debug!(
|
||||
crate::tracing::debug!(
|
||||
"{index:?} value is equal, back-dating to {:?}",
|
||||
old_memo.revisions.changed_at,
|
||||
);
|
||||
|
|
|
@ -4,7 +4,7 @@ use crate::function::{Configuration, IngredientImpl};
|
|||
use crate::sync::atomic::{AtomicBool, Ordering};
|
||||
use crate::zalsa::{MemoIngredientIndex, Zalsa, ZalsaDatabase};
|
||||
use crate::zalsa_local::{ActiveQueryGuard, QueryRevisions};
|
||||
use crate::{Event, EventKind, Id, Revision};
|
||||
use crate::{Event, EventKind, Id};
|
||||
|
||||
impl<C> IngredientImpl<C>
|
||||
where
|
||||
|
@ -29,7 +29,7 @@ where
|
|||
let database_key_index = active_query.database_key_index;
|
||||
let id = database_key_index.key_index();
|
||||
|
||||
tracing::info!("{:?}: executing query", database_key_index);
|
||||
crate::tracing::info!("{:?}: executing query", database_key_index);
|
||||
let zalsa = db.zalsa();
|
||||
|
||||
zalsa.event(&|| {
|
||||
|
@ -41,16 +41,11 @@ where
|
|||
|
||||
let (new_value, mut revisions) = match C::CYCLE_STRATEGY {
|
||||
CycleRecoveryStrategy::Panic => {
|
||||
Self::execute_query(db, active_query, opt_old_memo, zalsa.current_revision(), id)
|
||||
Self::execute_query(db, zalsa, active_query, opt_old_memo, id)
|
||||
}
|
||||
CycleRecoveryStrategy::FallbackImmediate => {
|
||||
let (mut new_value, mut revisions) = Self::execute_query(
|
||||
db,
|
||||
active_query,
|
||||
opt_old_memo,
|
||||
zalsa.current_revision(),
|
||||
id,
|
||||
);
|
||||
let (mut new_value, mut revisions) =
|
||||
Self::execute_query(db, zalsa, active_query, opt_old_memo, id);
|
||||
|
||||
if let Some(cycle_heads) = revisions.cycle_heads_mut() {
|
||||
// Did the new result we got depend on our own provisional value, in a cycle?
|
||||
|
@ -77,7 +72,7 @@ where
|
|||
let active_query = db
|
||||
.zalsa_local()
|
||||
.push_query(database_key_index, IterationCount::initial());
|
||||
new_value = C::cycle_initial(db, C::id_to_input(db, id));
|
||||
new_value = C::cycle_initial(db, C::id_to_input(zalsa, id));
|
||||
revisions = active_query.pop();
|
||||
// We need to set `cycle_heads` and `verified_final` because it needs to propagate to the callers.
|
||||
// When verifying this, we will see we have fallback and mark ourselves verified.
|
||||
|
@ -136,13 +131,8 @@ where
|
|||
let mut opt_last_provisional: Option<&Memo<'db, C>> = None;
|
||||
loop {
|
||||
let previous_memo = opt_last_provisional.or(opt_old_memo);
|
||||
let (mut new_value, mut revisions) = Self::execute_query(
|
||||
db,
|
||||
active_query,
|
||||
previous_memo,
|
||||
zalsa.current_revision(),
|
||||
id,
|
||||
);
|
||||
let (mut new_value, mut revisions) =
|
||||
Self::execute_query(db, zalsa, active_query, previous_memo, id);
|
||||
|
||||
// Did the new result we got depend on our own provisional value, in a cycle?
|
||||
if let Some(cycle_heads) = revisions
|
||||
|
@ -169,7 +159,7 @@ where
|
|||
};
|
||||
// SAFETY: The `LRU` does not run mid-execution, so the value remains filled
|
||||
let last_provisional_value = unsafe { last_provisional_value.unwrap_unchecked() };
|
||||
tracing::debug!(
|
||||
crate::tracing::debug!(
|
||||
"{database_key_index:?}: execute: \
|
||||
I am a cycle head, comparing last provisional value with new value"
|
||||
);
|
||||
|
@ -192,11 +182,11 @@ where
|
|||
db,
|
||||
&new_value,
|
||||
iteration_count.as_u32(),
|
||||
C::id_to_input(db, id),
|
||||
C::id_to_input(zalsa, id),
|
||||
) {
|
||||
crate::CycleRecoveryAction::Iterate => {}
|
||||
crate::CycleRecoveryAction::Fallback(fallback_value) => {
|
||||
tracing::debug!(
|
||||
crate::tracing::debug!(
|
||||
"{database_key_index:?}: execute: user cycle_fn says to fall back"
|
||||
);
|
||||
new_value = fallback_value;
|
||||
|
@ -220,7 +210,7 @@ where
|
|||
});
|
||||
cycle_heads.update_iteration_count(database_key_index, iteration_count);
|
||||
revisions.update_iteration_count(iteration_count);
|
||||
tracing::debug!(
|
||||
crate::tracing::debug!(
|
||||
"{database_key_index:?}: execute: iterate again, revisions: {revisions:#?}"
|
||||
);
|
||||
opt_last_provisional = Some(self.insert_memo(
|
||||
|
@ -236,7 +226,7 @@ where
|
|||
|
||||
continue;
|
||||
}
|
||||
tracing::debug!(
|
||||
crate::tracing::debug!(
|
||||
"{database_key_index:?}: execute: fixpoint iteration has a final value"
|
||||
);
|
||||
cycle_heads.remove(&database_key_index);
|
||||
|
@ -247,7 +237,9 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
tracing::debug!("{database_key_index:?}: execute: result.revisions = {revisions:#?}");
|
||||
crate::tracing::debug!(
|
||||
"{database_key_index:?}: execute: result.revisions = {revisions:#?}"
|
||||
);
|
||||
|
||||
break (new_value, revisions);
|
||||
}
|
||||
|
@ -256,9 +248,9 @@ where
|
|||
#[inline]
|
||||
fn execute_query<'db>(
|
||||
db: &'db C::DbView,
|
||||
zalsa: &'db Zalsa,
|
||||
active_query: ActiveQueryGuard<'db>,
|
||||
opt_old_memo: Option<&Memo<'db, C>>,
|
||||
current_revision: Revision,
|
||||
id: Id,
|
||||
) -> (C::Output<'db>, QueryRevisions) {
|
||||
if let Some(old_memo) = opt_old_memo {
|
||||
|
@ -273,14 +265,16 @@ where
|
|||
// * ensure that tracked struct created during the previous iteration
|
||||
// (and are owned by the query) are alive even if the query in this iteration no longer creates them.
|
||||
// * ensure the final returned memo depends on all inputs from all iterations.
|
||||
if old_memo.may_be_provisional() && old_memo.verified_at.load() == current_revision {
|
||||
if old_memo.may_be_provisional()
|
||||
&& old_memo.verified_at.load() == zalsa.current_revision()
|
||||
{
|
||||
active_query.seed_iteration(&old_memo.revisions);
|
||||
}
|
||||
}
|
||||
|
||||
// Query was not previously executed, or value is potentially
|
||||
// stale, or value is absent. Let's execute!
|
||||
let new_value = C::execute(db, C::id_to_input(db, id));
|
||||
let new_value = C::execute(db, C::id_to_input(zalsa, id));
|
||||
|
||||
(new_value, active_query.pop())
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
use crate::cycle::{CycleHeads, CycleRecoveryStrategy, IterationCount};
|
||||
use crate::cycle::{CycleHeadKeys, CycleHeads, CycleRecoveryStrategy, IterationCount};
|
||||
use crate::function::memo::Memo;
|
||||
use crate::function::sync::ClaimResult;
|
||||
use crate::function::{Configuration, IngredientImpl, VerifyResult};
|
||||
use crate::zalsa::{MemoIngredientIndex, Zalsa, ZalsaDatabase};
|
||||
use crate::zalsa::{MemoIngredientIndex, Zalsa};
|
||||
use crate::zalsa_local::{QueryRevisions, ZalsaLocal};
|
||||
use crate::Id;
|
||||
|
||||
|
@ -10,15 +10,22 @@ impl<C> IngredientImpl<C>
|
|||
where
|
||||
C: Configuration,
|
||||
{
|
||||
pub fn fetch<'db>(&'db self, db: &'db C::DbView, id: Id) -> &'db C::Output<'db> {
|
||||
let (zalsa, zalsa_local) = db.zalsas();
|
||||
pub fn fetch<'db>(
|
||||
&'db self,
|
||||
db: &'db C::DbView,
|
||||
zalsa: &'db Zalsa,
|
||||
zalsa_local: &'db ZalsaLocal,
|
||||
id: Id,
|
||||
) -> &'db C::Output<'db> {
|
||||
zalsa.unwind_if_revision_cancelled(zalsa_local);
|
||||
|
||||
let database_key_index = self.database_key_index(id);
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
let _span = tracing::debug_span!("fetch", query = ?database_key_index).entered();
|
||||
let _span = crate::tracing::debug_span!("fetch", query = ?database_key_index).entered();
|
||||
|
||||
let memo = self.refresh_memo(db, zalsa, zalsa_local, id);
|
||||
|
||||
// SAFETY: We just refreshed the memo so it is guaranteed to contain a value now.
|
||||
let memo_value = unsafe { memo.value.as_ref().unwrap_unchecked() };
|
||||
|
||||
|
@ -28,9 +35,11 @@ where
|
|||
database_key_index,
|
||||
memo.revisions.durability,
|
||||
memo.revisions.changed_at,
|
||||
memo.revisions.accumulated().is_some(),
|
||||
&memo.revisions.accumulated_inputs,
|
||||
memo.cycle_heads(),
|
||||
#[cfg(feature = "accumulator")]
|
||||
memo.revisions.accumulated().is_some(),
|
||||
#[cfg(feature = "accumulator")]
|
||||
&memo.revisions.accumulated_inputs,
|
||||
);
|
||||
|
||||
memo_value
|
||||
|
@ -161,20 +170,23 @@ where
|
|||
}
|
||||
// no provisional value; create/insert/return initial provisional value
|
||||
return match C::CYCLE_STRATEGY {
|
||||
CycleRecoveryStrategy::Panic => zalsa_local.with_query_stack(|stack| {
|
||||
panic!(
|
||||
"dependency graph cycle when querying {database_key_index:#?}, \
|
||||
// SAFETY: We do not access the query stack reentrantly.
|
||||
CycleRecoveryStrategy::Panic => unsafe {
|
||||
zalsa_local.with_query_stack_unchecked(|stack| {
|
||||
panic!(
|
||||
"dependency graph cycle when querying {database_key_index:#?}, \
|
||||
set cycle_fn/cycle_initial to fixpoint iterate.\n\
|
||||
Query stack:\n{stack:#?}",
|
||||
);
|
||||
}),
|
||||
);
|
||||
})
|
||||
},
|
||||
CycleRecoveryStrategy::Fixpoint => {
|
||||
tracing::debug!(
|
||||
crate::tracing::debug!(
|
||||
"hit cycle at {database_key_index:#?}, \
|
||||
inserting and returning fixpoint initial value"
|
||||
);
|
||||
let revisions = QueryRevisions::fixpoint_initial(database_key_index);
|
||||
let initial_value = C::cycle_initial(db, C::id_to_input(db, id));
|
||||
let initial_value = C::cycle_initial(db, C::id_to_input(zalsa, id));
|
||||
Some(self.insert_memo(
|
||||
zalsa,
|
||||
id,
|
||||
|
@ -183,12 +195,12 @@ where
|
|||
))
|
||||
}
|
||||
CycleRecoveryStrategy::FallbackImmediate => {
|
||||
tracing::debug!(
|
||||
crate::tracing::debug!(
|
||||
"hit a `FallbackImmediate` cycle at {database_key_index:#?}"
|
||||
);
|
||||
let active_query =
|
||||
zalsa_local.push_query(database_key_index, IterationCount::initial());
|
||||
let fallback_value = C::cycle_initial(db, C::id_to_input(db, id));
|
||||
let fallback_value = C::cycle_initial(db, C::id_to_input(zalsa, id));
|
||||
let mut revisions = active_query.pop();
|
||||
revisions.set_cycle_heads(CycleHeads::initial(database_key_index));
|
||||
// We need this for `cycle_heads()` to work. We will unset this in the outer `execute()`.
|
||||
|
@ -210,8 +222,8 @@ where
|
|||
|
||||
if let Some(old_memo) = opt_old_memo {
|
||||
if old_memo.value.is_some() {
|
||||
let mut cycle_heads = CycleHeads::default();
|
||||
if let VerifyResult::Unchanged(_) =
|
||||
let mut cycle_heads = CycleHeadKeys::new();
|
||||
if let VerifyResult::Unchanged { .. } =
|
||||
self.deep_verify_memo(db, zalsa, old_memo, database_key_index, &mut cycle_heads)
|
||||
{
|
||||
if cycle_heads.is_empty() {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#[cfg(feature = "accumulator")]
|
||||
use crate::accumulator::accumulated_map::InputAccumulatedValues;
|
||||
use crate::cycle::{CycleHeads, CycleRecoveryStrategy, IterationCount, ProvisionalStatus};
|
||||
use crate::cycle::{CycleHeadKeys, CycleRecoveryStrategy, IterationCount, ProvisionalStatus};
|
||||
use crate::function::memo::Memo;
|
||||
use crate::function::sync::ClaimResult;
|
||||
use crate::function::{Configuration, IngredientImpl};
|
||||
|
@ -7,7 +8,7 @@ use crate::key::DatabaseKeyIndex;
|
|||
use crate::sync::atomic::Ordering;
|
||||
use crate::zalsa::{MemoIngredientIndex, Zalsa, ZalsaDatabase};
|
||||
use crate::zalsa_local::{QueryEdgeKind, QueryOriginRef, ZalsaLocal};
|
||||
use crate::{AsDynDatabase as _, Id, Revision};
|
||||
use crate::{Id, Revision};
|
||||
|
||||
/// Result of memo validation.
|
||||
pub enum VerifyResult {
|
||||
|
@ -18,7 +19,10 @@ pub enum VerifyResult {
|
|||
///
|
||||
/// The inner value tracks whether the memo or any of its dependencies have an
|
||||
/// accumulated value.
|
||||
Unchanged(InputAccumulatedValues),
|
||||
Unchanged {
|
||||
#[cfg(feature = "accumulator")]
|
||||
accumulated: InputAccumulatedValues,
|
||||
},
|
||||
}
|
||||
|
||||
impl VerifyResult {
|
||||
|
@ -31,7 +35,10 @@ impl VerifyResult {
|
|||
}
|
||||
|
||||
pub(crate) fn unchanged() -> Self {
|
||||
Self::Unchanged(InputAccumulatedValues::Empty)
|
||||
Self::Unchanged {
|
||||
#[cfg(feature = "accumulator")]
|
||||
accumulated: InputAccumulatedValues::Empty,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -44,7 +51,7 @@ where
|
|||
db: &'db C::DbView,
|
||||
id: Id,
|
||||
revision: Revision,
|
||||
cycle_heads: &mut CycleHeads,
|
||||
cycle_heads: &mut CycleHeadKeys,
|
||||
) -> VerifyResult {
|
||||
let (zalsa, zalsa_local) = db.zalsas();
|
||||
let memo_ingredient_index = self.memo_ingredient_index(zalsa, id);
|
||||
|
@ -53,7 +60,9 @@ where
|
|||
loop {
|
||||
let database_key_index = self.database_key_index(id);
|
||||
|
||||
tracing::debug!("{database_key_index:?}: maybe_changed_after(revision = {revision:?})");
|
||||
crate::tracing::debug!(
|
||||
"{database_key_index:?}: maybe_changed_after(revision = {revision:?})"
|
||||
);
|
||||
|
||||
// Check if we have a verified version: this is the hot path.
|
||||
let memo_guard = self.get_memo_from_table_for(zalsa, id, memo_ingredient_index);
|
||||
|
@ -69,7 +78,10 @@ where
|
|||
return if memo.revisions.changed_at > revision {
|
||||
VerifyResult::Changed
|
||||
} else {
|
||||
VerifyResult::Unchanged(memo.revisions.accumulated_inputs.load())
|
||||
VerifyResult::Unchanged {
|
||||
#[cfg(feature = "accumulator")]
|
||||
accumulated: memo.revisions.accumulated_inputs.load(),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -96,7 +108,7 @@ where
|
|||
key_index: Id,
|
||||
revision: Revision,
|
||||
memo_ingredient_index: MemoIngredientIndex,
|
||||
cycle_heads: &mut CycleHeads,
|
||||
cycle_heads: &mut CycleHeadKeys,
|
||||
) -> Option<VerifyResult> {
|
||||
let database_key_index = self.database_key_index(key_index);
|
||||
|
||||
|
@ -106,21 +118,24 @@ where
|
|||
return None;
|
||||
}
|
||||
ClaimResult::Cycle { .. } => match C::CYCLE_STRATEGY {
|
||||
CycleRecoveryStrategy::Panic => db.zalsa_local().with_query_stack(|stack| {
|
||||
panic!(
|
||||
"dependency graph cycle when validating {database_key_index:#?}, \
|
||||
// SAFETY: We do not access the query stack reentrantly.
|
||||
CycleRecoveryStrategy::Panic => unsafe {
|
||||
db.zalsa_local().with_query_stack_unchecked(|stack| {
|
||||
panic!(
|
||||
"dependency graph cycle when validating {database_key_index:#?}, \
|
||||
set cycle_fn/cycle_initial to fixpoint iterate.\n\
|
||||
Query stack:\n{stack:#?}",
|
||||
);
|
||||
}),
|
||||
);
|
||||
})
|
||||
},
|
||||
CycleRecoveryStrategy::FallbackImmediate => {
|
||||
return Some(VerifyResult::unchanged());
|
||||
}
|
||||
CycleRecoveryStrategy::Fixpoint => {
|
||||
tracing::debug!(
|
||||
crate::tracing::debug!(
|
||||
"hit cycle at {database_key_index:?} in `maybe_changed_after`, returning fixpoint initial value",
|
||||
);
|
||||
cycle_heads.push_initial(database_key_index);
|
||||
cycle_heads.insert(database_key_index);
|
||||
return Some(VerifyResult::unchanged());
|
||||
}
|
||||
},
|
||||
|
@ -132,7 +147,7 @@ where
|
|||
return Some(VerifyResult::Changed);
|
||||
};
|
||||
|
||||
tracing::debug!(
|
||||
crate::tracing::debug!(
|
||||
"{database_key_index:?}: maybe_changed_after_cold, successful claim, \
|
||||
revision = {revision:?}, old_memo = {old_memo:#?}",
|
||||
old_memo = old_memo.tracing_debug()
|
||||
|
@ -141,11 +156,18 @@ where
|
|||
// Check if the inputs are still valid. We can just compare `changed_at`.
|
||||
let deep_verify =
|
||||
self.deep_verify_memo(db, zalsa, old_memo, database_key_index, cycle_heads);
|
||||
if let VerifyResult::Unchanged(accumulated_inputs) = deep_verify {
|
||||
if let VerifyResult::Unchanged {
|
||||
#[cfg(feature = "accumulator")]
|
||||
accumulated: accumulated_inputs,
|
||||
} = deep_verify
|
||||
{
|
||||
return Some(if old_memo.revisions.changed_at > revision {
|
||||
VerifyResult::Changed
|
||||
} else {
|
||||
VerifyResult::Unchanged(accumulated_inputs)
|
||||
VerifyResult::Unchanged {
|
||||
#[cfg(feature = "accumulator")]
|
||||
accumulated: accumulated_inputs,
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -169,10 +191,13 @@ where
|
|||
return Some(if changed_at > revision {
|
||||
VerifyResult::Changed
|
||||
} else {
|
||||
VerifyResult::Unchanged(match memo.revisions.accumulated() {
|
||||
Some(_) => InputAccumulatedValues::Any,
|
||||
None => memo.revisions.accumulated_inputs.load(),
|
||||
})
|
||||
VerifyResult::Unchanged {
|
||||
#[cfg(feature = "accumulator")]
|
||||
accumulated: match memo.revisions.accumulated() {
|
||||
Some(_) => InputAccumulatedValues::Any,
|
||||
None => memo.revisions.accumulated_inputs.load(),
|
||||
},
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -194,7 +219,7 @@ where
|
|||
database_key_index: DatabaseKeyIndex,
|
||||
memo: &Memo<'_, C>,
|
||||
) -> ShallowUpdate {
|
||||
tracing::debug!(
|
||||
crate::tracing::debug!(
|
||||
"{database_key_index:?}: shallow_verify_memo(memo = {memo:#?})",
|
||||
memo = memo.tracing_debug()
|
||||
);
|
||||
|
@ -207,7 +232,7 @@ where
|
|||
}
|
||||
|
||||
let last_changed = zalsa.last_changed_revision(memo.revisions.durability);
|
||||
tracing::debug!(
|
||||
crate::tracing::debug!(
|
||||
"{database_key_index:?}: check_durability(memo = {memo:#?}, last_changed={:?} <= verified_at={:?}) = {:?}",
|
||||
last_changed,
|
||||
verified_at,
|
||||
|
@ -263,7 +288,7 @@ where
|
|||
database_key_index: DatabaseKeyIndex,
|
||||
memo: &Memo<'_, C>,
|
||||
) -> bool {
|
||||
tracing::trace!(
|
||||
crate::tracing::trace!(
|
||||
"{database_key_index:?}: validate_provisional(memo = {memo:#?})",
|
||||
memo = memo.tracing_debug()
|
||||
);
|
||||
|
@ -324,7 +349,7 @@ where
|
|||
database_key_index: DatabaseKeyIndex,
|
||||
memo: &Memo<'_, C>,
|
||||
) -> bool {
|
||||
tracing::trace!(
|
||||
crate::tracing::trace!(
|
||||
"{database_key_index:?}: validate_same_iteration(memo = {memo:#?})",
|
||||
memo = memo.tracing_debug()
|
||||
);
|
||||
|
@ -334,32 +359,38 @@ where
|
|||
return true;
|
||||
}
|
||||
|
||||
zalsa_local.with_query_stack(|stack| {
|
||||
cycle_heads.iter().all(|cycle_head| {
|
||||
stack
|
||||
.iter()
|
||||
.rev()
|
||||
.find(|query| query.database_key_index == cycle_head.database_key_index)
|
||||
.map(|query| query.iteration_count())
|
||||
.or_else(|| {
|
||||
// If this is a cycle head is owned by another thread that is blocked by this ingredient,
|
||||
// check if it has the same iteration count.
|
||||
let ingredient = zalsa
|
||||
.lookup_ingredient(cycle_head.database_key_index.ingredient_index());
|
||||
let wait_result =
|
||||
ingredient.wait_for(zalsa, cycle_head.database_key_index.key_index());
|
||||
// SAFETY: We do not access the query stack reentrantly.
|
||||
unsafe {
|
||||
zalsa_local.with_query_stack_unchecked(|stack| {
|
||||
cycle_heads.iter().all(|cycle_head| {
|
||||
stack
|
||||
.iter()
|
||||
.rev()
|
||||
.find(|query| query.database_key_index == cycle_head.database_key_index)
|
||||
.map(|query| query.iteration_count())
|
||||
.or_else(|| {
|
||||
// If this is a cycle head is owned by another thread that is blocked by this ingredient,
|
||||
// check if it has the same iteration count.
|
||||
let ingredient = zalsa.lookup_ingredient(
|
||||
cycle_head.database_key_index.ingredient_index(),
|
||||
);
|
||||
let wait_result = ingredient
|
||||
.wait_for(zalsa, cycle_head.database_key_index.key_index());
|
||||
|
||||
if !wait_result.is_cycle_with_other_thread() {
|
||||
return None;
|
||||
}
|
||||
if !wait_result.is_cycle_with_other_thread() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let provisional_status = ingredient
|
||||
.provisional_status(zalsa, cycle_head.database_key_index.key_index())?;
|
||||
provisional_status.iteration()
|
||||
})
|
||||
== Some(cycle_head.iteration_count)
|
||||
let provisional_status = ingredient.provisional_status(
|
||||
zalsa,
|
||||
cycle_head.database_key_index.key_index(),
|
||||
)?;
|
||||
provisional_status.iteration()
|
||||
})
|
||||
== Some(cycle_head.iteration_count)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// VerifyResult::Unchanged if the memo's value and `changed_at` time is up-to-date in the
|
||||
|
@ -375,9 +406,9 @@ where
|
|||
zalsa: &Zalsa,
|
||||
old_memo: &Memo<'_, C>,
|
||||
database_key_index: DatabaseKeyIndex,
|
||||
cycle_heads: &mut CycleHeads,
|
||||
cycle_heads: &mut CycleHeadKeys,
|
||||
) -> VerifyResult {
|
||||
tracing::debug!(
|
||||
crate::tracing::debug!(
|
||||
"{database_key_index:?}: deep_verify_memo(old_memo = {old_memo:#?})",
|
||||
old_memo = old_memo.tracing_debug()
|
||||
);
|
||||
|
@ -416,7 +447,7 @@ where
|
|||
// are tracked by the outer query. Nothing should have changed assuming that the
|
||||
// fixpoint initial function is deterministic.
|
||||
QueryOriginRef::FixpointInitial => {
|
||||
cycle_heads.push_initial(database_key_index);
|
||||
cycle_heads.insert(database_key_index);
|
||||
VerifyResult::unchanged()
|
||||
}
|
||||
QueryOriginRef::DerivedUntracked(_) => {
|
||||
|
@ -432,8 +463,7 @@ where
|
|||
return VerifyResult::Changed;
|
||||
}
|
||||
|
||||
let dyn_db = db.as_dyn_database();
|
||||
|
||||
#[cfg(feature = "accumulator")]
|
||||
let mut inputs = InputAccumulatedValues::Empty;
|
||||
// Fully tracked inputs? Iterate over the inputs and check them, one by one.
|
||||
//
|
||||
|
@ -445,15 +475,18 @@ where
|
|||
match edge.kind() {
|
||||
QueryEdgeKind::Input(dependency_index) => {
|
||||
match dependency_index.maybe_changed_after(
|
||||
dyn_db,
|
||||
db.into(),
|
||||
zalsa,
|
||||
old_memo.verified_at.load(),
|
||||
cycle_heads,
|
||||
) {
|
||||
VerifyResult::Changed => return VerifyResult::Changed,
|
||||
VerifyResult::Unchanged(input_accumulated) => {
|
||||
inputs |= input_accumulated;
|
||||
#[cfg(feature = "accumulator")]
|
||||
VerifyResult::Unchanged { accumulated } => {
|
||||
inputs |= accumulated;
|
||||
}
|
||||
#[cfg(not(feature = "accumulator"))]
|
||||
VerifyResult::Unchanged { .. } => {}
|
||||
}
|
||||
}
|
||||
QueryEdgeKind::Output(dependency_index) => {
|
||||
|
@ -508,6 +541,7 @@ where
|
|||
// 1 and 3
|
||||
if cycle_heads.is_empty() {
|
||||
old_memo.mark_as_verified(zalsa, database_key_index);
|
||||
#[cfg(feature = "accumulator")]
|
||||
old_memo.revisions.accumulated_inputs.store(inputs);
|
||||
|
||||
if is_provisional {
|
||||
|
@ -518,7 +552,10 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
VerifyResult::Unchanged(inputs)
|
||||
VerifyResult::Unchanged {
|
||||
#[cfg(feature = "accumulator")]
|
||||
accumulated: inputs,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ impl<C: Configuration> IngredientImpl<C> {
|
|||
let static_memo =
|
||||
unsafe { transmute::<NonNull<Memo<'db, C>>, NonNull<Memo<'static, C>>>(memo) };
|
||||
let old_static_memo = zalsa
|
||||
.memo_table_for(id)
|
||||
.memo_table_for::<C::SalsaStruct<'_>>(id)
|
||||
.insert(memo_ingredient_index, static_memo)?;
|
||||
// SAFETY: The table stores 'static memos (to support `Any`), the memos are in fact valid
|
||||
// for `'db` though as we delay their dropping to the end of a revision.
|
||||
|
@ -48,7 +48,9 @@ impl<C: Configuration> IngredientImpl<C> {
|
|||
id: Id,
|
||||
memo_ingredient_index: MemoIngredientIndex,
|
||||
) -> Option<&'db Memo<'db, C>> {
|
||||
let static_memo = zalsa.memo_table_for(id).get(memo_ingredient_index)?;
|
||||
let static_memo = zalsa
|
||||
.memo_table_for::<C::SalsaStruct<'_>>(id)
|
||||
.get(memo_ingredient_index)?;
|
||||
// SAFETY: The table stores 'static memos (to support `Any`), the memos are in fact valid
|
||||
// for `'db` though as we delay their dropping to the end of a revision.
|
||||
Some(unsafe { transmute::<&Memo<'static, C>, &'db Memo<'db, C>>(static_memo.as_ref()) })
|
||||
|
@ -151,7 +153,7 @@ impl<'db, C: Configuration> Memo<'db, C> {
|
|||
} else {
|
||||
// all our cycle heads are complete; re-fetch
|
||||
// and we should get a non-provisional memo.
|
||||
tracing::debug!(
|
||||
crate::tracing::debug!(
|
||||
"Retrying provisional memo {database_key_index:?} after awaiting cycle heads."
|
||||
);
|
||||
true
|
||||
|
@ -178,7 +180,7 @@ impl<'db, C: Configuration> Memo<'db, C> {
|
|||
|
||||
#[inline(never)]
|
||||
fn block_on_heads_cold(zalsa: &Zalsa, heads: &CycleHeads) -> bool {
|
||||
let _entered = tracing::debug_span!("block_on_heads").entered();
|
||||
let _entered = crate::tracing::debug_span!("block_on_heads").entered();
|
||||
let mut cycle_heads = TryClaimCycleHeadsIter::new(zalsa, heads);
|
||||
let mut all_cycles = true;
|
||||
|
||||
|
@ -207,7 +209,7 @@ impl<'db, C: Configuration> Memo<'db, C> {
|
|||
/// Unlike `block_on_heads`, this code does not block on any cycle head. Instead it returns `false` if
|
||||
/// claiming all cycle heads failed because one of them is running on another thread.
|
||||
pub(super) fn try_claim_heads(&self, zalsa: &Zalsa, zalsa_local: &ZalsaLocal) -> bool {
|
||||
let _entered = tracing::debug_span!("try_claim_heads").entered();
|
||||
let _entered = crate::tracing::debug_span!("try_claim_heads").entered();
|
||||
if self.all_cycles_on_stack(zalsa_local) {
|
||||
return true;
|
||||
}
|
||||
|
@ -234,14 +236,17 @@ impl<'db, C: Configuration> Memo<'db, C> {
|
|||
return true;
|
||||
}
|
||||
|
||||
zalsa_local.with_query_stack(|stack| {
|
||||
cycle_heads.iter().all(|cycle_head| {
|
||||
stack
|
||||
.iter()
|
||||
.rev()
|
||||
.any(|query| query.database_key_index == cycle_head.database_key_index)
|
||||
// SAFETY: We do not access the query stack reentrantly.
|
||||
unsafe {
|
||||
zalsa_local.with_query_stack_unchecked(|stack| {
|
||||
cycle_heads.iter().all(|cycle_head| {
|
||||
stack
|
||||
.iter()
|
||||
.rev()
|
||||
.any(|query| query.database_key_index == cycle_head.database_key_index)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Cycle heads that should be propagated to dependent queries.
|
||||
|
@ -417,7 +422,7 @@ impl<'me> Iterator for TryClaimCycleHeadsIter<'me> {
|
|||
ProvisionalStatus::Final { .. } | ProvisionalStatus::FallbackImmediate => {
|
||||
// This cycle is already finalized, so we don't need to wait on it;
|
||||
// keep looping through cycle heads.
|
||||
tracing::trace!("Dependent cycle head {head:?} has been finalized.");
|
||||
crate::tracing::trace!("Dependent cycle head {head:?} has been finalized.");
|
||||
Some(TryClaimHeadsResult::Finalized)
|
||||
}
|
||||
ProvisionalStatus::Provisional { .. } => {
|
||||
|
@ -425,11 +430,11 @@ impl<'me> Iterator for TryClaimCycleHeadsIter<'me> {
|
|||
WaitForResult::Cycle { .. } => {
|
||||
// We hit a cycle blocking on the cycle head; this means this query actively
|
||||
// participates in the cycle and some other query is blocked on this thread.
|
||||
tracing::debug!("Waiting for {head:?} results in a cycle");
|
||||
crate::tracing::debug!("Waiting for {head:?} results in a cycle");
|
||||
Some(TryClaimHeadsResult::Cycle)
|
||||
}
|
||||
WaitForResult::Running(running) => {
|
||||
tracing::debug!("Ingredient {head:?} is running: {running:?}");
|
||||
crate::tracing::debug!("Ingredient {head:?} is running: {running:?}");
|
||||
|
||||
Some(TryClaimHeadsResult::Running(RunningCycleHead {
|
||||
inner: running,
|
||||
|
@ -451,8 +456,9 @@ mod _memory_usage {
|
|||
use crate::cycle::CycleRecoveryStrategy;
|
||||
use crate::ingredient::Location;
|
||||
use crate::plumbing::{IngredientIndices, MemoIngredientSingletonIndex, SalsaStructInDb};
|
||||
use crate::table::memo::MemoTableWithTypes;
|
||||
use crate::zalsa::Zalsa;
|
||||
use crate::{CycleRecoveryAction, Database, Id};
|
||||
use crate::{CycleRecoveryAction, Database, Id, Revision};
|
||||
|
||||
use std::any::TypeId;
|
||||
use std::num::NonZeroUsize;
|
||||
|
@ -466,13 +472,17 @@ mod _memory_usage {
|
|||
impl SalsaStructInDb for DummyStruct {
|
||||
type MemoIngredientMap = MemoIngredientSingletonIndex;
|
||||
|
||||
fn lookup_or_create_ingredient_index(_: &Zalsa) -> IngredientIndices {
|
||||
fn lookup_ingredient_index(_: &Zalsa) -> IngredientIndices {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn cast(_: Id, _: TypeId) -> Option<Self> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
unsafe fn memo_table(_: &Zalsa, _: Id, _: Revision) -> MemoTableWithTypes<'_> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
struct DummyConfiguration;
|
||||
|
@ -490,7 +500,7 @@ mod _memory_usage {
|
|||
unimplemented!()
|
||||
}
|
||||
|
||||
fn id_to_input(_: &Self::DbView, _: Id) -> Self::Input<'_> {
|
||||
fn id_to_input(_: &Zalsa, _: Id) -> Self::Input<'_> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
#[cfg(feature = "accumulator")]
|
||||
use crate::accumulator::accumulated_map::InputAccumulatedValues;
|
||||
use crate::function::memo::Memo;
|
||||
use crate::function::{Configuration, IngredientImpl};
|
||||
|
@ -66,6 +67,7 @@ where
|
|||
changed_at: current_deps.changed_at,
|
||||
durability: current_deps.durability,
|
||||
origin: QueryOrigin::assigned(active_query_key),
|
||||
#[cfg(feature = "accumulator")]
|
||||
accumulated_inputs: Default::default(),
|
||||
verified_final: AtomicBool::new(true),
|
||||
extra: QueryRevisionsExtra::default(),
|
||||
|
@ -83,7 +85,7 @@ where
|
|||
revisions,
|
||||
};
|
||||
|
||||
tracing::debug!(
|
||||
crate::tracing::debug!(
|
||||
"specify: about to add memo {:#?} for key {:?}",
|
||||
memo.tracing_debug(),
|
||||
key
|
||||
|
@ -124,6 +126,7 @@ where
|
|||
|
||||
let database_key_index = self.database_key_index(key);
|
||||
memo.mark_as_verified(zalsa, database_key_index);
|
||||
#[cfg(feature = "accumulator")]
|
||||
memo.revisions
|
||||
.accumulated_inputs
|
||||
.store(InputAccumulatedValues::Empty);
|
||||
|
|
|
@ -1,50 +1,35 @@
|
|||
use std::any::{Any, TypeId};
|
||||
use std::fmt;
|
||||
|
||||
use crate::accumulator::accumulated_map::{AccumulatedMap, InputAccumulatedValues};
|
||||
use crate::cycle::{
|
||||
empty_cycle_heads, CycleHeads, CycleRecoveryStrategy, IterationCount, ProvisionalStatus,
|
||||
empty_cycle_heads, CycleHeadKeys, CycleHeads, CycleRecoveryStrategy, IterationCount,
|
||||
ProvisionalStatus,
|
||||
};
|
||||
use crate::database::RawDatabase;
|
||||
use crate::function::VerifyResult;
|
||||
use crate::plumbing::IngredientIndices;
|
||||
use crate::runtime::Running;
|
||||
use crate::sync::Arc;
|
||||
use crate::table::memo::MemoTableTypes;
|
||||
use crate::table::Table;
|
||||
use crate::zalsa::{transmute_data_mut_ptr, transmute_data_ptr, IngredientIndex, Zalsa};
|
||||
use crate::zalsa_local::QueryOriginRef;
|
||||
use crate::{Database, DatabaseKeyIndex, Id, Revision};
|
||||
use crate::{DatabaseKeyIndex, Id, Revision};
|
||||
|
||||
/// A "jar" is a group of ingredients that are added atomically.
|
||||
///
|
||||
/// Each type implementing jar can be added to the database at most once.
|
||||
pub trait Jar: Any {
|
||||
/// This creates the ingredient dependencies of this jar. We need to split this from `create_ingredients()`
|
||||
/// because while `create_ingredients()` is called, a lock on the ingredient map is held (to guarantee
|
||||
/// atomicity), so other ingredients could not be created.
|
||||
///
|
||||
/// Only tracked fns use this.
|
||||
fn create_dependencies(_zalsa: &Zalsa) -> IngredientIndices
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
IngredientIndices::empty()
|
||||
}
|
||||
|
||||
/// Create the ingredients given the index of the first one.
|
||||
///
|
||||
/// All subsequent ingredients will be assigned contiguous indices.
|
||||
fn create_ingredients(
|
||||
zalsa: &Zalsa,
|
||||
zalsa: &mut Zalsa,
|
||||
first_index: IngredientIndex,
|
||||
dependencies: IngredientIndices,
|
||||
) -> Vec<Box<dyn Ingredient>>
|
||||
where
|
||||
Self: Sized;
|
||||
) -> Vec<Box<dyn Ingredient>>;
|
||||
|
||||
/// This returns the [`TypeId`] of the ID struct, that is, the struct that wraps `salsa::Id`
|
||||
/// and carry the name of the jar.
|
||||
fn id_struct_type_id() -> TypeId
|
||||
where
|
||||
Self: Sized;
|
||||
fn id_struct_type_id() -> TypeId;
|
||||
}
|
||||
|
||||
pub struct Location {
|
||||
|
@ -61,12 +46,13 @@ pub trait Ingredient: Any + std::fmt::Debug + Send + Sync {
|
|||
/// # Safety
|
||||
///
|
||||
/// The passed in database needs to be the same one that the ingredient was created with.
|
||||
unsafe fn maybe_changed_after<'db>(
|
||||
&'db self,
|
||||
db: &'db dyn Database,
|
||||
unsafe fn maybe_changed_after(
|
||||
&self,
|
||||
zalsa: &crate::zalsa::Zalsa,
|
||||
db: crate::database::RawDatabase<'_>,
|
||||
input: Id,
|
||||
revision: Revision,
|
||||
cycle_heads: &mut CycleHeads,
|
||||
cycle_heads: &mut CycleHeadKeys,
|
||||
) -> VerifyResult;
|
||||
|
||||
/// Returns information about the current provisional status of `input`.
|
||||
|
@ -151,7 +137,9 @@ pub trait Ingredient: Any + std::fmt::Debug + Send + Sync {
|
|||
);
|
||||
}
|
||||
|
||||
fn memo_table_types(&self) -> Arc<MemoTableTypes>;
|
||||
fn memo_table_types(&self) -> &Arc<MemoTableTypes>;
|
||||
|
||||
fn memo_table_types_mut(&mut self) -> &mut Arc<MemoTableTypes>;
|
||||
|
||||
fn fmt_index(&self, index: crate::Id, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
fmt_index(self.debug_name(), index, fmt)
|
||||
|
@ -173,26 +161,38 @@ pub trait Ingredient: Any + std::fmt::Debug + Send + Sync {
|
|||
|
||||
/// What values were accumulated during the creation of the value at `key_index`
|
||||
/// (if any).
|
||||
fn accumulated<'db>(
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The passed in database needs to be the same one that the ingredient was created with.
|
||||
#[cfg(feature = "accumulator")]
|
||||
unsafe fn accumulated<'db>(
|
||||
&'db self,
|
||||
db: &'db dyn Database,
|
||||
db: RawDatabase<'db>,
|
||||
key_index: Id,
|
||||
) -> (Option<&'db AccumulatedMap>, InputAccumulatedValues) {
|
||||
) -> (
|
||||
Option<&'db crate::accumulator::accumulated_map::AccumulatedMap>,
|
||||
crate::accumulator::accumulated_map::InputAccumulatedValues,
|
||||
) {
|
||||
let _ = (db, key_index);
|
||||
(None, InputAccumulatedValues::Empty)
|
||||
(
|
||||
None,
|
||||
crate::accumulator::accumulated_map::InputAccumulatedValues::Empty,
|
||||
)
|
||||
}
|
||||
|
||||
/// Returns memory usage information about any instances of the ingredient,
|
||||
/// if applicable.
|
||||
#[cfg(feature = "salsa_unstable")]
|
||||
fn memory_usage(&self, _db: &dyn Database) -> Option<Vec<crate::database::SlotInfo>> {
|
||||
fn memory_usage(&self, _db: &dyn crate::Database) -> Option<Vec<crate::database::SlotInfo>> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl dyn Ingredient {
|
||||
/// Equivalent to the `downcast` methods on `any`.
|
||||
/// Because we do not have dyn-upcasting support, we need this workaround.
|
||||
/// Equivalent to the `downcast` method on `Any`.
|
||||
///
|
||||
/// Because we do not have dyn-downcasting support, we need this workaround.
|
||||
pub fn assert_type<T: Any>(&self) -> &T {
|
||||
assert_eq!(
|
||||
self.type_id(),
|
||||
|
@ -206,8 +206,28 @@ impl dyn Ingredient {
|
|||
unsafe { transmute_data_ptr(self) }
|
||||
}
|
||||
|
||||
/// Equivalent to the `downcast` methods on `any`.
|
||||
/// Because we do not have dyn-upcasting support, we need this workaround.
|
||||
/// Equivalent to the `downcast` methods on `Any`.
|
||||
///
|
||||
/// Because we do not have dyn-downcasting support, we need this workaround.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The contained value must be of type `T`.
|
||||
pub unsafe fn assert_type_unchecked<T: Any>(&self) -> &T {
|
||||
debug_assert_eq!(
|
||||
self.type_id(),
|
||||
TypeId::of::<T>(),
|
||||
"ingredient `{self:?}` is not of type `{}`",
|
||||
std::any::type_name::<T>()
|
||||
);
|
||||
|
||||
// SAFETY: Guaranteed by caller.
|
||||
unsafe { transmute_data_ptr(self) }
|
||||
}
|
||||
|
||||
/// Equivalent to the `downcast` method on `Any`.
|
||||
///
|
||||
/// Because we do not have dyn-downcasting support, we need this workaround.
|
||||
pub fn assert_type_mut<T: Any>(&mut self) -> &mut T {
|
||||
assert_eq!(
|
||||
Any::type_id(self),
|
||||
|
|
237
src/ingredient_cache.rs
Normal file
237
src/ingredient_cache.rs
Normal file
|
@ -0,0 +1,237 @@
|
|||
pub use imp::IngredientCache;
|
||||
|
||||
#[cfg(feature = "inventory")]
|
||||
mod imp {
|
||||
use crate::plumbing::Ingredient;
|
||||
use crate::sync::atomic::{self, AtomicU32, Ordering};
|
||||
use crate::zalsa::Zalsa;
|
||||
use crate::IngredientIndex;
|
||||
|
||||
use std::marker::PhantomData;
|
||||
|
||||
/// Caches an ingredient index.
|
||||
///
|
||||
/// Note that all ingredients are statically registered with `inventory`, so their
|
||||
/// indices should be stable across any databases.
|
||||
pub struct IngredientCache<I>
|
||||
where
|
||||
I: Ingredient,
|
||||
{
|
||||
ingredient_index: AtomicU32,
|
||||
phantom: PhantomData<fn() -> I>,
|
||||
}
|
||||
|
||||
impl<I> Default for IngredientCache<I>
|
||||
where
|
||||
I: Ingredient,
|
||||
{
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> IngredientCache<I>
|
||||
where
|
||||
I: Ingredient,
|
||||
{
|
||||
const UNINITIALIZED: u32 = u32::MAX;
|
||||
|
||||
/// Create a new cache
|
||||
pub const fn new() -> Self {
|
||||
Self {
|
||||
ingredient_index: atomic::AtomicU32::new(Self::UNINITIALIZED),
|
||||
phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Get a reference to the ingredient in the database.
|
||||
///
|
||||
/// If the ingredient index is not already in the cache, it will be loaded and cached.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The `IngredientIndex` returned by the closure must reference a valid ingredient of
|
||||
/// type `I` in the provided zalsa database.
|
||||
pub unsafe fn get_or_create<'db>(
|
||||
&self,
|
||||
zalsa: &'db Zalsa,
|
||||
load_index: impl Fn() -> IngredientIndex,
|
||||
) -> &'db I {
|
||||
let mut ingredient_index = self.ingredient_index.load(Ordering::Acquire);
|
||||
if ingredient_index == Self::UNINITIALIZED {
|
||||
ingredient_index = self.get_or_create_index_slow(load_index).as_u32();
|
||||
};
|
||||
|
||||
// SAFETY: `ingredient_index` is initialized from a valid `IngredientIndex`.
|
||||
let ingredient_index = unsafe { IngredientIndex::new_unchecked(ingredient_index) };
|
||||
|
||||
// SAFETY: There are a two cases here:
|
||||
// - The `create_index` closure was called due to the data being uncached. In this
|
||||
// case, the caller guarantees the index is in-bounds and has the correct type.
|
||||
// - The index was cached. While the current database might not be the same database
|
||||
// the ingredient was initially loaded from, the `inventory` feature is enabled, so
|
||||
// ingredient indices are stable across databases. Thus the index is still in-bounds
|
||||
// and has the correct type.
|
||||
unsafe {
|
||||
zalsa
|
||||
.lookup_ingredient_unchecked(ingredient_index)
|
||||
.assert_type_unchecked()
|
||||
}
|
||||
}
|
||||
|
||||
#[cold]
|
||||
#[inline(never)]
|
||||
fn get_or_create_index_slow(
|
||||
&self,
|
||||
load_index: impl Fn() -> IngredientIndex,
|
||||
) -> IngredientIndex {
|
||||
let ingredient_index = load_index();
|
||||
|
||||
// It doesn't matter if we overwrite any stores, as `create_index` should
|
||||
// always return the same index when the `inventory` feature is enabled.
|
||||
self.ingredient_index
|
||||
.store(ingredient_index.as_u32(), Ordering::Release);
|
||||
|
||||
ingredient_index
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "inventory"))]
|
||||
mod imp {
|
||||
use crate::nonce::Nonce;
|
||||
use crate::plumbing::Ingredient;
|
||||
use crate::sync::atomic::{AtomicU64, Ordering};
|
||||
use crate::zalsa::{StorageNonce, Zalsa};
|
||||
use crate::IngredientIndex;
|
||||
|
||||
use std::marker::PhantomData;
|
||||
use std::mem;
|
||||
|
||||
/// Caches an ingredient index.
|
||||
///
|
||||
/// With manual registration, ingredient indices can vary across databases,
|
||||
/// but we can retain most of the benefit by optimizing for the the case of
|
||||
/// a single database.
|
||||
pub struct IngredientCache<I>
|
||||
where
|
||||
I: Ingredient,
|
||||
{
|
||||
// A packed representation of `Option<(Nonce<StorageNonce>, IngredientIndex)>`.
|
||||
//
|
||||
// This allows us to replace a lock in favor of an atomic load. This works thanks to `Nonce`
|
||||
// having a niche, which means the entire type can fit into an `AtomicU64`.
|
||||
cached_data: AtomicU64,
|
||||
phantom: PhantomData<fn() -> I>,
|
||||
}
|
||||
|
||||
impl<I> Default for IngredientCache<I>
|
||||
where
|
||||
I: Ingredient,
|
||||
{
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> IngredientCache<I>
|
||||
where
|
||||
I: Ingredient,
|
||||
{
|
||||
const UNINITIALIZED: u64 = 0;
|
||||
|
||||
/// Create a new cache
|
||||
pub const fn new() -> Self {
|
||||
Self {
|
||||
cached_data: AtomicU64::new(Self::UNINITIALIZED),
|
||||
phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Get a reference to the ingredient in the database.
|
||||
///
|
||||
/// If the ingredient is not already in the cache, it will be created.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The `IngredientIndex` returned by the closure must reference a valid ingredient of
|
||||
/// type `I` in the provided zalsa database.
|
||||
#[inline(always)]
|
||||
pub unsafe fn get_or_create<'db>(
|
||||
&self,
|
||||
zalsa: &'db Zalsa,
|
||||
create_index: impl Fn() -> IngredientIndex,
|
||||
) -> &'db I {
|
||||
let index = self.get_or_create_index(zalsa, create_index);
|
||||
|
||||
// SAFETY: There are a two cases here:
|
||||
// - The `create_index` closure was called due to the data being uncached for the
|
||||
// provided database. In this case, the caller guarantees the index is in-bounds
|
||||
// and has the correct type.
|
||||
// - We verified the index was cached for the same database, by the nonce check.
|
||||
// Thus the initial safety argument still applies.
|
||||
unsafe {
|
||||
zalsa
|
||||
.lookup_ingredient_unchecked(index)
|
||||
.assert_type_unchecked::<I>()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_or_create_index(
|
||||
&self,
|
||||
zalsa: &Zalsa,
|
||||
create_index: impl Fn() -> IngredientIndex,
|
||||
) -> IngredientIndex {
|
||||
const _: () = assert!(
|
||||
mem::size_of::<(Nonce<StorageNonce>, IngredientIndex)>() == mem::size_of::<u64>()
|
||||
);
|
||||
|
||||
let cached_data = self.cached_data.load(Ordering::Acquire);
|
||||
if cached_data == Self::UNINITIALIZED {
|
||||
return self.get_or_create_index_slow(zalsa, create_index);
|
||||
};
|
||||
|
||||
// Unpack our `u64` into the nonce and index.
|
||||
//
|
||||
// SAFETY: The lower bits of `cached_data` are initialized from a valid `IngredientIndex`.
|
||||
let index = unsafe { IngredientIndex::new_unchecked(cached_data as u32) };
|
||||
|
||||
// SAFETY: We've checked against `UNINITIALIZED` (0) above and so the upper bits must be non-zero.
|
||||
let nonce = crate::nonce::Nonce::<StorageNonce>::from_u32(unsafe {
|
||||
std::num::NonZeroU32::new_unchecked((cached_data >> u32::BITS) as u32)
|
||||
});
|
||||
|
||||
// The data was cached for a different database, we have to ensure the ingredient was
|
||||
// created in ours.
|
||||
if zalsa.nonce() != nonce {
|
||||
return create_index();
|
||||
}
|
||||
|
||||
index
|
||||
}
|
||||
|
||||
#[cold]
|
||||
#[inline(never)]
|
||||
fn get_or_create_index_slow(
|
||||
&self,
|
||||
zalsa: &Zalsa,
|
||||
create_index: impl Fn() -> IngredientIndex,
|
||||
) -> IngredientIndex {
|
||||
let index = create_index();
|
||||
let nonce = zalsa.nonce().into_u32().get() as u64;
|
||||
let packed = (nonce << u32::BITS) | (index.as_u32() as u64);
|
||||
debug_assert_ne!(packed, IngredientCache::<I>::UNINITIALIZED);
|
||||
|
||||
// Discard the result, whether we won over the cache or not doesn't matter.
|
||||
_ = self.cached_data.compare_exchange(
|
||||
IngredientCache::<I>::UNINITIALIZED,
|
||||
packed,
|
||||
Ordering::Release,
|
||||
Ordering::Relaxed,
|
||||
);
|
||||
|
||||
// Use our locally computed index regardless of which one was cached.
|
||||
index
|
||||
}
|
||||
}
|
||||
}
|
53
src/input.rs
53
src/input.rs
|
@ -8,7 +8,7 @@ pub mod singleton;
|
|||
|
||||
use input_field::FieldIngredientImpl;
|
||||
|
||||
use crate::cycle::CycleHeads;
|
||||
use crate::cycle::CycleHeadKeys;
|
||||
use crate::function::VerifyResult;
|
||||
use crate::id::{AsId, FromId, FromIdWithDb};
|
||||
use crate::ingredient::Ingredient;
|
||||
|
@ -19,7 +19,7 @@ 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};
|
||||
use crate::{zalsa_local, Durability, Id, Revision, Runtime};
|
||||
|
||||
pub trait Configuration: Any {
|
||||
const DEBUG_NAME: &'static str;
|
||||
|
@ -56,9 +56,8 @@ impl<C: Configuration> Default for JarImpl<C> {
|
|||
|
||||
impl<C: Configuration> Jar for JarImpl<C> {
|
||||
fn create_ingredients(
|
||||
_zalsa: &Zalsa,
|
||||
_zalsa: &mut 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);
|
||||
|
||||
|
@ -105,20 +104,23 @@ impl<C: Configuration> IngredientImpl<C> {
|
|||
|
||||
pub fn new_input(
|
||||
&self,
|
||||
db: &dyn Database,
|
||||
zalsa: &Zalsa,
|
||||
zalsa_local: &zalsa_local::ZalsaLocal,
|
||||
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> {
|
||||
let (id, _) = zalsa_local.allocate(zalsa, self.ingredient_index, |_| Value::<C> {
|
||||
fields,
|
||||
revisions,
|
||||
durabilities,
|
||||
memos: Default::default(),
|
||||
})
|
||||
// SAFETY: We only ever access the memos of a value that we allocated through
|
||||
// our `MemoTableTypes`.
|
||||
memos: unsafe { MemoTable::new(self.memo_table_types()) },
|
||||
});
|
||||
|
||||
id
|
||||
});
|
||||
|
||||
FromIdWithDb::from_id(id, zalsa)
|
||||
|
@ -176,11 +178,11 @@ impl<C: Configuration> IngredientImpl<C> {
|
|||
/// The caller is responsible for selecting the appropriate element.
|
||||
pub fn field<'db>(
|
||||
&'db self,
|
||||
db: &'db dyn crate::Database,
|
||||
zalsa: &'db Zalsa,
|
||||
zalsa_local: &'db zalsa_local::ZalsaLocal,
|
||||
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);
|
||||
|
@ -196,17 +198,13 @@ impl<C: Configuration> IngredientImpl<C> {
|
|||
|
||||
#[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>>()
|
||||
pub fn entries<'db>(&'db self, zalsa: &'db Zalsa) -> impl Iterator<Item = &'db Value<C>> {
|
||||
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();
|
||||
pub fn leak_fields<'db>(&'db self, zalsa: &'db Zalsa, id: C::Struct) -> &'db C::Fields {
|
||||
let id = id.as_id();
|
||||
let value = Self::data(zalsa, id);
|
||||
&value.fields
|
||||
|
@ -224,10 +222,11 @@ impl<C: Configuration> Ingredient for IngredientImpl<C> {
|
|||
|
||||
unsafe fn maybe_changed_after(
|
||||
&self,
|
||||
_db: &dyn Database,
|
||||
_zalsa: &crate::zalsa::Zalsa,
|
||||
_db: crate::database::RawDatabase<'_>,
|
||||
_input: Id,
|
||||
_revision: Revision,
|
||||
_cycle_heads: &mut CycleHeads,
|
||||
_cycle_heads: &mut CycleHeadKeys,
|
||||
) -> VerifyResult {
|
||||
// Input ingredients are just a counter, they store no data, they are immortal.
|
||||
// Their *fields* are stored in function ingredients elsewhere.
|
||||
|
@ -238,15 +237,19 @@ impl<C: Configuration> Ingredient for IngredientImpl<C> {
|
|||
C::DEBUG_NAME
|
||||
}
|
||||
|
||||
fn memo_table_types(&self) -> Arc<MemoTableTypes> {
|
||||
self.memo_table_types.clone()
|
||||
fn memo_table_types(&self) -> &Arc<MemoTableTypes> {
|
||||
&self.memo_table_types
|
||||
}
|
||||
|
||||
fn memo_table_types_mut(&mut self) -> &mut Arc<MemoTableTypes> {
|
||||
&mut self.memo_table_types
|
||||
}
|
||||
|
||||
/// Returns memory usage information about any inputs.
|
||||
#[cfg(feature = "salsa_unstable")]
|
||||
fn memory_usage(&self, db: &dyn Database) -> Option<Vec<crate::database::SlotInfo>> {
|
||||
fn memory_usage(&self, db: &dyn crate::Database) -> Option<Vec<crate::database::SlotInfo>> {
|
||||
let memory_usage = self
|
||||
.entries(db)
|
||||
.entries(db.zalsa())
|
||||
// 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) })
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
use std::fmt;
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use crate::cycle::CycleHeads;
|
||||
use crate::cycle::CycleHeadKeys;
|
||||
use crate::function::VerifyResult;
|
||||
use crate::ingredient::Ingredient;
|
||||
use crate::input::{Configuration, IngredientImpl, Value};
|
||||
use crate::sync::Arc;
|
||||
use crate::table::memo::MemoTableTypes;
|
||||
use crate::zalsa::IngredientIndex;
|
||||
use crate::{Database, Id, Revision};
|
||||
use crate::{Id, Revision};
|
||||
|
||||
/// Ingredient used to represent the fields of a `#[salsa::input]`.
|
||||
///
|
||||
|
@ -52,12 +52,12 @@ where
|
|||
|
||||
unsafe fn maybe_changed_after(
|
||||
&self,
|
||||
db: &dyn Database,
|
||||
zalsa: &crate::zalsa::Zalsa,
|
||||
_db: crate::database::RawDatabase<'_>,
|
||||
input: Id,
|
||||
revision: Revision,
|
||||
_cycle_heads: &mut CycleHeads,
|
||||
_cycle_heads: &mut CycleHeadKeys,
|
||||
) -> VerifyResult {
|
||||
let zalsa = db.zalsa();
|
||||
let value = <IngredientImpl<C>>::data(zalsa, input);
|
||||
VerifyResult::changed_if(value.revisions[self.field_index] > revision)
|
||||
}
|
||||
|
@ -76,7 +76,11 @@ where
|
|||
C::FIELD_DEBUG_NAMES[self.field_index]
|
||||
}
|
||||
|
||||
fn memo_table_types(&self) -> Arc<MemoTableTypes> {
|
||||
fn memo_table_types(&self) -> &Arc<MemoTableTypes> {
|
||||
unreachable!("input fields do not allocate pages")
|
||||
}
|
||||
|
||||
fn memo_table_types_mut(&mut self) -> &mut Arc<MemoTableTypes> {
|
||||
unreachable!("input fields do not allocate pages")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,18 +10,18 @@ use crossbeam_utils::CachePadded;
|
|||
use intrusive_collections::{intrusive_adapter, LinkedList, LinkedListLink, UnsafeRef};
|
||||
use rustc_hash::FxBuildHasher;
|
||||
|
||||
use crate::cycle::CycleHeads;
|
||||
use crate::cycle::CycleHeadKeys;
|
||||
use crate::durability::Durability;
|
||||
use crate::function::VerifyResult;
|
||||
use crate::id::{AsId, FromId};
|
||||
use crate::ingredient::Ingredient;
|
||||
use crate::plumbing::{IngredientIndices, Jar, ZalsaLocal};
|
||||
use crate::plumbing::{Jar, ZalsaLocal};
|
||||
use crate::revision::AtomicRevision;
|
||||
use crate::sync::{Arc, Mutex, OnceLock};
|
||||
use crate::table::memo::{MemoTable, MemoTableTypes, MemoTableWithTypesMut};
|
||||
use crate::table::Slot;
|
||||
use crate::zalsa::{IngredientIndex, Zalsa};
|
||||
use crate::{Database, DatabaseKeyIndex, Event, EventKind, Id, Revision};
|
||||
use crate::{DatabaseKeyIndex, Event, EventKind, Id, Revision};
|
||||
|
||||
/// Trait that defines the key properties of an interned struct.
|
||||
///
|
||||
|
@ -224,9 +224,8 @@ impl<C: Configuration> Default for JarImpl<C> {
|
|||
|
||||
impl<C: Configuration> Jar for JarImpl<C> {
|
||||
fn create_ingredients(
|
||||
_zalsa: &Zalsa,
|
||||
_zalsa: &mut Zalsa,
|
||||
first_index: IngredientIndex,
|
||||
_dependencies: IngredientIndices,
|
||||
) -> Vec<Box<dyn Ingredient>> {
|
||||
vec![Box::new(IngredientImpl::<C>::new(first_index)) as _]
|
||||
}
|
||||
|
@ -297,7 +296,8 @@ where
|
|||
/// the database ends up trying to intern or allocate a new value.
|
||||
pub fn intern<'db, Key>(
|
||||
&'db self,
|
||||
db: &'db dyn crate::Database,
|
||||
zalsa: &'db Zalsa,
|
||||
zalsa_local: &'db ZalsaLocal,
|
||||
key: Key,
|
||||
assemble: impl FnOnce(Id, Key) -> C::Fields<'db>,
|
||||
) -> C::Struct<'db>
|
||||
|
@ -305,7 +305,7 @@ where
|
|||
Key: Hash,
|
||||
C::Fields<'db>: HashEqLike<Key>,
|
||||
{
|
||||
FromId::from_id(self.intern_id(db, key, assemble))
|
||||
FromId::from_id(self.intern_id(zalsa, zalsa_local, key, assemble))
|
||||
}
|
||||
|
||||
/// Intern data to a unique reference.
|
||||
|
@ -320,7 +320,8 @@ where
|
|||
/// the database ends up trying to intern or allocate a new value.
|
||||
pub fn intern_id<'db, Key>(
|
||||
&'db self,
|
||||
db: &'db dyn crate::Database,
|
||||
zalsa: &'db Zalsa,
|
||||
zalsa_local: &'db ZalsaLocal,
|
||||
key: Key,
|
||||
assemble: impl FnOnce(Id, Key) -> C::Fields<'db>,
|
||||
) -> crate::Id
|
||||
|
@ -332,8 +333,6 @@ where
|
|||
// so instead we go with this and transmute the lifetime in the `eq` closure
|
||||
C::Fields<'db>: HashEqLike<Key>,
|
||||
{
|
||||
let (zalsa, zalsa_local) = db.zalsas();
|
||||
|
||||
// Record the current revision as active.
|
||||
let current_revision = zalsa.current_revision();
|
||||
self.revision_queue.record(current_revision);
|
||||
|
@ -416,7 +415,6 @@ where
|
|||
// Fill up the table for the first few revisions without attempting garbage collection.
|
||||
if !self.revision_queue.is_primed() {
|
||||
return self.intern_id_cold(
|
||||
db,
|
||||
key,
|
||||
zalsa,
|
||||
zalsa_local,
|
||||
|
@ -530,16 +528,16 @@ where
|
|||
// Insert the new value into the ID map.
|
||||
shard.key_map.insert_unique(hash, new_id, hasher);
|
||||
|
||||
// Free the memos associated with the previous interned value.
|
||||
//
|
||||
// SAFETY: We hold the lock for the shard containing the value, and the
|
||||
// value has not been interned in the current revision, so no references to
|
||||
// it can exist.
|
||||
let mut memo_table = unsafe { std::mem::take(&mut *value.memos.get()) };
|
||||
let memo_table = unsafe { &mut *value.memos.get() };
|
||||
|
||||
// Free the memos associated with the previous interned value.
|
||||
//
|
||||
// SAFETY: The memo table belongs to a value that we allocated, so it has the
|
||||
// correct type.
|
||||
unsafe { self.clear_memos(zalsa, &mut memo_table, new_id) };
|
||||
unsafe { self.clear_memos(zalsa, memo_table, new_id) };
|
||||
|
||||
if value_shared.is_reusable::<C>() {
|
||||
// Move the value to the front of the LRU list.
|
||||
|
@ -553,16 +551,7 @@ where
|
|||
}
|
||||
|
||||
// If we could not find any stale slots, we are forced to allocate a new one.
|
||||
self.intern_id_cold(
|
||||
db,
|
||||
key,
|
||||
zalsa,
|
||||
zalsa_local,
|
||||
assemble,
|
||||
shard,
|
||||
shard_index,
|
||||
hash,
|
||||
)
|
||||
self.intern_id_cold(key, zalsa, zalsa_local, assemble, shard, shard_index, hash)
|
||||
}
|
||||
|
||||
/// The cold path for interning a value, allocating a new slot.
|
||||
|
@ -571,7 +560,6 @@ where
|
|||
#[allow(clippy::too_many_arguments)]
|
||||
fn intern_id_cold<'db, Key>(
|
||||
&'db self,
|
||||
_db: &'db dyn crate::Database,
|
||||
key: Key,
|
||||
zalsa: &Zalsa,
|
||||
zalsa_local: &ZalsaLocal,
|
||||
|
@ -595,10 +583,12 @@ where
|
|||
.unwrap_or((Durability::MAX, Revision::max()));
|
||||
|
||||
// Allocate the value slot.
|
||||
let id = zalsa_local.allocate(zalsa, self.ingredient_index, |id| Value::<C> {
|
||||
let (id, value) = zalsa_local.allocate(zalsa, self.ingredient_index, |id| Value::<C> {
|
||||
shard: shard_index as u16,
|
||||
link: LinkedListLink::new(),
|
||||
memos: UnsafeCell::new(MemoTable::default()),
|
||||
// SAFETY: We only ever access the memos of a value that we allocated through
|
||||
// our `MemoTableTypes`.
|
||||
memos: UnsafeCell::new(unsafe { MemoTable::new(self.memo_table_types()) }),
|
||||
// SAFETY: We call `from_internal_data` to restore the correct lifetime before access.
|
||||
fields: UnsafeCell::new(unsafe { self.to_internal_data(assemble(id, key)) }),
|
||||
shared: UnsafeCell::new(ValueShared {
|
||||
|
@ -608,7 +598,6 @@ where
|
|||
}),
|
||||
});
|
||||
|
||||
let value = zalsa.table().get::<Value<C>>(id);
|
||||
// SAFETY: We hold the lock for the shard containing the value.
|
||||
let value_shared = unsafe { &mut *value.shared.get() };
|
||||
|
||||
|
@ -696,6 +685,9 @@ where
|
|||
};
|
||||
|
||||
std::mem::forget(table_guard);
|
||||
|
||||
// Reset the table after having dropped any memos.
|
||||
memo_table.reset();
|
||||
}
|
||||
|
||||
// Hashes the value by its fields.
|
||||
|
@ -742,8 +734,7 @@ where
|
|||
}
|
||||
|
||||
/// Lookup the data for an interned value based on its ID.
|
||||
pub fn data<'db>(&'db self, db: &'db dyn Database, id: Id) -> &'db C::Fields<'db> {
|
||||
let zalsa = db.zalsa();
|
||||
pub fn data<'db>(&'db self, zalsa: &'db Zalsa, id: Id) -> &'db C::Fields<'db> {
|
||||
let value = zalsa.table().get::<Value<C>>(id);
|
||||
|
||||
debug_assert!(
|
||||
|
@ -768,12 +759,12 @@ where
|
|||
/// Lookup the fields from an interned struct.
|
||||
///
|
||||
/// Note that this is not "leaking" since no dependency edge is required.
|
||||
pub fn fields<'db>(&'db self, db: &'db dyn Database, s: C::Struct<'db>) -> &'db C::Fields<'db> {
|
||||
self.data(db, AsId::as_id(&s))
|
||||
pub fn fields<'db>(&'db self, zalsa: &'db Zalsa, s: C::Struct<'db>) -> &'db C::Fields<'db> {
|
||||
self.data(zalsa, AsId::as_id(&s))
|
||||
}
|
||||
|
||||
pub fn reset(&mut self, db: &mut dyn Database) {
|
||||
_ = db.zalsa_mut();
|
||||
pub fn reset(&mut self, zalsa_mut: &mut Zalsa) {
|
||||
_ = zalsa_mut;
|
||||
|
||||
for shard in self.shards.iter() {
|
||||
// We can clear the key maps now that we have cancelled all other handles.
|
||||
|
@ -783,11 +774,8 @@ where
|
|||
|
||||
#[cfg(feature = "salsa_unstable")]
|
||||
/// Returns all data corresponding to the interned 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>>()
|
||||
pub fn entries<'db>(&'db self, zalsa: &'db Zalsa) -> impl Iterator<Item = &'db Value<C>> {
|
||||
zalsa.table().slots_of::<Value<C>>()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -805,13 +793,12 @@ where
|
|||
|
||||
unsafe fn maybe_changed_after(
|
||||
&self,
|
||||
db: &dyn Database,
|
||||
zalsa: &crate::zalsa::Zalsa,
|
||||
_db: crate::database::RawDatabase<'_>,
|
||||
input: Id,
|
||||
_revision: Revision,
|
||||
_cycle_heads: &mut CycleHeads,
|
||||
_cycle_heads: &mut CycleHeadKeys,
|
||||
) -> VerifyResult {
|
||||
let zalsa = db.zalsa();
|
||||
|
||||
// Record the current revision as active.
|
||||
let current_revision = zalsa.current_revision();
|
||||
self.revision_queue.record(current_revision);
|
||||
|
@ -849,13 +836,17 @@ where
|
|||
C::DEBUG_NAME
|
||||
}
|
||||
|
||||
fn memo_table_types(&self) -> Arc<MemoTableTypes> {
|
||||
self.memo_table_types.clone()
|
||||
fn memo_table_types(&self) -> &Arc<MemoTableTypes> {
|
||||
&self.memo_table_types
|
||||
}
|
||||
|
||||
fn memo_table_types_mut(&mut self) -> &mut Arc<MemoTableTypes> {
|
||||
&mut self.memo_table_types
|
||||
}
|
||||
|
||||
/// Returns memory usage information about any interned values.
|
||||
#[cfg(all(not(feature = "shuttle"), feature = "salsa_unstable"))]
|
||||
fn memory_usage(&self, db: &dyn Database) -> Option<Vec<crate::database::SlotInfo>> {
|
||||
fn memory_usage(&self, db: &dyn crate::Database) -> Option<Vec<crate::database::SlotInfo>> {
|
||||
use parking_lot::lock_api::RawMutex;
|
||||
|
||||
for shard in self.shards.iter() {
|
||||
|
@ -864,7 +855,7 @@ where
|
|||
}
|
||||
|
||||
let memory_usage = self
|
||||
.entries(db)
|
||||
.entries(db.zalsa())
|
||||
// SAFETY: The memo table belongs to a value that we allocated, so it
|
||||
// has the correct type. Additionally, we are holding the locks for all shards.
|
||||
.map(|value| unsafe { value.memory_usage(&self.memo_table_types) })
|
||||
|
|
12
src/key.rs
12
src/key.rs
|
@ -1,9 +1,9 @@
|
|||
use core::fmt;
|
||||
|
||||
use crate::cycle::CycleHeads;
|
||||
use crate::cycle::CycleHeadKeys;
|
||||
use crate::function::VerifyResult;
|
||||
use crate::zalsa::{IngredientIndex, Zalsa};
|
||||
use crate::{Database, Id};
|
||||
use crate::Id;
|
||||
|
||||
// ANCHOR: DatabaseKeyIndex
|
||||
/// An integer that uniquely identifies a particular query instance within the
|
||||
|
@ -36,16 +36,18 @@ impl DatabaseKeyIndex {
|
|||
|
||||
pub(crate) fn maybe_changed_after(
|
||||
&self,
|
||||
db: &dyn Database,
|
||||
db: crate::database::RawDatabase<'_>,
|
||||
zalsa: &Zalsa,
|
||||
last_verified_at: crate::Revision,
|
||||
cycle_heads: &mut CycleHeads,
|
||||
cycle_heads: &mut CycleHeadKeys,
|
||||
) -> VerifyResult {
|
||||
// SAFETY: The `db` belongs to the ingredient
|
||||
unsafe {
|
||||
// here, `db` has to be either the correct type already, or a subtype (as far as trait
|
||||
// hierarchy is concerned)
|
||||
zalsa
|
||||
.lookup_ingredient(self.ingredient_index())
|
||||
.maybe_changed_after(db, self.key_index(), last_verified_at, cycle_heads)
|
||||
.maybe_changed_after(zalsa, db, self.key_index(), last_verified_at, cycle_heads)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
33
src/lib.rs
33
src/lib.rs
|
@ -1,6 +1,7 @@
|
|||
#![deny(clippy::undocumented_unsafe_blocks)]
|
||||
#![forbid(unsafe_op_in_unsafe_fn)]
|
||||
|
||||
#[cfg(feature = "accumulator")]
|
||||
mod accumulator;
|
||||
mod active_query;
|
||||
mod attach;
|
||||
|
@ -14,13 +15,11 @@ mod function;
|
|||
mod hash;
|
||||
mod id;
|
||||
mod ingredient;
|
||||
mod ingredient_cache;
|
||||
mod input;
|
||||
mod interned;
|
||||
mod key;
|
||||
mod memo_ingredient_indices;
|
||||
mod nonce;
|
||||
#[cfg(feature = "rayon")]
|
||||
mod parallel;
|
||||
mod return_mode;
|
||||
mod revision;
|
||||
mod runtime;
|
||||
|
@ -28,12 +27,19 @@ mod salsa_struct;
|
|||
mod storage;
|
||||
mod sync;
|
||||
mod table;
|
||||
mod tracing;
|
||||
mod tracked_struct;
|
||||
mod update;
|
||||
mod views;
|
||||
mod zalsa;
|
||||
mod zalsa_local;
|
||||
|
||||
#[cfg(not(feature = "inventory"))]
|
||||
mod nonce;
|
||||
|
||||
#[cfg(feature = "rayon")]
|
||||
mod parallel;
|
||||
|
||||
#[cfg(feature = "rayon")]
|
||||
pub use parallel::{join, par_map};
|
||||
#[cfg(feature = "macros")]
|
||||
|
@ -42,11 +48,12 @@ pub use salsa_macros::{accumulator, db, input, interned, tracked, Supertype, Upd
|
|||
#[cfg(feature = "salsa_unstable")]
|
||||
pub use self::database::IngredientInfo;
|
||||
|
||||
#[cfg(feature = "accumulator")]
|
||||
pub use self::accumulator::Accumulator;
|
||||
pub use self::active_query::Backtrace;
|
||||
pub use self::cancelled::Cancelled;
|
||||
pub use self::cycle::CycleRecoveryAction;
|
||||
pub use self::database::{AsDynDatabase, Database};
|
||||
pub use self::database::Database;
|
||||
pub use self::database_impl::DatabaseImpl;
|
||||
pub use self::durability::Durability;
|
||||
pub use self::event::{Event, EventKind};
|
||||
|
@ -63,7 +70,9 @@ pub use self::zalsa::IngredientIndex;
|
|||
pub use crate::attach::{attach, with_attached_database};
|
||||
|
||||
pub mod prelude {
|
||||
pub use crate::{Accumulator, Database, Setter};
|
||||
#[cfg(feature = "accumulator")]
|
||||
pub use crate::accumulator::Accumulator;
|
||||
pub use crate::{Database, Setter};
|
||||
}
|
||||
|
||||
/// Internal names used by salsa macros.
|
||||
|
@ -76,13 +85,16 @@ pub mod plumbing {
|
|||
pub use std::any::TypeId;
|
||||
pub use std::option::Option::{self, None, Some};
|
||||
|
||||
#[cfg(feature = "accumulator")]
|
||||
pub use salsa_macro_rules::setup_accumulator_impl;
|
||||
pub use salsa_macro_rules::{
|
||||
macro_if, maybe_backdate, maybe_default, maybe_default_tt, return_mode_expression,
|
||||
return_mode_ty, setup_accumulator_impl, setup_input_struct, setup_interned_struct,
|
||||
gate_accumulated, macro_if, maybe_backdate, maybe_default, maybe_default_tt,
|
||||
return_mode_expression, return_mode_ty, setup_input_struct, setup_interned_struct,
|
||||
setup_tracked_assoc_fn_body, setup_tracked_fn, setup_tracked_method_body,
|
||||
setup_tracked_struct, unexpected_cycle_initial, unexpected_cycle_recovery,
|
||||
};
|
||||
|
||||
#[cfg(feature = "accumulator")]
|
||||
pub use crate::accumulator::Accumulator;
|
||||
pub use crate::attach::{attach, with_attached_database};
|
||||
pub use crate::cycle::{CycleRecoveryAction, CycleRecoveryStrategy};
|
||||
|
@ -90,6 +102,7 @@ pub mod plumbing {
|
|||
pub use crate::durability::Durability;
|
||||
pub use crate::id::{AsId, FromId, FromIdWithDb, Id};
|
||||
pub use crate::ingredient::{Ingredient, Jar, Location};
|
||||
pub use crate::ingredient_cache::IngredientCache;
|
||||
pub use crate::key::DatabaseKeyIndex;
|
||||
pub use crate::memo_ingredient_indices::{
|
||||
IngredientIndices, MemoIngredientIndices, MemoIngredientMap, MemoIngredientSingletonIndex,
|
||||
|
@ -99,14 +112,18 @@ pub mod plumbing {
|
|||
pub use crate::runtime::{stamp, Runtime, Stamp};
|
||||
pub use crate::salsa_struct::SalsaStructInDb;
|
||||
pub use crate::storage::{HasStorage, Storage};
|
||||
pub use crate::table::memo::MemoTableWithTypes;
|
||||
pub use crate::tracked_struct::TrackedStructInDb;
|
||||
pub use crate::update::helper::{Dispatch as UpdateDispatch, Fallback as UpdateFallback};
|
||||
pub use crate::update::{always_update, Update};
|
||||
pub use crate::views::DatabaseDownCaster;
|
||||
pub use crate::zalsa::{
|
||||
transmute_data_ptr, views, IngredientCache, IngredientIndex, Zalsa, ZalsaDatabase,
|
||||
register_jar, transmute_data_ptr, views, ErasedJar, HasJar, IngredientIndex, JarKind,
|
||||
Zalsa, ZalsaDatabase,
|
||||
};
|
||||
pub use crate::zalsa_local::ZalsaLocal;
|
||||
|
||||
#[cfg(feature = "accumulator")]
|
||||
pub mod accumulator {
|
||||
pub use crate::accumulator::{IngredientImpl, JarImpl};
|
||||
}
|
||||
|
|
|
@ -49,11 +49,11 @@ pub trait NewMemoIngredientIndices {
|
|||
///
|
||||
/// The memo types must be correct.
|
||||
unsafe fn create(
|
||||
zalsa: &Zalsa,
|
||||
zalsa: &mut Zalsa,
|
||||
struct_indices: IngredientIndices,
|
||||
ingredient: IngredientIndex,
|
||||
memo_type: MemoEntryType,
|
||||
intern_ingredient_memo_types: Option<Arc<MemoTableTypes>>,
|
||||
intern_ingredient_memo_types: Option<&mut Arc<MemoTableTypes>>,
|
||||
) -> Self;
|
||||
}
|
||||
|
||||
|
@ -62,34 +62,39 @@ impl NewMemoIngredientIndices for MemoIngredientIndices {
|
|||
///
|
||||
/// The memo types must be correct.
|
||||
unsafe fn create(
|
||||
zalsa: &Zalsa,
|
||||
zalsa: &mut Zalsa,
|
||||
struct_indices: IngredientIndices,
|
||||
ingredient: IngredientIndex,
|
||||
memo_type: MemoEntryType,
|
||||
_intern_ingredient_memo_types: Option<Arc<MemoTableTypes>>,
|
||||
_intern_ingredient_memo_types: Option<&mut Arc<MemoTableTypes>>,
|
||||
) -> Self {
|
||||
debug_assert!(
|
||||
_intern_ingredient_memo_types.is_none(),
|
||||
"intern ingredient can only have a singleton memo ingredient"
|
||||
);
|
||||
|
||||
let Some(&last) = struct_indices.indices.last() else {
|
||||
unreachable!("Attempting to construct struct memo mapping for non tracked function?")
|
||||
};
|
||||
|
||||
let mut indices = Vec::new();
|
||||
indices.resize(
|
||||
(last.as_u32() as usize) + 1,
|
||||
MemoIngredientIndex::from_usize((u32::MAX - 1) as usize),
|
||||
);
|
||||
|
||||
for &struct_ingredient in &struct_indices.indices {
|
||||
let memo_types = zalsa
|
||||
.lookup_ingredient(struct_ingredient)
|
||||
.memo_table_types();
|
||||
let memo_ingredient_index =
|
||||
zalsa.next_memo_ingredient_index(struct_ingredient, ingredient);
|
||||
indices[struct_ingredient.as_u32() as usize] = memo_ingredient_index;
|
||||
|
||||
let mi = zalsa.next_memo_ingredient_index(struct_ingredient, ingredient);
|
||||
memo_types.set(mi, &memo_type);
|
||||
let (struct_ingredient, _) = zalsa.lookup_ingredient_mut(struct_ingredient);
|
||||
let memo_types = Arc::get_mut(struct_ingredient.memo_table_types_mut())
|
||||
.expect("memo tables are not shared until database initialization is complete");
|
||||
|
||||
indices[struct_ingredient.as_u32() as usize] = mi;
|
||||
memo_types.set(memo_ingredient_index, memo_type);
|
||||
}
|
||||
|
||||
MemoIngredientIndices {
|
||||
indices: indices.into_boxed_slice(),
|
||||
}
|
||||
|
@ -146,25 +151,27 @@ impl MemoIngredientMap for MemoIngredientSingletonIndex {
|
|||
impl NewMemoIngredientIndices for MemoIngredientSingletonIndex {
|
||||
#[inline]
|
||||
unsafe fn create(
|
||||
zalsa: &Zalsa,
|
||||
zalsa: &mut Zalsa,
|
||||
indices: IngredientIndices,
|
||||
ingredient: IngredientIndex,
|
||||
memo_type: MemoEntryType,
|
||||
intern_ingredient_memo_types: Option<Arc<MemoTableTypes>>,
|
||||
intern_ingredient_memo_types: Option<&mut Arc<MemoTableTypes>>,
|
||||
) -> Self {
|
||||
let &[struct_ingredient] = &*indices.indices else {
|
||||
unreachable!("Attempting to construct struct memo mapping from enum?")
|
||||
};
|
||||
|
||||
let memo_ingredient_index = zalsa.next_memo_ingredient_index(struct_ingredient, ingredient);
|
||||
let memo_types = intern_ingredient_memo_types.unwrap_or_else(|| {
|
||||
zalsa
|
||||
.lookup_ingredient(struct_ingredient)
|
||||
.memo_table_types()
|
||||
let (struct_ingredient, _) = zalsa.lookup_ingredient_mut(struct_ingredient);
|
||||
struct_ingredient.memo_table_types_mut()
|
||||
});
|
||||
|
||||
let mi = zalsa.next_memo_ingredient_index(struct_ingredient, ingredient);
|
||||
memo_types.set(mi, &memo_type);
|
||||
Self(mi)
|
||||
Arc::get_mut(memo_types)
|
||||
.expect("memo tables are not shared until database initialization is complete")
|
||||
.set(memo_ingredient_index, memo_type);
|
||||
|
||||
Self(memo_ingredient_index)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,44 +1,91 @@
|
|||
use rayon::iter::{FromParallelIterator, IntoParallelIterator, ParallelIterator};
|
||||
|
||||
use crate::Database;
|
||||
use crate::{database::RawDatabase, views::DatabaseDownCaster, Database};
|
||||
|
||||
pub fn par_map<Db, F, T, R, C>(db: &Db, inputs: impl IntoParallelIterator<Item = T>, op: F) -> C
|
||||
where
|
||||
Db: Database + ?Sized,
|
||||
Db: Database + ?Sized + Send,
|
||||
F: Fn(&Db, T) -> R + Sync + Send,
|
||||
T: Send,
|
||||
R: Send + Sync,
|
||||
C: FromParallelIterator<R>,
|
||||
{
|
||||
let views = db.zalsa().views();
|
||||
let caster = &views.downcaster_for::<Db>();
|
||||
let db_caster = &views.downcaster_for::<dyn Database>();
|
||||
inputs
|
||||
.into_par_iter()
|
||||
.map_with(DbForkOnClone(db.fork_db()), |db, element| {
|
||||
op(db.0.as_view(), element)
|
||||
})
|
||||
.map_with(
|
||||
DbForkOnClone(db.fork_db(), caster, db_caster),
|
||||
|db, element| op(db.as_view(), element),
|
||||
)
|
||||
.collect()
|
||||
}
|
||||
|
||||
struct DbForkOnClone(Box<dyn Database>);
|
||||
struct DbForkOnClone<'views, Db: Database + ?Sized>(
|
||||
RawDatabase<'static>,
|
||||
&'views DatabaseDownCaster<Db>,
|
||||
&'views DatabaseDownCaster<dyn Database>,
|
||||
);
|
||||
|
||||
impl Clone for DbForkOnClone {
|
||||
fn clone(&self) -> Self {
|
||||
DbForkOnClone(self.0.fork_db())
|
||||
// SAFETY: `T: Send` -> `&own T: Send`, `DbForkOnClone` is an owning pointer
|
||||
unsafe impl<Db: Send + Database + ?Sized> Send for DbForkOnClone<'_, Db> {}
|
||||
|
||||
impl<Db: Database + ?Sized> DbForkOnClone<'_, Db> {
|
||||
fn as_view(&self) -> &Db {
|
||||
// SAFETY: The downcaster ensures that the pointer is valid for the lifetime of the view.
|
||||
unsafe { self.1.downcast_unchecked(self.0) }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn join<A, B, RA, RB, Db: Database + ?Sized>(db: &Db, a: A, b: B) -> (RA, RB)
|
||||
impl<Db: Database + ?Sized> Drop for DbForkOnClone<'_, Db> {
|
||||
fn drop(&mut self) {
|
||||
// SAFETY: `caster` is derived from a `db` fitting for our database clone
|
||||
let db = unsafe { self.1.downcast_mut_unchecked(self.0) };
|
||||
// SAFETY: `db` has been box allocated and leaked by `fork_db`
|
||||
_ = unsafe { Box::from_raw(db) };
|
||||
}
|
||||
}
|
||||
|
||||
impl<Db: Database + ?Sized> Clone for DbForkOnClone<'_, Db> {
|
||||
fn clone(&self) -> Self {
|
||||
DbForkOnClone(
|
||||
// SAFETY: `caster` is derived from a `db` fitting for our database clone
|
||||
unsafe { self.2.downcast_unchecked(self.0) }.fork_db(),
|
||||
self.1,
|
||||
self.2,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn join<A, B, RA, RB, Db: Send + Database + ?Sized>(db: &Db, a: A, b: B) -> (RA, RB)
|
||||
where
|
||||
A: FnOnce(&Db) -> RA + Send,
|
||||
B: FnOnce(&Db) -> RB + Send,
|
||||
RA: Send,
|
||||
RB: Send,
|
||||
{
|
||||
#[derive(Copy, Clone)]
|
||||
struct AssertSend<T>(T);
|
||||
// SAFETY: We send owning pointers over, which are Send, given the `Db` type parameter above is Send
|
||||
unsafe impl<T> Send for AssertSend<T> {}
|
||||
|
||||
let caster = &db.zalsa().views().downcaster_for::<Db>();
|
||||
// we need to fork eagerly, as `rayon::join_context` gives us no option to tell whether we get
|
||||
// moved to another thread before the closure is executed
|
||||
let db_a = db.fork_db();
|
||||
let db_b = db.fork_db();
|
||||
rayon::join(
|
||||
move || a(db_a.as_view::<Db>()),
|
||||
move || b(db_b.as_view::<Db>()),
|
||||
)
|
||||
let db_a = AssertSend(db.fork_db());
|
||||
let db_b = AssertSend(db.fork_db());
|
||||
let res = rayon::join(
|
||||
// SAFETY: `caster` is derived from a `db` fitting for our database clone
|
||||
move || a(unsafe { caster.downcast_unchecked({ db_a }.0) }),
|
||||
// SAFETY: `caster` is derived from a `db` fitting for our database clone
|
||||
move || b(unsafe { caster.downcast_unchecked({ db_b }.0) }),
|
||||
);
|
||||
|
||||
// SAFETY: `db` has been box allocated and leaked by `fork_db`
|
||||
// FIXME: Clean this mess up, RAII
|
||||
_ = unsafe { Box::from_raw(caster.downcast_mut_unchecked(db_a.0)) };
|
||||
// SAFETY: `db` has been box allocated and leaked by `fork_db`
|
||||
_ = unsafe { Box::from_raw(caster.downcast_mut_unchecked(db_b.0)) };
|
||||
res
|
||||
}
|
||||
|
|
|
@ -86,7 +86,7 @@ impl Running<'_> {
|
|||
})
|
||||
});
|
||||
|
||||
tracing::debug!(
|
||||
crate::tracing::debug!(
|
||||
"block_on: thread {thread_id:?} is blocking on {database_key:?} in thread {other_id:?}",
|
||||
);
|
||||
|
||||
|
@ -180,7 +180,7 @@ impl Runtime {
|
|||
}
|
||||
|
||||
pub(crate) fn set_cancellation_flag(&self) {
|
||||
tracing::trace!("set_cancellation_flag");
|
||||
crate::tracing::trace!("set_cancellation_flag");
|
||||
self.revision_canceled.store(true, Ordering::Release);
|
||||
}
|
||||
|
||||
|
@ -206,7 +206,7 @@ impl Runtime {
|
|||
let r_old = self.current_revision();
|
||||
let r_new = r_old.next();
|
||||
self.revisions[0] = r_new;
|
||||
tracing::debug!("new_revision: {r_old:?} -> {r_new:?}");
|
||||
crate::tracing::debug!("new_revision: {r_old:?} -> {r_new:?}");
|
||||
r_new
|
||||
}
|
||||
|
||||
|
@ -236,7 +236,7 @@ impl Runtime {
|
|||
let dg = self.dependency_graph.lock();
|
||||
|
||||
if dg.depends_on(other_id, thread_id) {
|
||||
tracing::debug!("block_on: cycle detected for {database_key:?} in thread {thread_id:?} on {other_id:?}");
|
||||
crate::tracing::debug!("block_on: cycle detected for {database_key:?} in thread {thread_id:?} on {other_id:?}");
|
||||
return BlockResult::Cycle { same_thread: false };
|
||||
}
|
||||
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
use std::any::TypeId;
|
||||
|
||||
use crate::memo_ingredient_indices::{IngredientIndices, MemoIngredientMap};
|
||||
use crate::table::memo::MemoTableWithTypes;
|
||||
use crate::zalsa::Zalsa;
|
||||
use crate::Id;
|
||||
use crate::{Id, Revision};
|
||||
|
||||
pub trait SalsaStructInDb: Sized {
|
||||
type MemoIngredientMap: MemoIngredientMap;
|
||||
|
@ -16,7 +17,7 @@ pub trait SalsaStructInDb: Sized {
|
|||
/// While implementors of this trait may call [`crate::zalsa::JarEntry::get_or_create`]
|
||||
/// to create the ingredient, they aren't required to. For example, supertypes recursively
|
||||
/// call [`crate::zalsa::JarEntry::get_or_create`] for their variants and combine them.
|
||||
fn lookup_or_create_ingredient_index(zalsa: &Zalsa) -> IngredientIndices;
|
||||
fn lookup_ingredient_index(zalsa: &Zalsa) -> IngredientIndices;
|
||||
|
||||
/// Plumbing to support nested salsa supertypes.
|
||||
///
|
||||
|
@ -62,4 +63,16 @@ pub trait SalsaStructInDb: Sized {
|
|||
/// Why `TypeId` and not `IngredientIndex`? Because it's cheaper and easier: the `TypeId` is readily
|
||||
/// available at compile time, while the `IngredientIndex` requires a runtime lookup.
|
||||
fn cast(id: Id, type_id: TypeId) -> Option<Self>;
|
||||
|
||||
/// Return the memo table associated with `id`.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The parameter `current_revision` must be the current revision of the owner of database
|
||||
/// owning this table.
|
||||
unsafe fn memo_table(
|
||||
zalsa: &Zalsa,
|
||||
id: Id,
|
||||
current_revision: Revision,
|
||||
) -> MemoTableWithTypes<'_>;
|
||||
}
|
||||
|
|
|
@ -2,8 +2,9 @@
|
|||
use std::marker::PhantomData;
|
||||
use std::panic::RefUnwindSafe;
|
||||
|
||||
use crate::database::RawDatabase;
|
||||
use crate::sync::{Arc, Condvar, Mutex};
|
||||
use crate::zalsa::{Zalsa, ZalsaDatabase};
|
||||
use crate::zalsa::{ErasedJar, HasJar, Zalsa, ZalsaDatabase};
|
||||
use crate::zalsa_local::{self, ZalsaLocal};
|
||||
use crate::{Database, Event, EventKind};
|
||||
|
||||
|
@ -42,8 +43,15 @@ impl<Db: Database> Default for StorageHandle<Db> {
|
|||
|
||||
impl<Db: Database> StorageHandle<Db> {
|
||||
pub fn new(event_callback: Option<Box<dyn Fn(crate::Event) + Send + Sync + 'static>>) -> Self {
|
||||
Self::with_jars(event_callback, Vec::new())
|
||||
}
|
||||
|
||||
fn with_jars(
|
||||
event_callback: Option<Box<dyn Fn(crate::Event) + Send + Sync + 'static>>,
|
||||
jars: Vec<ErasedJar>,
|
||||
) -> Self {
|
||||
Self {
|
||||
zalsa_impl: Arc::new(Zalsa::new::<Db>(event_callback)),
|
||||
zalsa_impl: Arc::new(Zalsa::new::<Db>(event_callback, jars)),
|
||||
coordinate: CoordinateDrop(Arc::new(Coordinate {
|
||||
clones: Mutex::new(1),
|
||||
cvar: Default::default(),
|
||||
|
@ -115,6 +123,11 @@ impl<Db: Database> Storage<Db> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Returns a builder for database storage.
|
||||
pub fn builder() -> StorageBuilder<Db> {
|
||||
StorageBuilder::default()
|
||||
}
|
||||
|
||||
/// Convert this instance of [`Storage`] into a [`StorageHandle`].
|
||||
///
|
||||
/// This will discard the local state of this [`Storage`], thereby returning a value that
|
||||
|
@ -168,6 +181,54 @@ impl<Db: Database> Storage<Db> {
|
|||
// ANCHOR_END: cancel_other_workers
|
||||
}
|
||||
|
||||
/// A builder for a [`Storage`] instance.
|
||||
///
|
||||
/// This type can be created with the [`Storage::builder`] function.
|
||||
pub struct StorageBuilder<Db> {
|
||||
jars: Vec<ErasedJar>,
|
||||
event_callback: Option<Box<dyn Fn(crate::Event) + Send + Sync + 'static>>,
|
||||
_db: PhantomData<Db>,
|
||||
}
|
||||
|
||||
impl<Db> Default for StorageBuilder<Db> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
jars: Vec::new(),
|
||||
event_callback: None,
|
||||
_db: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Db: Database> StorageBuilder<Db> {
|
||||
/// Set a callback for salsa events.
|
||||
///
|
||||
/// The `event_callback` function will be invoked by the salsa runtime at various points during execution.
|
||||
pub fn event_callback(
|
||||
mut self,
|
||||
callback: Box<dyn Fn(crate::Event) + Send + Sync + 'static>,
|
||||
) -> Self {
|
||||
self.event_callback = Some(callback);
|
||||
self
|
||||
}
|
||||
|
||||
/// Manually register an ingredient.
|
||||
///
|
||||
/// Manual ingredient registration is necessary when the `inventory` feature is disabled.
|
||||
pub fn ingredient<I: HasJar>(mut self) -> Self {
|
||||
self.jars.push(ErasedJar::erase::<I>());
|
||||
self
|
||||
}
|
||||
|
||||
/// Construct the [`Storage`] using the provided builder options.
|
||||
pub fn build(self) -> Storage<Db> {
|
||||
Storage {
|
||||
handle: StorageHandle::with_jars(self.event_callback, self.jars),
|
||||
zalsa_local: ZalsaLocal::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::undocumented_unsafe_blocks)] // TODO(#697) document safety
|
||||
unsafe impl<T: HasStorage> ZalsaDatabase for T {
|
||||
#[inline(always)]
|
||||
|
@ -185,8 +246,8 @@ unsafe impl<T: HasStorage> ZalsaDatabase for T {
|
|||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn fork_db(&self) -> Box<dyn Database> {
|
||||
Box::new(self.clone())
|
||||
fn fork_db(&self) -> RawDatabase<'static> {
|
||||
Box::leak(Box::new(self.clone())).into()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
96
src/sync.rs
96
src/sync.rs
|
@ -5,40 +5,6 @@ pub mod shim {
|
|||
pub use shuttle::sync::*;
|
||||
pub use shuttle::{thread, thread_local};
|
||||
|
||||
pub mod papaya {
|
||||
use std::hash::{BuildHasher, Hash};
|
||||
use std::marker::PhantomData;
|
||||
|
||||
pub struct HashMap<K, V, S>(super::Mutex<std::collections::HashMap<K, V, S>>);
|
||||
|
||||
impl<K, V, S: Default> Default for HashMap<K, V, S> {
|
||||
fn default() -> Self {
|
||||
Self(super::Mutex::default())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct LocalGuard<'a>(PhantomData<&'a ()>);
|
||||
|
||||
impl<K, V, S> HashMap<K, V, S>
|
||||
where
|
||||
K: Eq + Hash,
|
||||
V: Clone,
|
||||
S: BuildHasher,
|
||||
{
|
||||
pub fn guard(&self) -> LocalGuard<'_> {
|
||||
LocalGuard(PhantomData)
|
||||
}
|
||||
|
||||
pub fn get(&self, key: &K, _guard: &LocalGuard<'_>) -> Option<V> {
|
||||
self.0.lock().get(key).cloned()
|
||||
}
|
||||
|
||||
pub fn insert(&self, key: K, value: V, _guard: &LocalGuard<'_>) {
|
||||
self.0.lock().insert(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A wrapper around shuttle's `Mutex` to mirror parking-lot's API.
|
||||
#[derive(Default, Debug)]
|
||||
pub struct Mutex<T>(shuttle::sync::Mutex<T>);
|
||||
|
@ -57,24 +23,6 @@ pub mod shim {
|
|||
}
|
||||
}
|
||||
|
||||
/// A wrapper around shuttle's `RwLock` to mirror parking-lot's API.
|
||||
#[derive(Default, Debug)]
|
||||
pub struct RwLock<T>(shuttle::sync::RwLock<T>);
|
||||
|
||||
impl<T> RwLock<T> {
|
||||
pub fn read(&self) -> RwLockReadGuard<'_, T> {
|
||||
self.0.read().unwrap()
|
||||
}
|
||||
|
||||
pub fn write(&self) -> RwLockWriteGuard<'_, T> {
|
||||
self.0.write().unwrap()
|
||||
}
|
||||
|
||||
pub fn get_mut(&mut self) -> &mut T {
|
||||
self.0.get_mut().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
/// A wrapper around shuttle's `Condvar` to mirror parking-lot's API.
|
||||
#[derive(Default, Debug)]
|
||||
pub struct Condvar(shuttle::sync::Condvar);
|
||||
|
@ -164,7 +112,7 @@ pub mod shim {
|
|||
|
||||
#[cfg(not(feature = "shuttle"))]
|
||||
pub mod shim {
|
||||
pub use parking_lot::{Mutex, MutexGuard, RwLock};
|
||||
pub use parking_lot::{Mutex, MutexGuard};
|
||||
pub use std::sync::*;
|
||||
pub use std::{thread, thread_local};
|
||||
|
||||
|
@ -173,48 +121,6 @@ pub mod shim {
|
|||
pub use std::sync::atomic::*;
|
||||
}
|
||||
|
||||
pub mod papaya {
|
||||
use std::hash::{BuildHasher, Hash};
|
||||
|
||||
pub use papaya::LocalGuard;
|
||||
|
||||
pub struct HashMap<K, V, S>(papaya::HashMap<K, V, S>);
|
||||
|
||||
impl<K, V, S: Default> Default for HashMap<K, V, S> {
|
||||
fn default() -> Self {
|
||||
Self(
|
||||
papaya::HashMap::builder()
|
||||
.capacity(256) // A relatively large capacity to hopefully avoid resizing.
|
||||
.resize_mode(papaya::ResizeMode::Blocking)
|
||||
.hasher(S::default())
|
||||
.build(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V, S> HashMap<K, V, S>
|
||||
where
|
||||
K: Eq + Hash,
|
||||
V: Clone,
|
||||
S: BuildHasher,
|
||||
{
|
||||
#[inline]
|
||||
pub fn guard(&self) -> LocalGuard<'_> {
|
||||
self.0.guard()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get(&self, key: &K, guard: &LocalGuard<'_>) -> Option<V> {
|
||||
self.0.get(key, guard).cloned()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn insert(&self, key: K, value: V, guard: &LocalGuard<'_>) {
|
||||
self.0.insert(key, value, guard);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A wrapper around parking-lot's `Condvar` to mirror shuttle's API.
|
||||
pub struct Condvar(parking_lot::Condvar);
|
||||
|
||||
|
|
77
src/table.rs
77
src/table.rs
|
@ -34,7 +34,7 @@ pub struct Table {
|
|||
///
|
||||
/// Implementors of this trait need to make sure that their type is unique with respect to
|
||||
/// their owning ingredient as the allocation strategy relies on this.
|
||||
pub(crate) unsafe trait Slot: Any + Send + Sync {
|
||||
pub unsafe trait Slot: Any + Send + Sync {
|
||||
/// Access the [`MemoTable`][] for this slot.
|
||||
///
|
||||
/// # Safety condition
|
||||
|
@ -220,17 +220,42 @@ impl Table {
|
|||
PageIndex::new(self.pages.push(Page::new::<T>(ingredient, memo_types)))
|
||||
}
|
||||
|
||||
/// Get the memo table associated with `id`
|
||||
/// Get the memo table associated with `id` for the concrete type `T`.
|
||||
///
|
||||
/// # Safety condition
|
||||
/// # Safety
|
||||
///
|
||||
/// The parameter `current_revision` MUST be the current revision
|
||||
/// of the owner of database owning this table.
|
||||
pub(crate) unsafe fn memos(
|
||||
/// The parameter `current_revision` must be the current revision of the database
|
||||
/// owning this table.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// If `page` is out of bounds or the type `T` is incorrect.
|
||||
pub unsafe fn memos<T: Slot>(
|
||||
&self,
|
||||
id: Id,
|
||||
current_revision: Revision,
|
||||
) -> MemoTableWithTypes<'_> {
|
||||
let (page, slot) = split_id(id);
|
||||
let page = self.pages[page.0].assert_type::<T>();
|
||||
let slot = &page.data()[slot.0];
|
||||
|
||||
// SAFETY: The caller is required to pass the `current_revision`.
|
||||
let memos = unsafe { slot.memos(current_revision) };
|
||||
|
||||
// SAFETY: The `Page` keeps the correct memo types.
|
||||
unsafe { page.0.memo_types.attach_memos(memos) }
|
||||
}
|
||||
|
||||
/// Get the memo table associated with `id`.
|
||||
///
|
||||
/// Unlike `Table::memos`, this does not require a concrete type, and instead uses dynamic
|
||||
/// dispatch.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The parameter `current_revision` must be the current revision of the owner of database
|
||||
/// owning this table.
|
||||
pub unsafe fn dyn_memos(&self, id: Id, current_revision: Revision) -> MemoTableWithTypes<'_> {
|
||||
let (page, slot) = split_id(id);
|
||||
let page = &self.pages[page.0];
|
||||
// SAFETY: We supply a proper slot pointer and the caller is required to pass the `current_revision`.
|
||||
|
@ -261,6 +286,8 @@ impl Table {
|
|||
.flat_map(|view| view.data())
|
||||
}
|
||||
|
||||
#[cold]
|
||||
#[inline(never)]
|
||||
pub(crate) fn fetch_or_push_page<T: Slot>(
|
||||
&self,
|
||||
ingredient: IngredientIndex,
|
||||
|
@ -274,6 +301,7 @@ impl Table {
|
|||
{
|
||||
return page;
|
||||
}
|
||||
|
||||
self.push_page::<T>(ingredient, memo_types())
|
||||
}
|
||||
|
||||
|
@ -286,22 +314,23 @@ impl Table {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'p, T: Slot> PageView<'p, T> {
|
||||
impl<'db, T: Slot> PageView<'db, T> {
|
||||
#[inline]
|
||||
fn page_data(&self) -> &'p [PageDataEntry<T>] {
|
||||
fn page_data(&self) -> &'db [PageDataEntry<T>] {
|
||||
let len = self.0.allocated.load(Ordering::Acquire);
|
||||
// SAFETY: `len` is the initialized length of the page
|
||||
unsafe { slice::from_raw_parts(self.0.data.cast::<PageDataEntry<T>>().as_ptr(), len) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn data(&self) -> &'p [T] {
|
||||
fn data(&self) -> &'db [T] {
|
||||
let len = self.0.allocated.load(Ordering::Acquire);
|
||||
// SAFETY: `len` is the initialized length of the page
|
||||
unsafe { slice::from_raw_parts(self.0.data.cast::<T>().as_ptr(), len) }
|
||||
}
|
||||
|
||||
pub(crate) fn allocate<V>(&self, page: PageIndex, value: V) -> Result<Id, V>
|
||||
#[inline]
|
||||
pub(crate) fn allocate<V>(&self, page: PageIndex, value: V) -> Result<(Id, &'db T), V>
|
||||
where
|
||||
V: FnOnce(Id) -> T,
|
||||
{
|
||||
|
@ -322,11 +351,14 @@ impl<'p, T: Slot> PageView<'p, T> {
|
|||
// interior
|
||||
unsafe { (*entry.get()).write(value(id)) };
|
||||
|
||||
// SAFETY: We just initialized the value above.
|
||||
let value = unsafe { (*entry.get()).assume_init_ref() };
|
||||
|
||||
// Update the length (this must be done after initialization as otherwise an uninitialized
|
||||
// read could occur!)
|
||||
self.0.allocated.store(index + 1, Ordering::Release);
|
||||
|
||||
Ok(id)
|
||||
Ok((id, value))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -373,6 +405,7 @@ impl Page {
|
|||
slot.0 < len,
|
||||
"out of bounds access `{slot:?}` (maximum slot `{len}`)"
|
||||
);
|
||||
|
||||
// SAFETY: We have checked that the resulting pointer will be within bounds.
|
||||
unsafe {
|
||||
self.data
|
||||
|
@ -383,13 +416,10 @@ impl Page {
|
|||
|
||||
#[inline]
|
||||
fn assert_type<T: Slot>(&self) -> PageView<'_, T> {
|
||||
assert_eq!(
|
||||
self.slot_type_id,
|
||||
TypeId::of::<T>(),
|
||||
"page has slot type `{:?}` but `{:?}` was expected",
|
||||
self.slot_type_name,
|
||||
std::any::type_name::<T>(),
|
||||
);
|
||||
if self.slot_type_id != TypeId::of::<T>() {
|
||||
type_assert_failed::<T>(self);
|
||||
}
|
||||
|
||||
PageView(self, PhantomData)
|
||||
}
|
||||
|
||||
|
@ -403,6 +433,17 @@ impl Page {
|
|||
}
|
||||
}
|
||||
|
||||
/// This function is explicitly outlined to avoid debug machinery in the hot-path.
|
||||
#[cold]
|
||||
#[inline(never)]
|
||||
fn type_assert_failed<T: 'static>(page: &Page) -> ! {
|
||||
panic!(
|
||||
"page has slot type `{:?}` but `{:?}` was expected",
|
||||
page.slot_type_name,
|
||||
std::any::type_name::<T>(),
|
||||
)
|
||||
}
|
||||
|
||||
impl Drop for Page {
|
||||
fn drop(&mut self) {
|
||||
let len = *self.allocated.get_mut();
|
||||
|
|
|
@ -3,19 +3,39 @@ use std::fmt::Debug;
|
|||
use std::mem;
|
||||
use std::ptr::{self, NonNull};
|
||||
|
||||
use portable_atomic::hint::spin_loop;
|
||||
use thin_vec::ThinVec;
|
||||
|
||||
use crate::sync::atomic::{AtomicPtr, Ordering};
|
||||
use crate::sync::{OnceLock, RwLock};
|
||||
use crate::{zalsa::MemoIngredientIndex, zalsa_local::QueryOriginRef};
|
||||
|
||||
/// The "memo table" stores the memoized results of tracked function calls.
|
||||
/// Every tracked function must take a salsa struct as its first argument
|
||||
/// and memo tables are attached to those salsa structs as auxiliary data.
|
||||
#[derive(Default)]
|
||||
pub(crate) struct MemoTable {
|
||||
memos: RwLock<ThinVec<MemoEntry>>,
|
||||
pub struct MemoTable {
|
||||
memos: Box<[MemoEntry]>,
|
||||
}
|
||||
|
||||
impl MemoTable {
|
||||
/// Create a `MemoTable` with slots for memos from the provided `MemoTableTypes`.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The created memo table must only be accessed with the same `MemoTableTypes`.
|
||||
pub unsafe fn new(types: &MemoTableTypes) -> Self {
|
||||
// Note that the safety invariant guarantees that any indices in-bounds for
|
||||
// this table are also in-bounds for its `MemoTableTypes`, as `MemoTableTypes`
|
||||
// is append-only.
|
||||
Self {
|
||||
memos: (0..types.len()).map(|_| MemoEntry::default()).collect(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Reset any memos in the table.
|
||||
///
|
||||
/// Note that the memo entries should be freed manually before calling this function.
|
||||
pub fn reset(&mut self) {
|
||||
for memo in &mut self.memos {
|
||||
*memo = MemoEntry::default();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Memo: Any + Send + Sync {
|
||||
|
@ -50,13 +70,8 @@ struct MemoEntry {
|
|||
atomic_memo: AtomicPtr<DummyMemo>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct MemoEntryType {
|
||||
data: OnceLock<MemoEntryTypeData>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
struct MemoEntryTypeData {
|
||||
pub struct MemoEntryType {
|
||||
/// The `type_id` of the erased memo type `M`
|
||||
type_id: TypeId,
|
||||
|
||||
|
@ -89,17 +104,10 @@ impl MemoEntryType {
|
|||
#[inline]
|
||||
pub fn of<M: Memo>() -> Self {
|
||||
Self {
|
||||
data: OnceLock::from(MemoEntryTypeData {
|
||||
type_id: TypeId::of::<M>(),
|
||||
to_dyn_fn: Self::to_dyn_fn::<M>(),
|
||||
}),
|
||||
type_id: TypeId::of::<M>(),
|
||||
to_dyn_fn: Self::to_dyn_fn::<M>(),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn load(&self) -> Option<&MemoEntryTypeData> {
|
||||
self.data.get()
|
||||
}
|
||||
}
|
||||
|
||||
/// Dummy placeholder type that we use when erasing the memo type `M` in [`MemoEntryData`][].
|
||||
|
@ -127,43 +135,21 @@ impl Memo for DummyMemo {
|
|||
|
||||
#[derive(Default)]
|
||||
pub struct MemoTableTypes {
|
||||
types: boxcar::Vec<MemoEntryType>,
|
||||
types: Vec<MemoEntryType>,
|
||||
}
|
||||
|
||||
impl MemoTableTypes {
|
||||
pub(crate) fn set(
|
||||
&self,
|
||||
&mut self,
|
||||
memo_ingredient_index: MemoIngredientIndex,
|
||||
memo_type: &MemoEntryType,
|
||||
memo_type: MemoEntryType,
|
||||
) {
|
||||
let memo_ingredient_index = memo_ingredient_index.as_usize();
|
||||
self.types
|
||||
.insert(memo_ingredient_index.as_usize(), memo_type);
|
||||
}
|
||||
|
||||
// Try to create our entry if it has not already been created.
|
||||
if memo_ingredient_index >= self.types.count() {
|
||||
while self.types.push(MemoEntryType::default()) < memo_ingredient_index {}
|
||||
}
|
||||
|
||||
loop {
|
||||
let Some(memo_entry_type) = self.types.get(memo_ingredient_index) else {
|
||||
// It's possible that someone else began pushing to our index but has not
|
||||
// completed the entry's initialization yet, as `boxcar` is lock-free. This
|
||||
// is extremely unlikely given initialization is just a handful of instructions.
|
||||
// Additionally, this function is generally only called on startup, so we can
|
||||
// just spin here.
|
||||
spin_loop();
|
||||
continue;
|
||||
};
|
||||
|
||||
memo_entry_type
|
||||
.data
|
||||
.set(
|
||||
*memo_type.data.get().expect(
|
||||
"cannot provide an empty `MemoEntryType` for `MemoEntryType::set()`",
|
||||
),
|
||||
)
|
||||
.expect("memo type should only be set once");
|
||||
break;
|
||||
}
|
||||
pub fn len(&self) -> usize {
|
||||
self.types.len()
|
||||
}
|
||||
|
||||
/// # Safety
|
||||
|
@ -189,7 +175,7 @@ impl MemoTableTypes {
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) struct MemoTableWithTypes<'a> {
|
||||
pub struct MemoTableWithTypes<'a> {
|
||||
types: &'a MemoTableTypes,
|
||||
memos: &'a MemoTable,
|
||||
}
|
||||
|
@ -200,97 +186,62 @@ impl MemoTableWithTypes<'_> {
|
|||
memo_ingredient_index: MemoIngredientIndex,
|
||||
memo: NonNull<M>,
|
||||
) -> Option<NonNull<M>> {
|
||||
// The type must already exist, we insert it when creating the memo ingredient.
|
||||
assert_eq!(
|
||||
let MemoEntry { atomic_memo } = self.memos.memos.get(memo_ingredient_index.as_usize())?;
|
||||
|
||||
// SAFETY: Any indices that are in-bounds for the `MemoTable` are also in-bounds for its
|
||||
// corresponding `MemoTableTypes`, by construction.
|
||||
let type_ = unsafe {
|
||||
self.types
|
||||
.types
|
||||
.get(memo_ingredient_index.as_usize())
|
||||
.and_then(MemoEntryType::load)?
|
||||
.type_id,
|
||||
TypeId::of::<M>(),
|
||||
"inconsistent type-id for `{memo_ingredient_index:?}`"
|
||||
);
|
||||
.get_unchecked(memo_ingredient_index.as_usize())
|
||||
};
|
||||
|
||||
// If the memo slot is already occupied, it must already have the
|
||||
// right type info etc, and we only need the read-lock.
|
||||
if let Some(MemoEntry { atomic_memo }) = self
|
||||
.memos
|
||||
.memos
|
||||
.read()
|
||||
.get(memo_ingredient_index.as_usize())
|
||||
{
|
||||
let old_memo =
|
||||
atomic_memo.swap(MemoEntryType::to_dummy(memo).as_ptr(), Ordering::AcqRel);
|
||||
|
||||
let old_memo = NonNull::new(old_memo);
|
||||
|
||||
// SAFETY: `type_id` check asserted above
|
||||
return old_memo.map(|old_memo| unsafe { MemoEntryType::from_dummy(old_memo) });
|
||||
// Verify that the we are casting to the correct type.
|
||||
if type_.type_id != TypeId::of::<M>() {
|
||||
type_assert_failed(memo_ingredient_index);
|
||||
}
|
||||
|
||||
// Otherwise we need the write lock.
|
||||
self.insert_cold(memo_ingredient_index, memo)
|
||||
}
|
||||
|
||||
#[cold]
|
||||
fn insert_cold<M: Memo>(
|
||||
self,
|
||||
memo_ingredient_index: MemoIngredientIndex,
|
||||
memo: NonNull<M>,
|
||||
) -> Option<NonNull<M>> {
|
||||
let memo_ingredient_index = memo_ingredient_index.as_usize();
|
||||
let mut memos = self.memos.memos.write();
|
||||
|
||||
// Grow the table if needed.
|
||||
if memos.len() <= memo_ingredient_index {
|
||||
let additional_len = memo_ingredient_index - memos.len() + 1;
|
||||
memos.reserve(additional_len);
|
||||
while memos.len() <= memo_ingredient_index {
|
||||
memos.push(MemoEntry::default());
|
||||
}
|
||||
}
|
||||
|
||||
let old_entry = mem::replace(
|
||||
memos[memo_ingredient_index].atomic_memo.get_mut(),
|
||||
MemoEntryType::to_dummy(memo).as_ptr(),
|
||||
);
|
||||
|
||||
// SAFETY: The `TypeId` is asserted in `insert()`.
|
||||
NonNull::new(old_entry).map(|memo| unsafe { MemoEntryType::from_dummy(memo) })
|
||||
let old_memo = atomic_memo.swap(MemoEntryType::to_dummy(memo).as_ptr(), Ordering::AcqRel);
|
||||
|
||||
// SAFETY: We asserted that the type is correct above.
|
||||
NonNull::new(old_memo).map(|old_memo| unsafe { MemoEntryType::from_dummy(old_memo) })
|
||||
}
|
||||
|
||||
/// Returns a pointer to the memo at the given index, if one has been inserted.
|
||||
#[inline]
|
||||
pub(crate) fn get<M: Memo>(
|
||||
self,
|
||||
memo_ingredient_index: MemoIngredientIndex,
|
||||
) -> Option<NonNull<M>> {
|
||||
let read = self.memos.memos.read();
|
||||
let memo = read.get(memo_ingredient_index.as_usize())?;
|
||||
let type_ = self
|
||||
.types
|
||||
.types
|
||||
.get(memo_ingredient_index.as_usize())
|
||||
.and_then(MemoEntryType::load)?;
|
||||
assert_eq!(
|
||||
type_.type_id,
|
||||
TypeId::of::<M>(),
|
||||
"inconsistent type-id for `{memo_ingredient_index:?}`"
|
||||
);
|
||||
let memo = NonNull::new(memo.atomic_memo.load(Ordering::Acquire))?;
|
||||
// SAFETY: `type_id` check asserted above
|
||||
Some(unsafe { MemoEntryType::from_dummy(memo) })
|
||||
let MemoEntry { atomic_memo } = self.memos.memos.get(memo_ingredient_index.as_usize())?;
|
||||
|
||||
// SAFETY: Any indices that are in-bounds for the `MemoTable` are also in-bounds for its
|
||||
// corresponding `MemoTableTypes`, by construction.
|
||||
let type_ = unsafe {
|
||||
self.types
|
||||
.types
|
||||
.get_unchecked(memo_ingredient_index.as_usize())
|
||||
};
|
||||
|
||||
// Verify that the we are casting to the correct type.
|
||||
if type_.type_id != TypeId::of::<M>() {
|
||||
type_assert_failed(memo_ingredient_index);
|
||||
}
|
||||
|
||||
NonNull::new(atomic_memo.load(Ordering::Acquire))
|
||||
// SAFETY: We asserted that the type is correct above.
|
||||
.map(|memo| unsafe { MemoEntryType::from_dummy(memo) })
|
||||
}
|
||||
|
||||
#[cfg(feature = "salsa_unstable")]
|
||||
pub(crate) fn memory_usage(&self) -> Vec<crate::database::MemoInfo> {
|
||||
let mut memory_usage = Vec::new();
|
||||
let memos = self.memos.memos.read();
|
||||
for (index, memo) in memos.iter().enumerate() {
|
||||
for (index, memo) in self.memos.memos.iter().enumerate() {
|
||||
let Some(memo) = NonNull::new(memo.atomic_memo.load(Ordering::Acquire)) else {
|
||||
continue;
|
||||
};
|
||||
|
||||
let Some(type_) = self.types.types.get(index).and_then(MemoEntryType::load) else {
|
||||
let Some(type_) = self.types.types.get(index) else {
|
||||
continue;
|
||||
};
|
||||
|
||||
|
@ -317,32 +268,30 @@ impl MemoTableWithTypesMut<'_> {
|
|||
memo_ingredient_index: MemoIngredientIndex,
|
||||
f: impl FnOnce(&mut M),
|
||||
) {
|
||||
let Some(type_) = self
|
||||
.types
|
||||
.types
|
||||
.get(memo_ingredient_index.as_usize())
|
||||
.and_then(MemoEntryType::load)
|
||||
let Some(MemoEntry { atomic_memo }) =
|
||||
self.memos.memos.get_mut(memo_ingredient_index.as_usize())
|
||||
else {
|
||||
return;
|
||||
};
|
||||
assert_eq!(
|
||||
type_.type_id,
|
||||
TypeId::of::<M>(),
|
||||
"inconsistent type-id for `{memo_ingredient_index:?}`"
|
||||
);
|
||||
|
||||
// If the memo slot is already occupied, it must already have the
|
||||
// right type info etc, and we only need the read-lock.
|
||||
let memos = self.memos.memos.get_mut();
|
||||
let Some(MemoEntry { atomic_memo }) = memos.get_mut(memo_ingredient_index.as_usize())
|
||||
else {
|
||||
return;
|
||||
// SAFETY: Any indices that are in-bounds for the `MemoTable` are also in-bounds for its
|
||||
// corresponding `MemoTableTypes`, by construction.
|
||||
let type_ = unsafe {
|
||||
self.types
|
||||
.types
|
||||
.get_unchecked(memo_ingredient_index.as_usize())
|
||||
};
|
||||
|
||||
// Verify that the we are casting to the correct type.
|
||||
if type_.type_id != TypeId::of::<M>() {
|
||||
type_assert_failed(memo_ingredient_index);
|
||||
}
|
||||
|
||||
let Some(memo) = NonNull::new(*atomic_memo.get_mut()) else {
|
||||
return;
|
||||
};
|
||||
|
||||
// SAFETY: `type_id` check asserted above
|
||||
// SAFETY: We asserted that the type is correct above.
|
||||
f(unsafe { MemoEntryType::from_dummy(memo).as_mut() });
|
||||
}
|
||||
|
||||
|
@ -357,7 +306,7 @@ impl MemoTableWithTypesMut<'_> {
|
|||
#[inline]
|
||||
pub unsafe fn drop(&mut self) {
|
||||
let types = self.types.types.iter();
|
||||
for ((_, type_), memo) in std::iter::zip(types, self.memos.memos.get_mut()) {
|
||||
for (type_, memo) in std::iter::zip(types, &mut self.memos.memos) {
|
||||
// SAFETY: The types match as per our constructor invariant.
|
||||
unsafe { memo.take(type_) };
|
||||
}
|
||||
|
@ -371,12 +320,12 @@ impl MemoTableWithTypesMut<'_> {
|
|||
&mut self,
|
||||
mut f: impl FnMut(MemoIngredientIndex, Box<dyn Memo>),
|
||||
) {
|
||||
let memos = self.memos.memos.get_mut();
|
||||
memos
|
||||
self.memos
|
||||
.memos
|
||||
.iter_mut()
|
||||
.zip(self.types.types.iter())
|
||||
.enumerate()
|
||||
.filter_map(|(index, (memo, (_, type_)))| {
|
||||
.filter_map(|(index, (memo, type_))| {
|
||||
// SAFETY: The types match as per our constructor invariant.
|
||||
let memo = unsafe { memo.take(type_)? };
|
||||
Some((MemoIngredientIndex::from_usize(index), memo))
|
||||
|
@ -385,6 +334,13 @@ impl MemoTableWithTypesMut<'_> {
|
|||
}
|
||||
}
|
||||
|
||||
/// This function is explicitly outlined to avoid debug machinery in the hot-path.
|
||||
#[cold]
|
||||
#[inline(never)]
|
||||
fn type_assert_failed(memo_ingredient_index: MemoIngredientIndex) -> ! {
|
||||
panic!("inconsistent type-id for `{memo_ingredient_index:?}`")
|
||||
}
|
||||
|
||||
impl MemoEntry {
|
||||
/// # Safety
|
||||
///
|
||||
|
@ -393,7 +349,6 @@ impl MemoEntry {
|
|||
unsafe fn take(&mut self, type_: &MemoEntryType) -> Option<Box<dyn Memo>> {
|
||||
let memo = mem::replace(self.atomic_memo.get_mut(), ptr::null_mut());
|
||||
let memo = NonNull::new(memo)?;
|
||||
let type_ = type_.load()?;
|
||||
// SAFETY: Our preconditions.
|
||||
Some(unsafe { Box::from_raw((type_.to_dyn_fn)(memo).as_ptr()) })
|
||||
}
|
||||
|
|
54
src/tracing.rs
Normal file
54
src/tracing.rs
Normal file
|
@ -0,0 +1,54 @@
|
|||
//! Wrappers around `tracing` macros that avoid inlining debug machinery into the hot path,
|
||||
//! as tracing events are typically only enabled for debugging purposes.
|
||||
|
||||
macro_rules! trace {
|
||||
($($x:tt)*) => {
|
||||
crate::tracing::event!(TRACE, $($x)*)
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! info {
|
||||
($($x:tt)*) => {
|
||||
crate::tracing::event!(INFO, $($x)*)
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! debug {
|
||||
($($x:tt)*) => {
|
||||
crate::tracing::event!(DEBUG, $($x)*)
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! debug_span {
|
||||
($($x:tt)*) => {
|
||||
crate::tracing::span!(DEBUG, $($x)*)
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! event {
|
||||
($level:ident, $($x:tt)*) => {{
|
||||
let event = {
|
||||
#[cold] #[inline(never)] || { ::tracing::event!(::tracing::Level::$level, $($x)*) }
|
||||
};
|
||||
|
||||
if ::tracing::enabled!(::tracing::Level::$level) {
|
||||
event();
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
||||
macro_rules! span {
|
||||
($level:ident, $($x:tt)*) => {{
|
||||
let span = {
|
||||
#[cold] #[inline(never)] || { ::tracing::span!(::tracing::Level::$level, $($x)*) }
|
||||
};
|
||||
|
||||
if ::tracing::enabled!(::tracing::Level::$level) {
|
||||
span()
|
||||
} else {
|
||||
::tracing::Span::none()
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
||||
pub(crate) use {debug, debug_span, event, info, span, trace};
|
|
@ -10,7 +10,7 @@ use crossbeam_queue::SegQueue;
|
|||
use thin_vec::ThinVec;
|
||||
use tracked_field::FieldIngredientImpl;
|
||||
|
||||
use crate::cycle::CycleHeads;
|
||||
use crate::cycle::CycleHeadKeys;
|
||||
use crate::function::VerifyResult;
|
||||
use crate::id::{AsId, FromId};
|
||||
use crate::ingredient::{Ingredient, Jar};
|
||||
|
@ -23,7 +23,7 @@ use crate::sync::Arc;
|
|||
use crate::table::memo::{MemoTable, MemoTableTypes, MemoTableWithTypesMut};
|
||||
use crate::table::{Slot, Table};
|
||||
use crate::zalsa::{IngredientIndex, Zalsa};
|
||||
use crate::{Database, Durability, Event, EventKind, Id, Revision};
|
||||
use crate::{Durability, Event, EventKind, Id, Revision};
|
||||
|
||||
pub mod tracked_field;
|
||||
|
||||
|
@ -110,9 +110,8 @@ impl<C: Configuration> Default for JarImpl<C> {
|
|||
|
||||
impl<C: Configuration> Jar for JarImpl<C> {
|
||||
fn create_ingredients(
|
||||
_zalsa: &Zalsa,
|
||||
_zalsa: &mut Zalsa,
|
||||
struct_index: crate::zalsa::IngredientIndex,
|
||||
_dependencies: crate::memo_ingredient_indices::IngredientIndices,
|
||||
) -> Vec<Box<dyn Ingredient>> {
|
||||
let struct_ingredient = <IngredientImpl<C>>::new(struct_index);
|
||||
|
||||
|
@ -376,11 +375,10 @@ where
|
|||
|
||||
pub fn new_struct<'db>(
|
||||
&'db self,
|
||||
db: &'db dyn Database,
|
||||
zalsa: &'db Zalsa,
|
||||
zalsa_local: &'db ZalsaLocal,
|
||||
mut fields: C::Fields<'db>,
|
||||
) -> C::Struct<'db> {
|
||||
let (zalsa, zalsa_local) = db.zalsas();
|
||||
|
||||
let identity_hash = IdentityHash {
|
||||
ingredient_index: self.ingredient_index,
|
||||
hash: crate::hash::hash(&C::untracked_fields(&fields)),
|
||||
|
@ -398,7 +396,7 @@ where
|
|||
if let Some(id) = zalsa_local.tracked_struct_id(&identity) {
|
||||
// The struct already exists in the intern map.
|
||||
let index = self.database_key_index(id);
|
||||
tracing::trace!("Reuse tracked struct {id:?}", id = index);
|
||||
crate::tracing::trace!("Reuse tracked struct {id:?}", id = index);
|
||||
zalsa_local.add_output(index);
|
||||
|
||||
// SAFETY: The `id` was present in the interned map, so the value must be initialized.
|
||||
|
@ -424,7 +422,7 @@ where
|
|||
// in the struct map.
|
||||
let id = self.allocate(zalsa, zalsa_local, current_revision, ¤t_deps, fields);
|
||||
let key = self.database_key_index(id);
|
||||
tracing::trace!("Allocated new tracked struct {key:?}");
|
||||
crate::tracing::trace!("Allocated new tracked struct {key:?}");
|
||||
zalsa_local.add_output(key);
|
||||
zalsa_local.store_tracked_struct_id(identity, id);
|
||||
FromId::from_id(id)
|
||||
|
@ -444,7 +442,9 @@ where
|
|||
// lifetime erase for storage
|
||||
fields: unsafe { mem::transmute::<C::Fields<'db>, C::Fields<'static>>(fields) },
|
||||
revisions: C::new_revisions(current_deps.changed_at),
|
||||
memos: Default::default(),
|
||||
// SAFETY: We only ever access the memos of a value that we allocated through
|
||||
// our `MemoTableTypes`.
|
||||
memos: unsafe { MemoTable::new(self.memo_table_types()) },
|
||||
};
|
||||
|
||||
while let Some(id) = self.free_list.pop() {
|
||||
|
@ -454,7 +454,7 @@ where
|
|||
// If the generation would overflow, we are forced to leak the slot. Note that this
|
||||
// shouldn't be a problem in general as sufficient bits are reserved for the generation.
|
||||
let Some(id) = id.next_generation() else {
|
||||
tracing::info!(
|
||||
crate::tracing::info!(
|
||||
"leaking tracked struct {:?} due to generation overflow",
|
||||
self.database_key_index(id)
|
||||
);
|
||||
|
@ -477,7 +477,9 @@ where
|
|||
return id;
|
||||
}
|
||||
|
||||
zalsa_local.allocate::<Value<C>>(zalsa, self.ingredient_index, value)
|
||||
let (id, _) = zalsa_local.allocate::<Value<C>>(zalsa, self.ingredient_index, value);
|
||||
|
||||
id
|
||||
}
|
||||
|
||||
/// Get mutable access to the data for `id` -- this holds a write lock for the duration
|
||||
|
@ -554,7 +556,7 @@ where
|
|||
// the unlikely case that the ID is already at its maximum generation, we are forced to leak
|
||||
// the previous slot and allocate a new value.
|
||||
if id.generation() == u32::MAX {
|
||||
tracing::info!(
|
||||
crate::tracing::info!(
|
||||
"leaking tracked struct {:?} due to generation overflow",
|
||||
self.database_key_index(id)
|
||||
);
|
||||
|
@ -601,11 +603,11 @@ where
|
|||
// Note that we hold the lock and have exclusive access to the tracked struct data,
|
||||
// so there should be no live instances of IDs from the previous generation. We clear
|
||||
// the memos and return a new ID here as if we have allocated a new slot.
|
||||
let mut table = data.take_memo_table();
|
||||
let memo_table = data.memo_table_mut();
|
||||
|
||||
// SAFETY: The memo table belongs to a value that we allocated, so it has the
|
||||
// correct type.
|
||||
unsafe { self.clear_memos(zalsa, &mut table, id) };
|
||||
unsafe { self.clear_memos(zalsa, memo_table, id) };
|
||||
|
||||
id = id
|
||||
.next_generation()
|
||||
|
@ -674,11 +676,11 @@ where
|
|||
|
||||
// SAFETY: We have acquired the write lock
|
||||
let data = unsafe { &mut *data_raw };
|
||||
let mut memo_table = data.take_memo_table();
|
||||
let memo_table = data.memo_table_mut();
|
||||
|
||||
// SAFETY: The memo table belongs to a value that we allocated, so it
|
||||
// has the correct type.
|
||||
unsafe { self.clear_memos(zalsa, &mut memo_table, id) };
|
||||
unsafe { self.clear_memos(zalsa, memo_table, id) };
|
||||
|
||||
// now that all cleanup has occurred, make available for re-use
|
||||
self.free_list.push(id);
|
||||
|
@ -724,17 +726,20 @@ where
|
|||
};
|
||||
|
||||
mem::forget(table_guard);
|
||||
|
||||
// Reset the table after having dropped any memos.
|
||||
memo_table.reset();
|
||||
}
|
||||
|
||||
/// Return reference to the field data ignoring dependency tracking.
|
||||
/// Used for debugging.
|
||||
pub fn leak_fields<'db>(
|
||||
&'db self,
|
||||
db: &'db dyn Database,
|
||||
zalsa: &'db Zalsa,
|
||||
s: C::Struct<'db>,
|
||||
) -> &'db C::Fields<'db> {
|
||||
let id = AsId::as_id(&s);
|
||||
let data = Self::data(db.zalsa().table(), id);
|
||||
let data = Self::data(zalsa.table(), id);
|
||||
data.fields()
|
||||
}
|
||||
|
||||
|
@ -744,11 +749,11 @@ where
|
|||
/// The caller is responsible for selecting the appropriate element.
|
||||
pub fn tracked_field<'db>(
|
||||
&'db self,
|
||||
db: &'db dyn crate::Database,
|
||||
zalsa: &'db Zalsa,
|
||||
zalsa_local: &'db ZalsaLocal,
|
||||
s: C::Struct<'db>,
|
||||
relative_tracked_index: usize,
|
||||
) -> &'db C::Fields<'db> {
|
||||
let (zalsa, zalsa_local) = db.zalsas();
|
||||
let id = AsId::as_id(&s);
|
||||
let field_ingredient_index = self.ingredient_index.successor(relative_tracked_index);
|
||||
let data = Self::data(zalsa.table(), id);
|
||||
|
@ -772,10 +777,9 @@ where
|
|||
/// The caller is responsible for selecting the appropriate element.
|
||||
pub fn untracked_field<'db>(
|
||||
&'db self,
|
||||
db: &'db dyn crate::Database,
|
||||
zalsa: &'db Zalsa,
|
||||
s: C::Struct<'db>,
|
||||
) -> &'db C::Fields<'db> {
|
||||
let zalsa = db.zalsa();
|
||||
let id = AsId::as_id(&s);
|
||||
let data = Self::data(zalsa.table(), id);
|
||||
|
||||
|
@ -790,11 +794,8 @@ where
|
|||
|
||||
#[cfg(feature = "salsa_unstable")]
|
||||
/// Returns all data corresponding to the tracked 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>>()
|
||||
pub fn entries<'db>(&'db self, zalsa: &'db Zalsa) -> impl Iterator<Item = &'db Value<C>> {
|
||||
zalsa.table().slots_of::<Value<C>>()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -812,10 +813,11 @@ where
|
|||
|
||||
unsafe fn maybe_changed_after(
|
||||
&self,
|
||||
_db: &dyn Database,
|
||||
_zalsa: &crate::zalsa::Zalsa,
|
||||
_db: crate::database::RawDatabase<'_>,
|
||||
_input: Id,
|
||||
_revision: Revision,
|
||||
_cycle_heads: &mut CycleHeads,
|
||||
_cycle_heads: &mut CycleHeadKeys,
|
||||
) -> VerifyResult {
|
||||
// Any change to a tracked struct results in a new ID generation.
|
||||
VerifyResult::unchanged()
|
||||
|
@ -849,15 +851,19 @@ where
|
|||
C::DEBUG_NAME
|
||||
}
|
||||
|
||||
fn memo_table_types(&self) -> Arc<MemoTableTypes> {
|
||||
self.memo_table_types.clone()
|
||||
fn memo_table_types(&self) -> &Arc<MemoTableTypes> {
|
||||
&self.memo_table_types
|
||||
}
|
||||
|
||||
fn memo_table_types_mut(&mut self) -> &mut Arc<MemoTableTypes> {
|
||||
&mut self.memo_table_types
|
||||
}
|
||||
|
||||
/// Returns memory usage information about any tracked structs.
|
||||
#[cfg(feature = "salsa_unstable")]
|
||||
fn memory_usage(&self, db: &dyn Database) -> Option<Vec<crate::database::SlotInfo>> {
|
||||
fn memory_usage(&self, db: &dyn crate::Database) -> Option<Vec<crate::database::SlotInfo>> {
|
||||
let memory_usage = self
|
||||
.entries(db)
|
||||
.entries(db.zalsa())
|
||||
// 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) })
|
||||
|
@ -891,13 +897,12 @@ where
|
|||
unsafe { mem::transmute::<&C::Fields<'static>, &C::Fields<'_>>(&self.fields) }
|
||||
}
|
||||
|
||||
fn take_memo_table(&mut self) -> MemoTable {
|
||||
fn memo_table_mut(&mut self) -> &mut MemoTable {
|
||||
// This fn is only called after `updated_at` has been set to `None`;
|
||||
// this ensures that there is no concurrent access
|
||||
// (and that the `&mut self` is accurate...).
|
||||
assert!(self.updated_at.load().is_none());
|
||||
|
||||
mem::take(&mut self.memos)
|
||||
&mut self.memos
|
||||
}
|
||||
|
||||
fn read_lock(&self, current_revision: Revision) {
|
||||
|
@ -949,9 +954,8 @@ where
|
|||
{
|
||||
#[inline(always)]
|
||||
unsafe fn memos(&self, current_revision: Revision) -> &crate::table::memo::MemoTable {
|
||||
// Acquiring the read lock here with the current revision
|
||||
// ensures that there is no danger of a race
|
||||
// when deleting a tracked struct.
|
||||
// Acquiring the read lock here with the current revision to ensure that there
|
||||
// is no danger of a race when deleting a tracked struct.
|
||||
self.read_lock(current_revision);
|
||||
&self.memos
|
||||
}
|
||||
|
@ -971,19 +975,19 @@ mod tests {
|
|||
let mut d = DisambiguatorMap::default();
|
||||
// set up all 4 permutations of differing field values
|
||||
let h1 = IdentityHash {
|
||||
ingredient_index: IngredientIndex::from(0),
|
||||
ingredient_index: IngredientIndex::new(0),
|
||||
hash: 0,
|
||||
};
|
||||
let h2 = IdentityHash {
|
||||
ingredient_index: IngredientIndex::from(1),
|
||||
ingredient_index: IngredientIndex::new(1),
|
||||
hash: 0,
|
||||
};
|
||||
let h3 = IdentityHash {
|
||||
ingredient_index: IngredientIndex::from(0),
|
||||
ingredient_index: IngredientIndex::new(0),
|
||||
hash: 1,
|
||||
};
|
||||
let h4 = IdentityHash {
|
||||
ingredient_index: IngredientIndex::from(1),
|
||||
ingredient_index: IngredientIndex::new(1),
|
||||
hash: 1,
|
||||
};
|
||||
assert_eq!(d.disambiguate(h1), Disambiguator(0));
|
||||
|
@ -1001,42 +1005,42 @@ mod tests {
|
|||
let mut d = IdentityMap::default();
|
||||
// set up all 8 permutations of differing field values
|
||||
let i1 = Identity {
|
||||
ingredient_index: IngredientIndex::from(0),
|
||||
ingredient_index: IngredientIndex::new(0),
|
||||
hash: 0,
|
||||
disambiguator: Disambiguator(0),
|
||||
};
|
||||
let i2 = Identity {
|
||||
ingredient_index: IngredientIndex::from(1),
|
||||
ingredient_index: IngredientIndex::new(1),
|
||||
hash: 0,
|
||||
disambiguator: Disambiguator(0),
|
||||
};
|
||||
let i3 = Identity {
|
||||
ingredient_index: IngredientIndex::from(0),
|
||||
ingredient_index: IngredientIndex::new(0),
|
||||
hash: 1,
|
||||
disambiguator: Disambiguator(0),
|
||||
};
|
||||
let i4 = Identity {
|
||||
ingredient_index: IngredientIndex::from(1),
|
||||
ingredient_index: IngredientIndex::new(1),
|
||||
hash: 1,
|
||||
disambiguator: Disambiguator(0),
|
||||
};
|
||||
let i5 = Identity {
|
||||
ingredient_index: IngredientIndex::from(0),
|
||||
ingredient_index: IngredientIndex::new(0),
|
||||
hash: 0,
|
||||
disambiguator: Disambiguator(1),
|
||||
};
|
||||
let i6 = Identity {
|
||||
ingredient_index: IngredientIndex::from(1),
|
||||
ingredient_index: IngredientIndex::new(1),
|
||||
hash: 0,
|
||||
disambiguator: Disambiguator(1),
|
||||
};
|
||||
let i7 = Identity {
|
||||
ingredient_index: IngredientIndex::from(0),
|
||||
ingredient_index: IngredientIndex::new(0),
|
||||
hash: 1,
|
||||
disambiguator: Disambiguator(1),
|
||||
};
|
||||
let i8 = Identity {
|
||||
ingredient_index: IngredientIndex::from(1),
|
||||
ingredient_index: IngredientIndex::new(1),
|
||||
hash: 1,
|
||||
disambiguator: Disambiguator(1),
|
||||
};
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
use std::marker::PhantomData;
|
||||
|
||||
use crate::cycle::CycleHeads;
|
||||
use crate::cycle::CycleHeadKeys;
|
||||
use crate::function::VerifyResult;
|
||||
use crate::ingredient::Ingredient;
|
||||
use crate::sync::Arc;
|
||||
use crate::table::memo::MemoTableTypes;
|
||||
use crate::tracked_struct::{Configuration, Value};
|
||||
use crate::zalsa::IngredientIndex;
|
||||
use crate::{Database, Id};
|
||||
use crate::Id;
|
||||
|
||||
/// Created for each tracked struct.
|
||||
///
|
||||
|
@ -55,14 +55,14 @@ where
|
|||
self.ingredient_index
|
||||
}
|
||||
|
||||
unsafe fn maybe_changed_after<'db>(
|
||||
&'db self,
|
||||
db: &'db dyn Database,
|
||||
unsafe fn maybe_changed_after(
|
||||
&self,
|
||||
zalsa: &crate::zalsa::Zalsa,
|
||||
_db: crate::database::RawDatabase<'_>,
|
||||
input: Id,
|
||||
revision: crate::Revision,
|
||||
_cycle_heads: &mut CycleHeads,
|
||||
_cycle_heads: &mut CycleHeadKeys,
|
||||
) -> VerifyResult {
|
||||
let zalsa = db.zalsa();
|
||||
let data = <super::IngredientImpl<C>>::data(zalsa.table(), input);
|
||||
let field_changed_at = data.revisions[self.field_index];
|
||||
VerifyResult::changed_if(field_changed_at > revision)
|
||||
|
@ -82,7 +82,11 @@ where
|
|||
C::TRACKED_FIELD_NAMES[self.field_index]
|
||||
}
|
||||
|
||||
fn memo_table_types(&self) -> Arc<MemoTableTypes> {
|
||||
fn memo_table_types(&self) -> &Arc<MemoTableTypes> {
|
||||
unreachable!("tracked field does not allocate pages")
|
||||
}
|
||||
|
||||
fn memo_table_types_mut(&mut self) -> &mut Arc<MemoTableTypes> {
|
||||
unreachable!("tracked field does not allocate pages")
|
||||
}
|
||||
}
|
||||
|
|
118
src/views.rs
118
src/views.rs
|
@ -1,10 +1,15 @@
|
|||
use std::any::{Any, TypeId};
|
||||
use std::{
|
||||
any::{Any, TypeId},
|
||||
marker::PhantomData,
|
||||
mem,
|
||||
ptr::NonNull,
|
||||
};
|
||||
|
||||
use crate::Database;
|
||||
use crate::{database::RawDatabase, Database};
|
||||
|
||||
/// A `Views` struct is associated with some specific database type
|
||||
/// (a `DatabaseImpl<U>` for some existential `U`). It contains functions
|
||||
/// to downcast from `dyn Database` to `dyn DbView` for various traits `DbView` via this specific
|
||||
/// to downcast to `dyn DbView` for various traits `DbView` via this specific
|
||||
/// database type.
|
||||
/// None of these types are known at compilation time, they are all checked
|
||||
/// dynamically through `TypeId` magic.
|
||||
|
@ -13,6 +18,7 @@ pub struct Views {
|
|||
view_casters: boxcar::Vec<ViewCaster>,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
struct ViewCaster {
|
||||
/// The id of the target type `dyn DbView` that we can cast to.
|
||||
target_type_id: TypeId,
|
||||
|
@ -20,50 +26,69 @@ struct ViewCaster {
|
|||
/// The name of the target type `dyn DbView` that we can cast to.
|
||||
type_name: &'static str,
|
||||
|
||||
/// Type-erased function pointer that downcasts from `dyn Database` to `dyn DbView`.
|
||||
/// Type-erased function pointer that downcasts to `dyn DbView`.
|
||||
cast: ErasedDatabaseDownCasterSig,
|
||||
}
|
||||
|
||||
impl ViewCaster {
|
||||
fn new<DbView: ?Sized + Any>(func: unsafe fn(&dyn Database) -> &DbView) -> ViewCaster {
|
||||
fn new<DbView: ?Sized + Any>(func: DatabaseDownCasterSig<DbView>) -> ViewCaster {
|
||||
ViewCaster {
|
||||
target_type_id: TypeId::of::<DbView>(),
|
||||
type_name: std::any::type_name::<DbView>(),
|
||||
// SAFETY: We are type erasing for storage, taking care of unerasing before we call
|
||||
// the function pointer.
|
||||
cast: unsafe {
|
||||
std::mem::transmute::<DatabaseDownCasterSig<DbView>, ErasedDatabaseDownCasterSig>(
|
||||
func,
|
||||
)
|
||||
mem::transmute::<DatabaseDownCasterSig<DbView>, ErasedDatabaseDownCasterSig>(func)
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type ErasedDatabaseDownCasterSig = unsafe fn(&dyn Database) -> *const ();
|
||||
type DatabaseDownCasterSig<DbView> = unsafe fn(&dyn Database) -> &DbView;
|
||||
type ErasedDatabaseDownCasterSig = unsafe fn(RawDatabase<'_>) -> NonNull<()>;
|
||||
type DatabaseDownCasterSig<DbView> = unsafe fn(RawDatabase<'_>) -> NonNull<DbView>;
|
||||
|
||||
pub struct DatabaseDownCaster<DbView: ?Sized>(TypeId, DatabaseDownCasterSig<DbView>);
|
||||
#[repr(transparent)]
|
||||
pub struct DatabaseDownCaster<DbView: ?Sized>(ViewCaster, PhantomData<fn() -> DbView>);
|
||||
|
||||
impl<DbView: ?Sized> Copy for DatabaseDownCaster<DbView> {}
|
||||
impl<DbView: ?Sized> Clone for DatabaseDownCaster<DbView> {
|
||||
fn clone(&self) -> Self {
|
||||
*self
|
||||
}
|
||||
}
|
||||
|
||||
impl<DbView: ?Sized + Any> DatabaseDownCaster<DbView> {
|
||||
pub fn downcast<'db>(&self, db: &'db dyn Database) -> &'db DbView {
|
||||
assert_eq!(
|
||||
self.0,
|
||||
db.type_id(),
|
||||
"Database type does not match the expected type for this `Views` instance"
|
||||
);
|
||||
// SAFETY: We've asserted that the database is correct.
|
||||
unsafe { (self.1)(db) }
|
||||
}
|
||||
|
||||
/// Downcast `db` to `DbView`.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The caller must ensure that `db` is of the correct type.
|
||||
pub unsafe fn downcast_unchecked<'db>(&self, db: &'db dyn Database) -> &'db DbView {
|
||||
#[inline]
|
||||
pub unsafe fn downcast_unchecked<'db>(&self, db: RawDatabase<'db>) -> &'db DbView {
|
||||
// SAFETY: The caller must ensure that `db` is of the correct type.
|
||||
unsafe { (self.1)(db) }
|
||||
// The returned pointer is live for `'db` due to construction of the downcaster functions.
|
||||
unsafe { (self.unerased_downcaster())(db).as_ref() }
|
||||
}
|
||||
/// Downcast `db` to `DbView`.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The caller must ensure that `db` is of the correct type.
|
||||
#[inline]
|
||||
pub unsafe fn downcast_mut_unchecked<'db>(&self, db: RawDatabase<'db>) -> &'db mut DbView {
|
||||
// SAFETY: The caller must ensure that `db` is of the correct type.
|
||||
// The returned pointer is live for `'db` due to construction of the downcaster functions.
|
||||
unsafe { (self.unerased_downcaster())(db).as_mut() }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn unerased_downcaster(&self) -> DatabaseDownCasterSig<DbView> {
|
||||
// SAFETY: The type-erased function pointer is guaranteed to be ABI compatible for `DbView`
|
||||
unsafe {
|
||||
mem::transmute::<ErasedDatabaseDownCasterSig, DatabaseDownCasterSig<DbView>>(
|
||||
self.0.cast,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -71,43 +96,56 @@ impl Views {
|
|||
pub(crate) fn new<Db: Database>() -> Self {
|
||||
let source_type_id = TypeId::of::<Db>();
|
||||
let view_casters = boxcar::Vec::new();
|
||||
// special case the no-op transformation, that way we skip out on reconstructing the wide pointer
|
||||
view_casters.push(ViewCaster::new::<dyn Database>(|db| db));
|
||||
view_casters.push(ViewCaster::new::<dyn Database>(|db| db.ptr.cast::<Db>()));
|
||||
Self {
|
||||
source_type_id,
|
||||
view_casters,
|
||||
}
|
||||
}
|
||||
|
||||
/// Add a new downcaster from `dyn Database` to `dyn DbView`.
|
||||
pub fn add<DbView: ?Sized + Any>(&self, func: DatabaseDownCasterSig<DbView>) {
|
||||
/// Add a new downcaster to `dyn DbView`.
|
||||
pub fn add<Concrete: 'static, DbView: ?Sized + Any>(
|
||||
&self,
|
||||
func: fn(NonNull<Concrete>) -> NonNull<DbView>,
|
||||
) -> &DatabaseDownCaster<DbView> {
|
||||
assert_eq!(self.source_type_id, TypeId::of::<Concrete>());
|
||||
let target_type_id = TypeId::of::<DbView>();
|
||||
if self
|
||||
if let Some((_, caster)) = self
|
||||
.view_casters
|
||||
.iter()
|
||||
.any(|(_, u)| u.target_type_id == target_type_id)
|
||||
.find(|(_, u)| u.target_type_id == target_type_id)
|
||||
{
|
||||
return;
|
||||
// SAFETY: The type-erased function pointer is guaranteed to be valid for `DbView`
|
||||
return unsafe { &*(&raw const *caster).cast::<DatabaseDownCaster<DbView>>() };
|
||||
}
|
||||
self.view_casters.push(ViewCaster::new::<DbView>(func));
|
||||
|
||||
// SAFETY: We are type erasing the function pointer for storage, and we will unerase it
|
||||
// before we call it.
|
||||
let caster = unsafe {
|
||||
mem::transmute::<fn(NonNull<Concrete>) -> NonNull<DbView>, DatabaseDownCasterSig<DbView>>(
|
||||
func,
|
||||
)
|
||||
};
|
||||
let caster = ViewCaster::new::<DbView>(caster);
|
||||
let idx = self.view_casters.push(caster);
|
||||
// SAFETY: The type-erased function pointer is guaranteed to be valid for `DbView`
|
||||
unsafe { &*(&raw const self.view_casters[idx]).cast::<DatabaseDownCaster<DbView>>() }
|
||||
}
|
||||
|
||||
/// Retrieve an downcaster function from `dyn Database` to `dyn DbView`.
|
||||
/// Retrieve an downcaster function to `dyn DbView`.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// If the underlying type of `db` is not the same as the database type this upcasts was created for.
|
||||
pub fn downcaster_for<DbView: ?Sized + Any>(&self) -> DatabaseDownCaster<DbView> {
|
||||
/// If the underlying type of `db` is not the same as the database type this downcasts was created for.
|
||||
pub fn downcaster_for<DbView: ?Sized + Any>(&self) -> &DatabaseDownCaster<DbView> {
|
||||
let view_type_id = TypeId::of::<DbView>();
|
||||
for (_idx, view) in self.view_casters.iter() {
|
||||
for (_, view) in self.view_casters.iter() {
|
||||
if view.target_type_id == view_type_id {
|
||||
// SAFETY: We are unerasing the type erased function pointer having made sure the
|
||||
// TypeId matches.
|
||||
return DatabaseDownCaster(self.source_type_id, unsafe {
|
||||
std::mem::transmute::<ErasedDatabaseDownCasterSig, DatabaseDownCasterSig<DbView>>(
|
||||
view.cast,
|
||||
)
|
||||
});
|
||||
return unsafe {
|
||||
&*((view as *const ViewCaster).cast::<DatabaseDownCaster<DbView>>())
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
|
360
src/zalsa.rs
360
src/zalsa.rs
|
@ -1,18 +1,15 @@
|
|||
use std::any::{Any, TypeId};
|
||||
use std::hash::BuildHasherDefault;
|
||||
use std::marker::PhantomData;
|
||||
use std::mem;
|
||||
use std::num::NonZeroU32;
|
||||
use std::panic::RefUnwindSafe;
|
||||
|
||||
use hashbrown::HashMap;
|
||||
use rustc_hash::FxHashMap;
|
||||
|
||||
use crate::database::RawDatabase;
|
||||
use crate::hash::TypeIdHasher;
|
||||
use crate::ingredient::{Ingredient, Jar};
|
||||
use crate::nonce::{Nonce, NonceGenerator};
|
||||
use crate::plumbing::SalsaStructInDb;
|
||||
use crate::runtime::Runtime;
|
||||
use crate::sync::atomic::{AtomicU64, Ordering};
|
||||
use crate::sync::{papaya, Mutex, RwLock};
|
||||
use crate::table::memo::MemoTableWithTypes;
|
||||
use crate::table::Table;
|
||||
use crate::views::Views;
|
||||
|
@ -56,19 +53,20 @@ pub unsafe trait ZalsaDatabase: Any {
|
|||
|
||||
/// Clone the database.
|
||||
#[doc(hidden)]
|
||||
fn fork_db(&self) -> Box<dyn Database>;
|
||||
fn fork_db(&self) -> RawDatabase<'static>;
|
||||
}
|
||||
|
||||
pub fn views<Db: ?Sized + Database>(db: &Db) -> &Views {
|
||||
db.zalsa().views()
|
||||
}
|
||||
|
||||
/// Nonce type representing the underlying database storage.
|
||||
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
||||
#[cfg(not(feature = "inventory"))]
|
||||
pub struct StorageNonce;
|
||||
|
||||
// Generator for storage nonces.
|
||||
static NONCE: NonceGenerator<StorageNonce> = NonceGenerator::new();
|
||||
#[cfg(not(feature = "inventory"))]
|
||||
static NONCE: crate::nonce::NonceGenerator<StorageNonce> = crate::nonce::NonceGenerator::new();
|
||||
|
||||
/// An ingredient index identifies a particular [`Ingredient`] in the database.
|
||||
///
|
||||
|
@ -83,10 +81,20 @@ impl IngredientIndex {
|
|||
/// This reserves one bit for an optional tag.
|
||||
const MAX_INDEX: u32 = 0x7FFF_FFFF;
|
||||
|
||||
/// Create an ingredient index from a `usize`.
|
||||
pub(crate) fn from(v: usize) -> Self {
|
||||
assert!(v <= Self::MAX_INDEX as usize);
|
||||
Self(v as u32)
|
||||
/// Create an ingredient index from a `u32`.
|
||||
pub(crate) fn new(v: u32) -> Self {
|
||||
assert!(v <= Self::MAX_INDEX);
|
||||
Self(v)
|
||||
}
|
||||
|
||||
/// Create an ingredient index from a `u32`, without performing validating
|
||||
/// that the index is valid.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The index must be less than or equal to `IngredientIndex::MAX_INDEX`.
|
||||
pub(crate) unsafe fn new_unchecked(v: u32) -> Self {
|
||||
Self(v)
|
||||
}
|
||||
|
||||
/// Convert the ingredient index back into a `u32`.
|
||||
|
@ -134,28 +142,24 @@ impl MemoIngredientIndex {
|
|||
pub struct Zalsa {
|
||||
views_of: Views,
|
||||
|
||||
nonce: Nonce<StorageNonce>,
|
||||
#[cfg(not(feature = "inventory"))]
|
||||
nonce: crate::nonce::Nonce<StorageNonce>,
|
||||
|
||||
/// Map from the [`IngredientIndex::as_usize`][] of a salsa struct to a list of
|
||||
/// [ingredient-indices](`IngredientIndex`) for tracked functions that have this salsa struct
|
||||
/// as input.
|
||||
memo_ingredient_indices: RwLock<Vec<Vec<IngredientIndex>>>,
|
||||
memo_ingredient_indices: Vec<Vec<IngredientIndex>>,
|
||||
|
||||
/// Map from the type-id of an `impl Jar` to the index of its first ingredient.
|
||||
jar_map: papaya::HashMap<TypeId, IngredientIndex, BuildHasherDefault<TypeIdHasher>>,
|
||||
|
||||
/// The write-lock for `jar_map`.
|
||||
jar_map_lock: Mutex<()>,
|
||||
jar_map: HashMap<TypeId, IngredientIndex, BuildHasherDefault<TypeIdHasher>>,
|
||||
|
||||
/// A map from the `IngredientIndex` to the `TypeId` of its ID struct.
|
||||
///
|
||||
/// Notably this is not the reverse mapping of `jar_map`.
|
||||
ingredient_to_id_struct_type_id_map: RwLock<FxHashMap<IngredientIndex, TypeId>>,
|
||||
ingredient_to_id_struct_type_id_map: FxHashMap<IngredientIndex, TypeId>,
|
||||
|
||||
/// Vector of ingredients.
|
||||
///
|
||||
/// Immutable unless the mutex on `ingredients_map` is held.
|
||||
ingredients_vec: boxcar::Vec<Box<dyn Ingredient>>,
|
||||
ingredients_vec: Vec<Box<dyn Ingredient>>,
|
||||
|
||||
/// Indices of ingredients that require reset when a new revision starts.
|
||||
ingredients_requiring_reset: boxcar::Vec<IngredientIndex>,
|
||||
|
@ -177,22 +181,43 @@ impl RefUnwindSafe for Zalsa {}
|
|||
impl Zalsa {
|
||||
pub(crate) fn new<Db: Database>(
|
||||
event_callback: Option<Box<dyn Fn(crate::Event) + Send + Sync + 'static>>,
|
||||
jars: Vec<ErasedJar>,
|
||||
) -> Self {
|
||||
Self {
|
||||
let mut zalsa = Self {
|
||||
views_of: Views::new::<Db>(),
|
||||
nonce: NONCE.nonce(),
|
||||
jar_map: papaya::HashMap::default(),
|
||||
jar_map_lock: Mutex::default(),
|
||||
jar_map: HashMap::default(),
|
||||
ingredient_to_id_struct_type_id_map: Default::default(),
|
||||
ingredients_vec: boxcar::Vec::new(),
|
||||
ingredients_vec: Vec::new(),
|
||||
ingredients_requiring_reset: boxcar::Vec::new(),
|
||||
runtime: Runtime::default(),
|
||||
memo_ingredient_indices: Default::default(),
|
||||
event_callback,
|
||||
#[cfg(not(feature = "inventory"))]
|
||||
nonce: NONCE.nonce(),
|
||||
};
|
||||
|
||||
// Collect and initialize all registered ingredients.
|
||||
#[cfg(feature = "inventory")]
|
||||
let mut jars = inventory::iter::<ErasedJar>()
|
||||
.copied()
|
||||
.chain(jars)
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
#[cfg(not(feature = "inventory"))]
|
||||
let mut jars = jars;
|
||||
|
||||
// Ensure structs are initialized before tracked functions.
|
||||
jars.sort_by_key(|jar| jar.kind);
|
||||
|
||||
for jar in jars {
|
||||
zalsa.insert_jar(jar);
|
||||
}
|
||||
|
||||
zalsa
|
||||
}
|
||||
|
||||
pub(crate) fn nonce(&self) -> Nonce<StorageNonce> {
|
||||
#[cfg(not(feature = "inventory"))]
|
||||
pub(crate) fn nonce(&self) -> crate::nonce::Nonce<StorageNonce> {
|
||||
self.nonce
|
||||
}
|
||||
|
||||
|
@ -206,19 +231,19 @@ impl Zalsa {
|
|||
|
||||
/// Returns the [`Table`] used to store the value of salsa structs
|
||||
#[inline]
|
||||
pub(crate) fn table(&self) -> &Table {
|
||||
pub fn table(&self) -> &Table {
|
||||
self.runtime.table()
|
||||
}
|
||||
|
||||
/// Returns the [`MemoTable`][] for the salsa struct with the given id
|
||||
pub(crate) fn memo_table_for(&self, id: Id) -> MemoTableWithTypes<'_> {
|
||||
let table = self.table();
|
||||
// SAFETY: We are supplying the correct current revision
|
||||
unsafe { table.memos(id, self.current_revision()) }
|
||||
pub(crate) fn memo_table_for<T: SalsaStructInDb>(&self, id: Id) -> MemoTableWithTypes<'_> {
|
||||
// SAFETY: We are supplying the correct current revision.
|
||||
unsafe { T::memo_table(self, id, self.current_revision()) }
|
||||
}
|
||||
|
||||
/// Returns the ingredient at the given index, or panics if it is out-of-bounds.
|
||||
#[inline]
|
||||
pub(crate) fn lookup_ingredient(&self, index: IngredientIndex) -> &dyn Ingredient {
|
||||
pub fn lookup_ingredient(&self, index: IngredientIndex) -> &dyn Ingredient {
|
||||
let index = index.as_u32() as usize;
|
||||
self.ingredients_vec
|
||||
.get(index)
|
||||
|
@ -226,12 +251,25 @@ impl Zalsa {
|
|||
.as_ref()
|
||||
}
|
||||
|
||||
/// Returns the ingredient at the given index.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The index must be in-bounds.
|
||||
#[inline]
|
||||
pub unsafe fn lookup_ingredient_unchecked(&self, index: IngredientIndex) -> &dyn Ingredient {
|
||||
let index = index.as_u32() as usize;
|
||||
|
||||
// SAFETY: Guaranteed by caller.
|
||||
unsafe { self.ingredients_vec.get_unchecked(index).as_ref() }
|
||||
}
|
||||
|
||||
pub(crate) fn ingredient_index_for_memo(
|
||||
&self,
|
||||
struct_ingredient_index: IngredientIndex,
|
||||
memo_ingredient_index: MemoIngredientIndex,
|
||||
) -> IngredientIndex {
|
||||
self.memo_ingredient_indices.read()[struct_ingredient_index.as_u32() as usize]
|
||||
self.memo_ingredient_indices[struct_ingredient_index.as_u32() as usize]
|
||||
[memo_ingredient_index.as_usize()]
|
||||
}
|
||||
|
||||
|
@ -239,7 +277,7 @@ impl Zalsa {
|
|||
pub(crate) fn ingredients(&self) -> impl Iterator<Item = &dyn Ingredient> {
|
||||
self.ingredients_vec
|
||||
.iter()
|
||||
.map(|(_, ingredient)| ingredient.as_ref())
|
||||
.map(|ingredient| ingredient.as_ref())
|
||||
}
|
||||
|
||||
/// Starts unwinding the stack if the current revision is cancelled.
|
||||
|
@ -259,11 +297,11 @@ impl Zalsa {
|
|||
}
|
||||
|
||||
pub(crate) fn next_memo_ingredient_index(
|
||||
&self,
|
||||
&mut self,
|
||||
struct_ingredient_index: IngredientIndex,
|
||||
ingredient_index: IngredientIndex,
|
||||
) -> MemoIngredientIndex {
|
||||
let mut memo_ingredients = self.memo_ingredient_indices.write();
|
||||
let memo_ingredients = &mut self.memo_ingredient_indices;
|
||||
let idx = struct_ingredient_index.as_u32() as usize;
|
||||
let memo_ingredients = if let Some(memo_ingredients) = memo_ingredients.get_mut(idx) {
|
||||
memo_ingredients
|
||||
|
@ -291,7 +329,6 @@ impl Zalsa {
|
|||
let ingredient_index = self.ingredient_index(id);
|
||||
*self
|
||||
.ingredient_to_id_struct_type_id_map
|
||||
.read()
|
||||
.get(&ingredient_index)
|
||||
.expect("should have the ingredient index available")
|
||||
}
|
||||
|
@ -299,44 +336,36 @@ impl Zalsa {
|
|||
/// **NOT SEMVER STABLE**
|
||||
#[doc(hidden)]
|
||||
#[inline]
|
||||
pub fn lookup_jar_by_type<J: Jar>(&self) -> JarEntry<'_, J> {
|
||||
pub fn lookup_jar_by_type<J: Jar>(&self) -> IngredientIndex {
|
||||
let jar_type_id = TypeId::of::<J>();
|
||||
let guard = self.jar_map.guard();
|
||||
|
||||
match self.jar_map.get(&jar_type_id, &guard) {
|
||||
Some(index) => JarEntry::Occupied(index),
|
||||
None => JarEntry::Vacant {
|
||||
guard,
|
||||
zalsa: self,
|
||||
_jar: PhantomData,
|
||||
},
|
||||
}
|
||||
*self.jar_map.get(&jar_type_id).unwrap_or_else(|| {
|
||||
panic!(
|
||||
"ingredient `{}` was not registered",
|
||||
std::any::type_name::<J>()
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
#[cold]
|
||||
#[inline(never)]
|
||||
fn add_or_lookup_jar_by_type<J: Jar>(&self, guard: &papaya::LocalGuard<'_>) -> IngredientIndex {
|
||||
let jar_type_id = TypeId::of::<J>();
|
||||
let dependencies = J::create_dependencies(self);
|
||||
fn insert_jar(&mut self, jar: ErasedJar) {
|
||||
let jar_type_id = (jar.type_id)();
|
||||
|
||||
let jar_map_lock = self.jar_map_lock.lock();
|
||||
let index = IngredientIndex::new(self.ingredients_vec.len() as u32);
|
||||
|
||||
let index = IngredientIndex::from(self.ingredients_vec.count());
|
||||
if self.jar_map.contains_key(&jar_type_id) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Someone made it earlier than us.
|
||||
if let Some(index) = self.jar_map.get(&jar_type_id, guard) {
|
||||
return index;
|
||||
};
|
||||
|
||||
let ingredients = J::create_ingredients(self, index, dependencies);
|
||||
let ingredients = (jar.create_ingredients)(self, index);
|
||||
for ingredient in ingredients {
|
||||
let expected_index = ingredient.ingredient_index();
|
||||
|
||||
if ingredient.requires_reset_for_new_revision() {
|
||||
self.ingredients_requiring_reset.push(expected_index);
|
||||
}
|
||||
|
||||
let actual_index = self.ingredients_vec.push(ingredient);
|
||||
self.ingredients_vec.push(ingredient);
|
||||
|
||||
let actual_index = self.ingredients_vec.len() - 1;
|
||||
assert_eq!(
|
||||
expected_index.as_u32() as usize,
|
||||
actual_index,
|
||||
|
@ -347,17 +376,10 @@ impl Zalsa {
|
|||
);
|
||||
}
|
||||
|
||||
// Insert the index after all ingredients are inserted to avoid exposing
|
||||
// partially initialized jars to readers.
|
||||
self.jar_map.insert(jar_type_id, index, guard);
|
||||
|
||||
drop(jar_map_lock);
|
||||
self.jar_map.insert(jar_type_id, index);
|
||||
|
||||
self.ingredient_to_id_struct_type_id_map
|
||||
.write()
|
||||
.insert(index, J::id_struct_type_id());
|
||||
|
||||
index
|
||||
.insert(index, (jar.id_struct_type_id)());
|
||||
}
|
||||
|
||||
/// **NOT SEMVER STABLE**
|
||||
|
@ -393,7 +415,7 @@ impl Zalsa {
|
|||
#[doc(hidden)]
|
||||
pub fn new_revision(&mut self) -> Revision {
|
||||
let new_revision = self.runtime.new_revision();
|
||||
let _span = tracing::debug_span!("new_revision", ?new_revision).entered();
|
||||
let _span = crate::tracing::debug_span!("new_revision", ?new_revision).entered();
|
||||
|
||||
for (_, index) in self.ingredients_requiring_reset.iter() {
|
||||
let index = index.as_u32() as usize;
|
||||
|
@ -411,7 +433,7 @@ impl Zalsa {
|
|||
/// **NOT SEMVER STABLE**
|
||||
#[doc(hidden)]
|
||||
pub fn evict_lru(&mut self) {
|
||||
let _span = tracing::debug_span!("evict_lru").entered();
|
||||
let _span = crate::tracing::debug_span!("evict_lru").entered();
|
||||
for (_, index) in self.ingredients_requiring_reset.iter() {
|
||||
let index = index.as_u32() as usize;
|
||||
self.ingredients_vec
|
||||
|
@ -428,145 +450,83 @@ impl Zalsa {
|
|||
|
||||
#[inline(always)]
|
||||
pub fn event(&self, event: &dyn Fn() -> crate::Event) {
|
||||
if let Some(event_callback) = &self.event_callback {
|
||||
event_callback(event());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub enum JarEntry<'a, J> {
|
||||
Occupied(IngredientIndex),
|
||||
Vacant {
|
||||
zalsa: &'a Zalsa,
|
||||
guard: papaya::LocalGuard<'a>,
|
||||
_jar: PhantomData<J>,
|
||||
},
|
||||
}
|
||||
|
||||
impl<J> JarEntry<'_, J>
|
||||
where
|
||||
J: Jar,
|
||||
{
|
||||
#[inline]
|
||||
pub fn get(&self) -> Option<IngredientIndex> {
|
||||
match *self {
|
||||
JarEntry::Occupied(index) => Some(index),
|
||||
JarEntry::Vacant { .. } => None,
|
||||
if self.event_callback.is_some() {
|
||||
self.event_cold(event);
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_or_create(&self) -> IngredientIndex {
|
||||
match self {
|
||||
JarEntry::Occupied(index) => *index,
|
||||
JarEntry::Vacant { zalsa, guard, _jar } => zalsa.add_or_lookup_jar_by_type::<J>(guard),
|
||||
}
|
||||
// Avoid inlining, as events are typically only enabled for debugging purposes.
|
||||
#[cold]
|
||||
#[inline(never)]
|
||||
pub fn event_cold(&self, event: &dyn Fn() -> crate::Event) {
|
||||
let event_callback = self.event_callback.as_ref().unwrap();
|
||||
event_callback(event());
|
||||
}
|
||||
}
|
||||
|
||||
/// Caches a pointer to an ingredient in a database.
|
||||
/// Optimized for the case of a single database.
|
||||
pub struct IngredientCache<I>
|
||||
where
|
||||
I: Ingredient,
|
||||
{
|
||||
// A packed representation of `Option<(Nonce<StorageNonce>, IngredientIndex)>`.
|
||||
//
|
||||
// This allows us to replace a lock in favor of an atomic load. This works thanks to `Nonce`
|
||||
// having a niche, which means the entire type can fit into an `AtomicU64`.
|
||||
cached_data: AtomicU64,
|
||||
phantom: PhantomData<fn() -> I>,
|
||||
/// A type-erased `Jar`, used for ingredient registration.
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct ErasedJar {
|
||||
kind: JarKind,
|
||||
type_id: fn() -> TypeId,
|
||||
id_struct_type_id: fn() -> TypeId,
|
||||
create_ingredients: fn(&mut Zalsa, IngredientIndex) -> Vec<Box<dyn Ingredient>>,
|
||||
}
|
||||
|
||||
impl<I> Default for IngredientCache<I>
|
||||
where
|
||||
I: Ingredient,
|
||||
{
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
/// The kind of an `Jar`.
|
||||
///
|
||||
/// Note that the ordering of the variants is important. Struct ingredients must be
|
||||
/// initialized before tracked functions, as tracked function ingredients depend on
|
||||
/// their input struct.
|
||||
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug)]
|
||||
pub enum JarKind {
|
||||
/// An input/tracked/interned struct.
|
||||
Struct,
|
||||
|
||||
/// A tracked function.
|
||||
TrackedFn,
|
||||
}
|
||||
|
||||
impl<I> IngredientCache<I>
|
||||
where
|
||||
I: Ingredient,
|
||||
{
|
||||
const UNINITIALIZED: u64 = 0;
|
||||
|
||||
/// Create a new cache
|
||||
pub const fn new() -> Self {
|
||||
impl ErasedJar {
|
||||
/// Performs type-erasure of a given ingredient.
|
||||
pub const fn erase<I: HasJar>() -> Self {
|
||||
Self {
|
||||
cached_data: AtomicU64::new(Self::UNINITIALIZED),
|
||||
phantom: PhantomData,
|
||||
kind: I::KIND,
|
||||
type_id: TypeId::of::<I::Jar>,
|
||||
create_ingredients: <I::Jar>::create_ingredients,
|
||||
id_struct_type_id: <I::Jar>::id_struct_type_id,
|
||||
}
|
||||
}
|
||||
|
||||
/// Get a reference to the ingredient in the database.
|
||||
/// If the ingredient is not already in the cache, it will be created.
|
||||
#[inline(always)]
|
||||
pub fn get_or_create<'db>(
|
||||
&self,
|
||||
zalsa: &'db Zalsa,
|
||||
create_index: impl Fn() -> IngredientIndex,
|
||||
) -> &'db I {
|
||||
let index = self.get_or_create_index(zalsa, create_index);
|
||||
zalsa.lookup_ingredient(index).assert_type::<I>()
|
||||
}
|
||||
|
||||
/// Get a reference to the ingredient in the database.
|
||||
/// If the ingredient is not already in the cache, it will be created.
|
||||
#[inline(always)]
|
||||
pub fn get_or_create_index(
|
||||
&self,
|
||||
zalsa: &Zalsa,
|
||||
create_index: impl Fn() -> IngredientIndex,
|
||||
) -> IngredientIndex {
|
||||
const _: () = assert!(
|
||||
mem::size_of::<(Nonce<StorageNonce>, IngredientIndex)>() == mem::size_of::<u64>()
|
||||
);
|
||||
let cached_data = self.cached_data.load(Ordering::Acquire);
|
||||
if cached_data == Self::UNINITIALIZED {
|
||||
#[cold]
|
||||
#[inline(never)]
|
||||
fn get_or_create_index_slow<I: Ingredient>(
|
||||
this: &IngredientCache<I>,
|
||||
zalsa: &Zalsa,
|
||||
create_index: impl Fn() -> IngredientIndex,
|
||||
) -> IngredientIndex {
|
||||
let index = create_index();
|
||||
let nonce = zalsa.nonce().into_u32().get() as u64;
|
||||
let packed = (nonce << u32::BITS) | (index.as_u32() as u64);
|
||||
debug_assert_ne!(packed, IngredientCache::<I>::UNINITIALIZED);
|
||||
|
||||
// Discard the result, whether we won over the cache or not does not matter
|
||||
// we know that something has been cached now
|
||||
_ = this.cached_data.compare_exchange(
|
||||
IngredientCache::<I>::UNINITIALIZED,
|
||||
packed,
|
||||
Ordering::Release,
|
||||
Ordering::Acquire,
|
||||
);
|
||||
// and we already have our index computed so we can just use that
|
||||
index
|
||||
}
|
||||
|
||||
return get_or_create_index_slow(self, zalsa, create_index);
|
||||
};
|
||||
|
||||
// unpack our u64
|
||||
// SAFETY: We've checked against `UNINITIALIZED` (0) above and so the upper bits must be non-zero
|
||||
let nonce = Nonce::<StorageNonce>::from_u32(unsafe {
|
||||
NonZeroU32::new_unchecked((cached_data >> u32::BITS) as u32)
|
||||
});
|
||||
let mut index = IngredientIndex(cached_data as u32);
|
||||
|
||||
if zalsa.nonce() != nonce {
|
||||
index = create_index();
|
||||
}
|
||||
index
|
||||
}
|
||||
}
|
||||
|
||||
/// A salsa ingredient that can be registered in the database.
|
||||
///
|
||||
/// This trait is implemented for tracked functions and salsa structs.
|
||||
pub trait HasJar {
|
||||
/// The [`Jar`] associated with this ingredient.
|
||||
type Jar: Jar;
|
||||
|
||||
/// The [`JarKind`] for `Self::Jar`.
|
||||
const KIND: JarKind;
|
||||
}
|
||||
|
||||
// Collect jars statically at compile-time if supported.
|
||||
#[cfg(feature = "inventory")]
|
||||
inventory::collect!(ErasedJar);
|
||||
|
||||
#[cfg(feature = "inventory")]
|
||||
pub use inventory::submit as register_jar;
|
||||
|
||||
#[cfg(not(feature = "inventory"))]
|
||||
#[macro_export]
|
||||
#[doc(hidden)]
|
||||
macro_rules! register_jar {
|
||||
($($_:tt)*) => {};
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "inventory"))]
|
||||
pub use crate::register_jar;
|
||||
|
||||
/// Given a wide pointer `T`, extracts the data pointer (typed as `U`).
|
||||
///
|
||||
/// # Safety
|
||||
|
|
|
@ -1,12 +1,15 @@
|
|||
use std::cell::RefCell;
|
||||
use std::cell::{RefCell, UnsafeCell};
|
||||
use std::panic::UnwindSafe;
|
||||
use std::ptr::{self, NonNull};
|
||||
|
||||
use rustc_hash::FxHashMap;
|
||||
use thin_vec::ThinVec;
|
||||
use tracing::debug;
|
||||
|
||||
use crate::accumulator::accumulated_map::{AccumulatedMap, AtomicInputAccumulatedValues};
|
||||
#[cfg(feature = "accumulator")]
|
||||
use crate::accumulator::{
|
||||
accumulated_map::{AccumulatedMap, AtomicInputAccumulatedValues},
|
||||
Accumulator,
|
||||
};
|
||||
use crate::active_query::QueryStack;
|
||||
use crate::cycle::{empty_cycle_heads, CycleHeads, IterationCount};
|
||||
use crate::durability::Durability;
|
||||
|
@ -16,7 +19,7 @@ use crate::sync::atomic::AtomicBool;
|
|||
use crate::table::{PageIndex, Slot, Table};
|
||||
use crate::tracked_struct::{Disambiguator, Identity, IdentityHash, IdentityMap};
|
||||
use crate::zalsa::{IngredientIndex, Zalsa};
|
||||
use crate::{Accumulator, Cancelled, Id, Revision};
|
||||
use crate::{Cancelled, Id, Revision};
|
||||
|
||||
/// State that is specific to a single execution thread.
|
||||
///
|
||||
|
@ -33,14 +36,14 @@ pub struct ZalsaLocal {
|
|||
|
||||
/// Stores the most recent page for a given ingredient.
|
||||
/// This is thread-local to avoid contention.
|
||||
most_recent_pages: RefCell<FxHashMap<IngredientIndex, PageIndex>>,
|
||||
most_recent_pages: UnsafeCell<FxHashMap<IngredientIndex, PageIndex>>,
|
||||
}
|
||||
|
||||
impl ZalsaLocal {
|
||||
pub(crate) fn new() -> Self {
|
||||
ZalsaLocal {
|
||||
query_stack: RefCell::new(QueryStack::default()),
|
||||
most_recent_pages: RefCell::new(FxHashMap::default()),
|
||||
most_recent_pages: UnsafeCell::new(FxHashMap::default()),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -54,35 +57,60 @@ impl ZalsaLocal {
|
|||
/// Allocate a new id in `table` for the given ingredient
|
||||
/// storing `value`. Remembers the most recent page from this
|
||||
/// thread and attempts to reuse it.
|
||||
pub(crate) fn allocate<T: Slot>(
|
||||
pub(crate) fn allocate<'db, T: Slot>(
|
||||
&self,
|
||||
zalsa: &Zalsa,
|
||||
zalsa: &'db Zalsa,
|
||||
ingredient: IngredientIndex,
|
||||
mut value: impl FnOnce(Id) -> T,
|
||||
) -> Id {
|
||||
) -> (Id, &'db T) {
|
||||
// SAFETY: `ZalsaLocal` is `!Sync`, and we never expose a reference to this field,
|
||||
// so we have exclusive access.
|
||||
let most_recent_pages = unsafe { &mut *self.most_recent_pages.get() };
|
||||
|
||||
// Fast-path, we already have an unfilled page available.
|
||||
if let Some(&page) = most_recent_pages.get(&ingredient) {
|
||||
let page_ref = zalsa.table().page::<T>(page);
|
||||
match page_ref.allocate(page, value) {
|
||||
Ok((id, value)) => return (id, value),
|
||||
Err(v) => value = v,
|
||||
}
|
||||
}
|
||||
|
||||
self.allocate_cold(zalsa, ingredient, value)
|
||||
}
|
||||
|
||||
#[cold]
|
||||
#[inline(never)]
|
||||
pub(crate) fn allocate_cold<'db, T: Slot>(
|
||||
&self,
|
||||
zalsa: &'db Zalsa,
|
||||
ingredient: IngredientIndex,
|
||||
mut value: impl FnOnce(Id) -> T,
|
||||
) -> (Id, &'db T) {
|
||||
let memo_types = || {
|
||||
zalsa
|
||||
.lookup_ingredient(ingredient)
|
||||
.memo_table_types()
|
||||
.clone()
|
||||
};
|
||||
|
||||
// SAFETY: `ZalsaLocal` is `!Sync`, and we never expose a reference to this field,
|
||||
// so we have exclusive access.
|
||||
let most_recent_pages = unsafe { &mut *self.most_recent_pages.get() };
|
||||
|
||||
// Find the most recent page, pushing a page if needed
|
||||
let mut page = *self
|
||||
.most_recent_pages
|
||||
.borrow_mut()
|
||||
.entry(ingredient)
|
||||
.or_insert_with(|| {
|
||||
zalsa
|
||||
.table()
|
||||
.fetch_or_push_page::<T>(ingredient, memo_types)
|
||||
});
|
||||
let mut page = *most_recent_pages.entry(ingredient).or_insert_with(|| {
|
||||
zalsa
|
||||
.table()
|
||||
.fetch_or_push_page::<T>(ingredient, memo_types)
|
||||
});
|
||||
|
||||
loop {
|
||||
// Try to allocate an entry on that page
|
||||
let page_ref = zalsa.table().page::<T>(page);
|
||||
match page_ref.allocate(page, value) {
|
||||
// If successful, return
|
||||
Ok(id) => return id,
|
||||
Ok((id, value)) => return (id, value),
|
||||
|
||||
// Otherwise, create a new page and try again
|
||||
// Note that we could try fetching a page again, but as we just filled one up
|
||||
|
@ -90,7 +118,7 @@ impl ZalsaLocal {
|
|||
Err(v) => {
|
||||
value = v;
|
||||
page = zalsa.table().push_page::<T>(ingredient, memo_types());
|
||||
self.most_recent_pages.borrow_mut().insert(ingredient, page);
|
||||
most_recent_pages.insert(ingredient, page);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -102,88 +130,124 @@ impl ZalsaLocal {
|
|||
database_key_index: DatabaseKeyIndex,
|
||||
iteration_count: IterationCount,
|
||||
) -> ActiveQueryGuard<'_> {
|
||||
let mut query_stack = self.query_stack.borrow_mut();
|
||||
query_stack.push_new_query(database_key_index, iteration_count);
|
||||
ActiveQueryGuard {
|
||||
local_state: self,
|
||||
database_key_index,
|
||||
#[cfg(debug_assertions)]
|
||||
push_len: query_stack.len(),
|
||||
// SAFETY: We do not access the query stack reentrantly.
|
||||
unsafe {
|
||||
self.with_query_stack_unchecked_mut(|stack| {
|
||||
stack.push_new_query(database_key_index, iteration_count);
|
||||
|
||||
ActiveQueryGuard {
|
||||
local_state: self,
|
||||
database_key_index,
|
||||
#[cfg(debug_assertions)]
|
||||
push_len: stack.len(),
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Executes a closure within the context of the current active query stacks (mutable).
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The closure cannot access the query stack reentrantly, whether mutable or immutable.
|
||||
#[inline(always)]
|
||||
pub(crate) fn with_query_stack_mut<R>(
|
||||
pub(crate) unsafe fn with_query_stack_unchecked_mut<R>(
|
||||
&self,
|
||||
c: impl UnwindSafe + FnOnce(&mut QueryStack) -> R,
|
||||
f: impl UnwindSafe + FnOnce(&mut QueryStack) -> R,
|
||||
) -> R {
|
||||
c(&mut self.query_stack.borrow_mut())
|
||||
// SAFETY: The caller guarantees that the query stack will not be accessed reentrantly.
|
||||
// Additionally, `ZalsaLocal` is `!Sync`, and we never expose a reference to the query
|
||||
// stack except through this method, so we have exclusive access.
|
||||
unsafe { f(&mut self.query_stack.try_borrow_mut().unwrap_unchecked()) }
|
||||
}
|
||||
|
||||
/// Executes a closure within the context of the current active query stacks.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// No mutable references to the query stack can exist while the closure is executed.
|
||||
#[inline(always)]
|
||||
pub(crate) fn with_query_stack<R>(&self, c: impl UnwindSafe + FnOnce(&QueryStack) -> R) -> R {
|
||||
c(&mut self.query_stack.borrow())
|
||||
pub(crate) unsafe fn with_query_stack_unchecked<R>(
|
||||
&self,
|
||||
f: impl UnwindSafe + FnOnce(&QueryStack) -> R,
|
||||
) -> R {
|
||||
// SAFETY: The caller guarantees that the query stack will not being accessed mutably.
|
||||
// Additionally, `ZalsaLocal` is `!Sync`, and we never expose a reference to the query
|
||||
// stack except through this method, so we have exclusive access.
|
||||
unsafe { f(&self.query_stack.try_borrow().unwrap_unchecked()) }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub(crate) fn try_with_query_stack<R>(
|
||||
&self,
|
||||
c: impl UnwindSafe + FnOnce(&QueryStack) -> R,
|
||||
f: impl UnwindSafe + FnOnce(&QueryStack) -> R,
|
||||
) -> Option<R> {
|
||||
self.query_stack
|
||||
.try_borrow()
|
||||
.ok()
|
||||
.as_ref()
|
||||
.map(|stack| c(stack))
|
||||
.map(|stack| f(stack))
|
||||
}
|
||||
|
||||
/// Returns the index of the active query along with its *current* durability/changed-at
|
||||
/// information. As the query continues to execute, naturally, that information may change.
|
||||
pub(crate) fn active_query(&self) -> Option<(DatabaseKeyIndex, Stamp)> {
|
||||
self.with_query_stack(|stack| {
|
||||
stack
|
||||
.last()
|
||||
.map(|active_query| (active_query.database_key_index, active_query.stamp()))
|
||||
})
|
||||
// SAFETY: We do not access the query stack reentrantly.
|
||||
unsafe {
|
||||
self.with_query_stack_unchecked(|stack| {
|
||||
stack
|
||||
.last()
|
||||
.map(|active_query| (active_query.database_key_index, active_query.stamp()))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Add an output to the current query's list of dependencies
|
||||
///
|
||||
/// Returns `Err` if not in a query.
|
||||
#[cfg(feature = "accumulator")]
|
||||
pub(crate) fn accumulate<A: Accumulator>(
|
||||
&self,
|
||||
index: IngredientIndex,
|
||||
value: A,
|
||||
) -> Result<(), ()> {
|
||||
self.with_query_stack_mut(|stack| {
|
||||
if let Some(top_query) = stack.last_mut() {
|
||||
top_query.accumulate(index, value);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
})
|
||||
// SAFETY: We do not access the query stack reentrantly.
|
||||
unsafe {
|
||||
self.with_query_stack_unchecked_mut(|stack| {
|
||||
if let Some(top_query) = stack.last_mut() {
|
||||
top_query.accumulate(index, value);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Add an output to the current query's list of dependencies
|
||||
pub(crate) fn add_output(&self, entity: DatabaseKeyIndex) {
|
||||
self.with_query_stack_mut(|stack| {
|
||||
if let Some(top_query) = stack.last_mut() {
|
||||
top_query.add_output(entity)
|
||||
}
|
||||
})
|
||||
// SAFETY: We do not access the query stack reentrantly.
|
||||
unsafe {
|
||||
self.with_query_stack_unchecked_mut(|stack| {
|
||||
if let Some(top_query) = stack.last_mut() {
|
||||
top_query.add_output(entity)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Check whether `entity` is an output of the currently active query (if any)
|
||||
pub(crate) fn is_output_of_active_query(&self, entity: DatabaseKeyIndex) -> bool {
|
||||
self.with_query_stack_mut(|stack| {
|
||||
if let Some(top_query) = stack.last_mut() {
|
||||
top_query.is_output(entity)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
})
|
||||
// SAFETY: We do not access the query stack reentrantly.
|
||||
unsafe {
|
||||
self.with_query_stack_unchecked_mut(|stack| {
|
||||
if let Some(top_query) = stack.last_mut() {
|
||||
top_query.is_output(entity)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Register that currently active query reads the given input
|
||||
|
@ -193,26 +257,34 @@ impl ZalsaLocal {
|
|||
input: DatabaseKeyIndex,
|
||||
durability: Durability,
|
||||
changed_at: Revision,
|
||||
has_accumulated: bool,
|
||||
accumulated_inputs: &AtomicInputAccumulatedValues,
|
||||
cycle_heads: &CycleHeads,
|
||||
#[cfg(feature = "accumulator")] has_accumulated: bool,
|
||||
#[cfg(feature = "accumulator")] accumulated_inputs: &AtomicInputAccumulatedValues,
|
||||
) {
|
||||
debug!(
|
||||
crate::tracing::debug!(
|
||||
"report_tracked_read(input={:?}, durability={:?}, changed_at={:?})",
|
||||
input, durability, changed_at
|
||||
input,
|
||||
durability,
|
||||
changed_at
|
||||
);
|
||||
self.with_query_stack_mut(|stack| {
|
||||
if let Some(top_query) = stack.last_mut() {
|
||||
top_query.add_read(
|
||||
input,
|
||||
durability,
|
||||
changed_at,
|
||||
has_accumulated,
|
||||
accumulated_inputs,
|
||||
cycle_heads,
|
||||
);
|
||||
}
|
||||
})
|
||||
|
||||
// SAFETY: We do not access the query stack reentrantly.
|
||||
unsafe {
|
||||
self.with_query_stack_unchecked_mut(|stack| {
|
||||
if let Some(top_query) = stack.last_mut() {
|
||||
top_query.add_read(
|
||||
input,
|
||||
durability,
|
||||
changed_at,
|
||||
cycle_heads,
|
||||
#[cfg(feature = "accumulator")]
|
||||
has_accumulated,
|
||||
#[cfg(feature = "accumulator")]
|
||||
accumulated_inputs,
|
||||
);
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Register that currently active query reads the given input
|
||||
|
@ -223,15 +295,21 @@ impl ZalsaLocal {
|
|||
durability: Durability,
|
||||
changed_at: Revision,
|
||||
) {
|
||||
debug!(
|
||||
crate::tracing::debug!(
|
||||
"report_tracked_read(input={:?}, durability={:?}, changed_at={:?})",
|
||||
input, durability, changed_at
|
||||
input,
|
||||
durability,
|
||||
changed_at
|
||||
);
|
||||
self.with_query_stack_mut(|stack| {
|
||||
if let Some(top_query) = stack.last_mut() {
|
||||
top_query.add_read_simple(input, durability, changed_at);
|
||||
}
|
||||
})
|
||||
|
||||
// SAFETY: We do not access the query stack reentrantly.
|
||||
unsafe {
|
||||
self.with_query_stack_unchecked_mut(|stack| {
|
||||
if let Some(top_query) = stack.last_mut() {
|
||||
top_query.add_read_simple(input, durability, changed_at);
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Register that the current query read an untracked value
|
||||
|
@ -241,11 +319,14 @@ impl ZalsaLocal {
|
|||
/// * `current_revision`, the current revision
|
||||
#[inline(always)]
|
||||
pub(crate) fn report_untracked_read(&self, current_revision: Revision) {
|
||||
self.with_query_stack_mut(|stack| {
|
||||
if let Some(top_query) = stack.last_mut() {
|
||||
top_query.add_untracked_read(current_revision);
|
||||
}
|
||||
})
|
||||
// SAFETY: We do not access the query stack reentrantly.
|
||||
unsafe {
|
||||
self.with_query_stack_unchecked_mut(|stack| {
|
||||
if let Some(top_query) = stack.last_mut() {
|
||||
top_query.add_untracked_read(current_revision);
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Update the top query on the stack to act as though it read a value
|
||||
|
@ -253,11 +334,14 @@ impl ZalsaLocal {
|
|||
// FIXME: Use or remove this.
|
||||
#[allow(dead_code)]
|
||||
pub(crate) fn report_synthetic_read(&self, durability: Durability, revision: Revision) {
|
||||
self.with_query_stack_mut(|stack| {
|
||||
if let Some(top_query) = stack.last_mut() {
|
||||
top_query.add_synthetic_read(durability, revision);
|
||||
}
|
||||
})
|
||||
// SAFETY: We do not access the query stack reentrantly.
|
||||
unsafe {
|
||||
self.with_query_stack_unchecked_mut(|stack| {
|
||||
if let Some(top_query) = stack.last_mut() {
|
||||
top_query.add_synthetic_read(durability, revision);
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Called when the active queries creates an index from the
|
||||
|
@ -272,33 +356,42 @@ impl ZalsaLocal {
|
|||
/// * the disambiguator index
|
||||
#[track_caller]
|
||||
pub(crate) fn disambiguate(&self, key: IdentityHash) -> (Stamp, Disambiguator) {
|
||||
self.with_query_stack_mut(|stack| {
|
||||
let top_query = stack.last_mut().expect(
|
||||
"cannot create a tracked struct disambiguator outside of a tracked function",
|
||||
);
|
||||
let disambiguator = top_query.disambiguate(key);
|
||||
(top_query.stamp(), disambiguator)
|
||||
})
|
||||
// SAFETY: We do not access the query stack reentrantly.
|
||||
unsafe {
|
||||
self.with_query_stack_unchecked_mut(|stack| {
|
||||
let top_query = stack.last_mut().expect(
|
||||
"cannot create a tracked struct disambiguator outside of a tracked function",
|
||||
);
|
||||
let disambiguator = top_query.disambiguate(key);
|
||||
(top_query.stamp(), disambiguator)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[track_caller]
|
||||
pub(crate) fn tracked_struct_id(&self, identity: &Identity) -> Option<Id> {
|
||||
self.with_query_stack(|stack| {
|
||||
let top_query = stack
|
||||
.last()
|
||||
.expect("cannot create a tracked struct ID outside of a tracked function");
|
||||
top_query.tracked_struct_ids().get(identity)
|
||||
})
|
||||
// SAFETY: We do not access the query stack reentrantly.
|
||||
unsafe {
|
||||
self.with_query_stack_unchecked(|stack| {
|
||||
let top_query = stack
|
||||
.last()
|
||||
.expect("cannot create a tracked struct ID outside of a tracked function");
|
||||
top_query.tracked_struct_ids().get(identity)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[track_caller]
|
||||
pub(crate) fn store_tracked_struct_id(&self, identity: Identity, id: Id) {
|
||||
self.with_query_stack_mut(|stack| {
|
||||
let top_query = stack
|
||||
.last_mut()
|
||||
.expect("cannot store a tracked struct ID outside of a tracked function");
|
||||
top_query.tracked_struct_ids_mut().insert(identity, id);
|
||||
})
|
||||
// SAFETY: We do not access the query stack reentrantly.
|
||||
unsafe {
|
||||
self.with_query_stack_unchecked_mut(|stack| {
|
||||
let top_query = stack
|
||||
.last_mut()
|
||||
.expect("cannot store a tracked struct ID outside of a tracked function");
|
||||
top_query.tracked_struct_ids_mut().insert(identity, id);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cold]
|
||||
|
@ -334,6 +427,7 @@ pub(crate) struct QueryRevisions {
|
|||
///
|
||||
/// Note that this field could be in `QueryRevisionsExtra` as it is only relevant
|
||||
/// for accumulators, but we get it for free anyways due to padding.
|
||||
#[cfg(feature = "accumulator")]
|
||||
pub(super) accumulated_inputs: AtomicInputAccumulatedValues,
|
||||
|
||||
/// Are the `cycle_heads` verified to not be provisional anymore?
|
||||
|
@ -353,10 +447,11 @@ impl QueryRevisions {
|
|||
let QueryRevisions {
|
||||
changed_at: _,
|
||||
durability: _,
|
||||
accumulated_inputs: _,
|
||||
verified_final: _,
|
||||
origin,
|
||||
extra,
|
||||
#[cfg(feature = "accumulator")]
|
||||
accumulated_inputs: _,
|
||||
} = self;
|
||||
|
||||
let mut memory = 0;
|
||||
|
@ -386,19 +481,24 @@ pub(crate) struct QueryRevisionsExtra(Option<Box<QueryRevisionsExtraInner>>);
|
|||
|
||||
impl QueryRevisionsExtra {
|
||||
pub fn new(
|
||||
accumulated: AccumulatedMap,
|
||||
#[cfg(feature = "accumulator")] accumulated: AccumulatedMap,
|
||||
tracked_struct_ids: IdentityMap,
|
||||
cycle_heads: CycleHeads,
|
||||
iteration: IterationCount,
|
||||
) -> Self {
|
||||
let inner = if tracked_struct_ids.is_empty()
|
||||
#[cfg(feature = "accumulator")]
|
||||
let acc = accumulated.is_empty();
|
||||
#[cfg(not(feature = "accumulator"))]
|
||||
let acc = true;
|
||||
let inner = if acc
|
||||
&& tracked_struct_ids.is_empty()
|
||||
&& cycle_heads.is_empty()
|
||||
&& accumulated.is_empty()
|
||||
&& iteration.is_initial()
|
||||
{
|
||||
None
|
||||
} else {
|
||||
Some(Box::new(QueryRevisionsExtraInner {
|
||||
#[cfg(feature = "accumulator")]
|
||||
accumulated,
|
||||
cycle_heads,
|
||||
tracked_struct_ids: tracked_struct_ids.into_thin_vec(),
|
||||
|
@ -412,6 +512,7 @@ impl QueryRevisionsExtra {
|
|||
|
||||
#[derive(Debug)]
|
||||
struct QueryRevisionsExtraInner {
|
||||
#[cfg(feature = "accumulator")]
|
||||
accumulated: AccumulatedMap,
|
||||
|
||||
/// The ids of tracked structs created by this query.
|
||||
|
@ -450,15 +551,18 @@ impl QueryRevisionsExtraInner {
|
|||
#[cfg(feature = "salsa_unstable")]
|
||||
fn allocation_size(&self) -> usize {
|
||||
let QueryRevisionsExtraInner {
|
||||
#[cfg(feature = "accumulator")]
|
||||
accumulated,
|
||||
tracked_struct_ids,
|
||||
cycle_heads,
|
||||
iteration: _,
|
||||
} = self;
|
||||
|
||||
accumulated.allocation_size()
|
||||
+ cycle_heads.allocation_size()
|
||||
+ std::mem::size_of_val(tracked_struct_ids.as_slice())
|
||||
#[cfg(feature = "accumulator")]
|
||||
let b = accumulated.allocation_size();
|
||||
#[cfg(not(feature = "accumulator"))]
|
||||
let b = 0;
|
||||
b + cycle_heads.allocation_size() + std::mem::size_of_val(tracked_struct_ids.as_slice())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -469,7 +573,7 @@ const _: [(); std::mem::size_of::<QueryRevisions>()] = [(); std::mem::size_of::<
|
|||
#[cfg(not(feature = "shuttle"))]
|
||||
#[cfg(target_pointer_width = "64")]
|
||||
const _: [(); std::mem::size_of::<QueryRevisionsExtraInner>()] =
|
||||
[(); std::mem::size_of::<[usize; 7]>()];
|
||||
[(); std::mem::size_of::<[usize; if cfg!(feature = "accumulator") { 7 } else { 3 }]>()];
|
||||
|
||||
impl QueryRevisions {
|
||||
pub(crate) fn fixpoint_initial(query: DatabaseKeyIndex) -> Self {
|
||||
|
@ -477,9 +581,11 @@ impl QueryRevisions {
|
|||
changed_at: Revision::start(),
|
||||
durability: Durability::MAX,
|
||||
origin: QueryOrigin::fixpoint_initial(),
|
||||
#[cfg(feature = "accumulator")]
|
||||
accumulated_inputs: Default::default(),
|
||||
verified_final: AtomicBool::new(false),
|
||||
extra: QueryRevisionsExtra::new(
|
||||
#[cfg(feature = "accumulator")]
|
||||
AccumulatedMap::default(),
|
||||
IdentityMap::default(),
|
||||
CycleHeads::initial(query),
|
||||
|
@ -489,6 +595,7 @@ impl QueryRevisions {
|
|||
}
|
||||
|
||||
/// Returns a reference to the `AccumulatedMap` for this query, or `None` if the map is empty.
|
||||
#[cfg(feature = "accumulator")]
|
||||
pub(crate) fn accumulated(&self) -> Option<&AccumulatedMap> {
|
||||
self.extra
|
||||
.0
|
||||
|
@ -520,6 +627,7 @@ impl QueryRevisions {
|
|||
Some(extra) => extra.cycle_heads = cycle_heads,
|
||||
None => {
|
||||
self.extra = QueryRevisionsExtra::new(
|
||||
#[cfg(feature = "accumulator")]
|
||||
AccumulatedMap::default(),
|
||||
IdentityMap::default(),
|
||||
cycle_heads,
|
||||
|
@ -590,6 +698,7 @@ pub enum QueryOriginRef<'a> {
|
|||
impl<'a> QueryOriginRef<'a> {
|
||||
/// Indices for queries *read* by this query
|
||||
#[inline]
|
||||
#[cfg(feature = "accumulator")]
|
||||
pub(crate) fn inputs(self) -> impl DoubleEndedIterator<Item = DatabaseKeyIndex> + use<'a> {
|
||||
let opt_edges = match self {
|
||||
QueryOriginRef::Derived(edges) | QueryOriginRef::DerivedUntracked(edges) => Some(edges),
|
||||
|
@ -754,7 +863,11 @@ impl QueryOrigin {
|
|||
QueryOriginKind::Assigned => {
|
||||
// SAFETY: `data.index` is initialized when the tag is `QueryOriginKind::Assigned`.
|
||||
let index = unsafe { self.data.index };
|
||||
let ingredient_index = IngredientIndex::from(self.metadata as usize);
|
||||
|
||||
// SAFETY: `metadata` is initialized from a valid `IngredientIndex` when the tag
|
||||
// is `QueryOriginKind::Assigned`.
|
||||
let ingredient_index = unsafe { IngredientIndex::new_unchecked(self.metadata) };
|
||||
|
||||
QueryOriginRef::Assigned(DatabaseKeyIndex::new(ingredient_index, index))
|
||||
}
|
||||
|
||||
|
@ -878,6 +991,7 @@ pub enum QueryEdgeKind {
|
|||
/// Returns the (tracked) inputs that were executed in computing this memoized value.
|
||||
///
|
||||
/// These will always be in execution order.
|
||||
#[cfg(feature = "accumulator")]
|
||||
pub(crate) fn input_edges(
|
||||
input_outputs: &[QueryEdge],
|
||||
) -> impl DoubleEndedIterator<Item = DatabaseKeyIndex> + use<'_> {
|
||||
|
@ -913,15 +1027,18 @@ pub(crate) struct ActiveQueryGuard<'me> {
|
|||
impl ActiveQueryGuard<'_> {
|
||||
/// Initialize the tracked struct ids with the values from the prior execution.
|
||||
pub(crate) fn seed_tracked_struct_ids(&self, tracked_struct_ids: &[(Identity, Id)]) {
|
||||
self.local_state.with_query_stack_mut(|stack| {
|
||||
#[cfg(debug_assertions)]
|
||||
assert_eq!(stack.len(), self.push_len);
|
||||
let frame = stack.last_mut().unwrap();
|
||||
assert!(frame.tracked_struct_ids().is_empty());
|
||||
frame
|
||||
.tracked_struct_ids_mut()
|
||||
.clone_from_slice(tracked_struct_ids);
|
||||
})
|
||||
// SAFETY: We do not access the query stack reentrantly.
|
||||
unsafe {
|
||||
self.local_state.with_query_stack_unchecked_mut(|stack| {
|
||||
#[cfg(debug_assertions)]
|
||||
assert_eq!(stack.len(), self.push_len);
|
||||
let frame = stack.last_mut().unwrap();
|
||||
assert!(frame.tracked_struct_ids().is_empty());
|
||||
frame
|
||||
.tracked_struct_ids_mut()
|
||||
.clone_from_slice(tracked_struct_ids);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Append the given `outputs` to the query's output list.
|
||||
|
@ -934,23 +1051,29 @@ impl ActiveQueryGuard<'_> {
|
|||
QueryOriginRef::DerivedUntracked(_)
|
||||
);
|
||||
|
||||
self.local_state.with_query_stack_mut(|stack| {
|
||||
#[cfg(debug_assertions)]
|
||||
assert_eq!(stack.len(), self.push_len);
|
||||
let frame = stack.last_mut().unwrap();
|
||||
frame.seed_iteration(durability, changed_at, edges, untracked_read);
|
||||
})
|
||||
// SAFETY: We do not access the query stack reentrantly.
|
||||
unsafe {
|
||||
self.local_state.with_query_stack_unchecked_mut(|stack| {
|
||||
#[cfg(debug_assertions)]
|
||||
assert_eq!(stack.len(), self.push_len);
|
||||
let frame = stack.last_mut().unwrap();
|
||||
frame.seed_iteration(durability, changed_at, edges, untracked_read);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Invoked when the query has successfully completed execution.
|
||||
fn complete(self) -> QueryRevisions {
|
||||
let query = self.local_state.with_query_stack_mut(|stack| {
|
||||
stack.pop_into_revisions(
|
||||
self.database_key_index,
|
||||
#[cfg(debug_assertions)]
|
||||
self.push_len,
|
||||
)
|
||||
});
|
||||
// SAFETY: We do not access the query stack reentrantly.
|
||||
let query = unsafe {
|
||||
self.local_state.with_query_stack_unchecked_mut(|stack| {
|
||||
stack.pop_into_revisions(
|
||||
self.database_key_index,
|
||||
#[cfg(debug_assertions)]
|
||||
self.push_len,
|
||||
)
|
||||
})
|
||||
};
|
||||
std::mem::forget(self);
|
||||
query
|
||||
}
|
||||
|
@ -966,12 +1089,15 @@ impl ActiveQueryGuard<'_> {
|
|||
|
||||
impl Drop for ActiveQueryGuard<'_> {
|
||||
fn drop(&mut self) {
|
||||
self.local_state.with_query_stack_mut(|stack| {
|
||||
stack.pop(
|
||||
self.database_key_index,
|
||||
#[cfg(debug_assertions)]
|
||||
self.push_len,
|
||||
);
|
||||
});
|
||||
// SAFETY: We do not access the query stack reentrantly.
|
||||
unsafe {
|
||||
self.local_state.with_query_stack_unchecked_mut(|stack| {
|
||||
stack.pop(
|
||||
self.database_key_index,
|
||||
#[cfg(debug_assertions)]
|
||||
self.push_len,
|
||||
);
|
||||
})
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(all(feature = "inventory", feature = "accumulator"))]
|
||||
|
||||
//! Test that when having nested tracked functions
|
||||
//! we don't drop any values when accumulating.
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(all(feature = "inventory", feature = "accumulator"))]
|
||||
|
||||
mod common;
|
||||
|
||||
use expect_test::expect;
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(all(feature = "inventory", feature = "accumulator"))]
|
||||
|
||||
mod common;
|
||||
|
||||
use expect_test::expect;
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(all(feature = "inventory", feature = "accumulator"))]
|
||||
|
||||
//! Demonstrates that accumulation is done in the order
|
||||
//! in which things were originally executed.
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(all(feature = "inventory", feature = "accumulator"))]
|
||||
|
||||
//! Accumulate values from within a tracked function.
|
||||
//! Then mutate the values so that the tracked function re-executes.
|
||||
//! Check that we accumulate the appropriate, new values.
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(all(feature = "inventory", feature = "accumulator"))]
|
||||
|
||||
//! Test that we don't get duplicate accumulated values
|
||||
|
||||
mod common;
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(all(feature = "inventory", feature = "accumulator"))]
|
||||
|
||||
//! Demonstrates the workaround of wrapping calls to
|
||||
//! `accumulated` in a tracked function to get better
|
||||
//! reuse.
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(all(feature = "inventory", feature = "accumulator"))]
|
||||
|
||||
//! Accumulator re-use test.
|
||||
//!
|
||||
//! Tests behavior when a query's only inputs
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(all(feature = "inventory", feature = "accumulator"))]
|
||||
|
||||
mod common;
|
||||
use common::{LogDatabase, LoggerDatabase};
|
||||
use expect_test::expect;
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(all(feature = "inventory", feature = "accumulator"))]
|
||||
|
||||
//! Tests that accumulated values are correctly accounted for
|
||||
//! when backdating a value.
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(feature = "inventory")]
|
||||
|
||||
use expect_test::expect;
|
||||
use salsa::{Backtrace, Database, DatabaseImpl};
|
||||
use test_log::test;
|
||||
|
@ -71,15 +73,15 @@ fn backtrace_works() {
|
|||
expect![[r#"
|
||||
query stacktrace:
|
||||
0: query_e(Id(0))
|
||||
at tests/backtrace.rs:30
|
||||
at tests/backtrace.rs:32
|
||||
1: query_d(Id(0))
|
||||
at tests/backtrace.rs:25
|
||||
at tests/backtrace.rs:27
|
||||
2: query_c(Id(0))
|
||||
at tests/backtrace.rs:20
|
||||
at tests/backtrace.rs:22
|
||||
3: query_b(Id(0))
|
||||
at tests/backtrace.rs:15
|
||||
at tests/backtrace.rs:17
|
||||
4: query_a(Id(0))
|
||||
at tests/backtrace.rs:10
|
||||
at tests/backtrace.rs:12
|
||||
"#]]
|
||||
.assert_eq(&backtrace);
|
||||
|
||||
|
@ -87,15 +89,15 @@ fn backtrace_works() {
|
|||
expect![[r#"
|
||||
query stacktrace:
|
||||
0: query_e(Id(1)) -> (R1, Durability::LOW)
|
||||
at tests/backtrace.rs:30
|
||||
at tests/backtrace.rs:32
|
||||
1: query_d(Id(1)) -> (R1, Durability::HIGH)
|
||||
at tests/backtrace.rs:25
|
||||
at tests/backtrace.rs:27
|
||||
2: query_c(Id(1)) -> (R1, Durability::HIGH)
|
||||
at tests/backtrace.rs:20
|
||||
at tests/backtrace.rs:22
|
||||
3: query_b(Id(1)) -> (R1, Durability::HIGH)
|
||||
at tests/backtrace.rs:15
|
||||
at tests/backtrace.rs:17
|
||||
4: query_a(Id(1)) -> (R1, Durability::HIGH)
|
||||
at tests/backtrace.rs:10
|
||||
at tests/backtrace.rs:12
|
||||
"#]]
|
||||
.assert_eq(&backtrace);
|
||||
|
||||
|
@ -103,12 +105,12 @@ fn backtrace_works() {
|
|||
expect![[r#"
|
||||
query stacktrace:
|
||||
0: query_e(Id(2))
|
||||
at tests/backtrace.rs:30
|
||||
at tests/backtrace.rs:32
|
||||
1: query_cycle(Id(2))
|
||||
at tests/backtrace.rs:43
|
||||
at tests/backtrace.rs:45
|
||||
cycle heads: query_cycle(Id(2)) -> IterationCount(0)
|
||||
2: query_f(Id(2))
|
||||
at tests/backtrace.rs:38
|
||||
at tests/backtrace.rs:40
|
||||
"#]]
|
||||
.assert_eq(&backtrace);
|
||||
|
||||
|
@ -116,12 +118,12 @@ fn backtrace_works() {
|
|||
expect![[r#"
|
||||
query stacktrace:
|
||||
0: query_e(Id(3)) -> (R1, Durability::LOW)
|
||||
at tests/backtrace.rs:30
|
||||
at tests/backtrace.rs:32
|
||||
1: query_cycle(Id(3)) -> (R1, Durability::HIGH, iteration = IterationCount(0))
|
||||
at tests/backtrace.rs:43
|
||||
at tests/backtrace.rs:45
|
||||
cycle heads: query_cycle(Id(3)) -> IterationCount(0)
|
||||
2: query_f(Id(3)) -> (R1, Durability::HIGH)
|
||||
at tests/backtrace.rs:38
|
||||
at tests/backtrace.rs:40
|
||||
"#]]
|
||||
.assert_eq(&backtrace);
|
||||
}
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(feature = "inventory")]
|
||||
|
||||
//! Test that auto trait impls exist as expected.
|
||||
|
||||
use std::panic::UnwindSafe;
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(feature = "inventory")]
|
||||
|
||||
#[rustversion::all(stable, since(1.84))]
|
||||
#[test]
|
||||
fn compile_fail() {
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(feature = "inventory")]
|
||||
|
||||
//! Test cases for fixpoint iteration cycle resolution.
|
||||
//!
|
||||
//! These test cases use a generic query setup that allows constructing arbitrary dependency
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(all(feature = "inventory", feature = "accumulator"))]
|
||||
|
||||
use std::collections::HashSet;
|
||||
|
||||
mod common;
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(feature = "inventory")]
|
||||
|
||||
//! It is possible to omit the `cycle_fn`, only specifying `cycle_result` in which case
|
||||
//! an immediate fallback value is used as the cycle handling opposed to doing a fixpoint resolution.
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(feature = "inventory")]
|
||||
|
||||
//! Calling back into the same cycle from your cycle initial function will trigger another cycle.
|
||||
|
||||
#[salsa::tracked]
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(feature = "inventory")]
|
||||
|
||||
//! It's possible to call a Salsa query from within a cycle initial fn.
|
||||
|
||||
#[salsa::tracked]
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(feature = "inventory")]
|
||||
|
||||
//! Tests for incremental validation for queries involved in a cycle.
|
||||
mod common;
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(feature = "inventory")]
|
||||
|
||||
//! Test tracked struct output from a query in a cycle.
|
||||
mod common;
|
||||
use common::{HasLogger, LogDatabase, Logger};
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(feature = "inventory")]
|
||||
|
||||
//! Calling back into the same cycle from your cycle recovery function _can_ work out, as long as
|
||||
//! the overall cycle still converges.
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(feature = "inventory")]
|
||||
|
||||
//! It's possible to call a Salsa query from within a cycle recovery fn.
|
||||
|
||||
#[salsa::tracked]
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(feature = "inventory")]
|
||||
|
||||
use salsa::{Database, Setter};
|
||||
|
||||
#[salsa::tracked]
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(feature = "inventory")]
|
||||
|
||||
use salsa::{Database, Setter};
|
||||
|
||||
#[salsa::input]
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(feature = "inventory")]
|
||||
|
||||
//! Tests for cycles where the cycle head is stored on a tracked struct
|
||||
//! and that tracked struct is freed in a later revision.
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(feature = "inventory")]
|
||||
|
||||
//! Test for cycle handling where a tracked struct created in the first revision
|
||||
//! is stored in the final value of the cycle but isn't recreated in the second
|
||||
//! iteration of the creating query.
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(feature = "inventory")]
|
||||
|
||||
//! Test case for fixpoint iteration cycle resolution.
|
||||
//!
|
||||
//! This test case is intended to simulate a (very simplified) version of a real dataflow analysis
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(feature = "inventory")]
|
||||
|
||||
//! Test that `DeriveWithDb` is correctly derived.
|
||||
|
||||
use expect_test::expect;
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(feature = "inventory")]
|
||||
|
||||
#[salsa::interned(debug)]
|
||||
struct InternedStruct<'db> {
|
||||
name: String,
|
||||
|
@ -20,14 +22,15 @@ fn tracked_fn(db: &dyn salsa::Database, input: InputStruct) -> TrackedStruct<'_>
|
|||
|
||||
#[test]
|
||||
fn execute() {
|
||||
use salsa::plumbing::ZalsaDatabase;
|
||||
let db = salsa::DatabaseImpl::new();
|
||||
|
||||
let _ = InternedStruct::new(&db, "Salsa".to_string());
|
||||
let _ = InternedStruct::new(&db, "Salsa2".to_string());
|
||||
|
||||
// test interned structs
|
||||
let interned = InternedStruct::ingredient(&db)
|
||||
.entries(&db)
|
||||
let interned = InternedStruct::ingredient(db.zalsa())
|
||||
.entries(db.zalsa())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
assert_eq!(interned.len(), 2);
|
||||
|
@ -38,7 +41,7 @@ fn execute() {
|
|||
let input = InputStruct::new(&db, 22);
|
||||
|
||||
let inputs = InputStruct::ingredient(&db)
|
||||
.entries(&db)
|
||||
.entries(db.zalsa())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
assert_eq!(inputs.len(), 1);
|
||||
|
@ -48,7 +51,7 @@ fn execute() {
|
|||
let computed = tracked_fn(&db, input).field(&db);
|
||||
assert_eq!(computed, 44);
|
||||
let tracked = TrackedStruct::ingredient(&db)
|
||||
.entries(&db)
|
||||
.entries(db.zalsa())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
assert_eq!(tracked.len(), 1);
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(feature = "inventory")]
|
||||
|
||||
//! Delete cascade:
|
||||
//!
|
||||
//! * when we delete memoized data, also delete outputs from that data
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(feature = "inventory")]
|
||||
|
||||
//! Basic deletion test:
|
||||
//!
|
||||
//! * entities not created in a revision are deleted, as is any memoized data keyed on them.
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(feature = "inventory")]
|
||||
|
||||
//! Basic deletion test:
|
||||
//!
|
||||
//! * entities not created in a revision are deleted, as is any memoized data keyed on them.
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(feature = "inventory")]
|
||||
|
||||
//! Test that the `Update` derive works as expected
|
||||
|
||||
#[derive(salsa::Update)]
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(feature = "inventory")]
|
||||
|
||||
//! Tests that code using the builder's durability methods compiles.
|
||||
|
||||
use salsa::{Database, Durability, Setter};
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(feature = "inventory")]
|
||||
|
||||
//! Test that a `tracked` fn on a `salsa::input`
|
||||
//! compiles and executes successfully.
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(feature = "inventory")]
|
||||
|
||||
//! Test that if field X of a tracked struct changes but not field Y,
|
||||
//! functions that depend on X re-execute, but those depending only on Y do not
|
||||
//! compiles and executes successfully.
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(feature = "inventory")]
|
||||
|
||||
//! Test that if field X of an input changes but not field Y,
|
||||
//! functions that depend on X re-execute, but those depending only on Y do not
|
||||
//! compiles and executes successfully.
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(feature = "inventory")]
|
||||
|
||||
use std::hash::Hash;
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(feature = "inventory")]
|
||||
|
||||
//! Test that a `tracked` fn on a `salsa::input`
|
||||
//! compiles and executes successfully.
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(feature = "inventory")]
|
||||
|
||||
//! Tests that fields attributed with `#[default]` are initialized with `Default::default()`.
|
||||
|
||||
use salsa::Durability;
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(feature = "inventory")]
|
||||
|
||||
//! Tests that code using the builder's durability methods compiles.
|
||||
|
||||
use salsa::Durability;
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(feature = "inventory")]
|
||||
|
||||
use salsa::plumbing::ZalsaDatabase;
|
||||
use salsa::{Durability, Setter};
|
||||
use test_log::test;
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(feature = "inventory")]
|
||||
|
||||
use salsa::{Durability, Setter};
|
||||
|
||||
#[salsa::interned(no_lifetime)]
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(feature = "inventory")]
|
||||
|
||||
//! Test that a `tracked` fn on a `salsa::input`
|
||||
//! compiles and executes successfully.
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(feature = "inventory")]
|
||||
|
||||
//! Test that a `tracked` fn on a `salsa::input`
|
||||
//! compiles and executes successfully.
|
||||
|
||||
|
@ -130,13 +132,13 @@ fn interning_boxed() {
|
|||
|
||||
#[test]
|
||||
fn interned_structs_have_public_ingredients() {
|
||||
use salsa::plumbing::AsId;
|
||||
use salsa::plumbing::{AsId, ZalsaDatabase};
|
||||
|
||||
let db = salsa::DatabaseImpl::new();
|
||||
let s = InternedString::new(&db, String::from("Hello, world!"));
|
||||
let underlying_id = s.0;
|
||||
|
||||
let data = InternedString::ingredient(&db).data(&db, underlying_id.as_id());
|
||||
let data = InternedString::ingredient(db.zalsa()).data(db.zalsa(), underlying_id.as_id());
|
||||
assert_eq!(data, &(String::from("Hello, world!"),));
|
||||
}
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(feature = "inventory")]
|
||||
|
||||
//! Test that a `tracked` fn on a `salsa::input`
|
||||
//! compiles and executes successfully.
|
||||
|
||||
|
@ -35,7 +37,18 @@ struct InternedString<'db>(
|
|||
const _: () = {
|
||||
use salsa::plumbing as zalsa_;
|
||||
use zalsa_::interned as zalsa_struct_;
|
||||
|
||||
type Configuration_ = InternedString<'static>;
|
||||
|
||||
impl<'db> zalsa_::HasJar for InternedString<'db> {
|
||||
type Jar = zalsa_struct_::JarImpl<Configuration_>;
|
||||
const KIND: zalsa_::JarKind = zalsa_::JarKind::Struct;
|
||||
}
|
||||
|
||||
zalsa_::register_jar! {
|
||||
zalsa_::ErasedJar::erase::<InternedString<'static>>()
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct StructData<'db>(String, InternedString<'db>);
|
||||
|
||||
|
@ -86,11 +99,14 @@ const _: () = {
|
|||
zalsa_::IngredientCache::new();
|
||||
|
||||
let zalsa = db.zalsa();
|
||||
CACHE.get_or_create(zalsa, || {
|
||||
zalsa
|
||||
.lookup_jar_by_type::<zalsa_struct_::JarImpl<Configuration_>>()
|
||||
.get_or_create()
|
||||
})
|
||||
|
||||
// SAFETY: `lookup_jar_by_type` returns a valid ingredient index, and the only
|
||||
// ingredient created by our jar is the struct ingredient.
|
||||
unsafe {
|
||||
CACHE.get_or_create(zalsa, || {
|
||||
zalsa.lookup_jar_by_type::<zalsa_struct_::JarImpl<Configuration_>>()
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
impl zalsa_::AsId for InternedString<'_> {
|
||||
|
@ -115,9 +131,8 @@ const _: () = {
|
|||
impl zalsa_::SalsaStructInDb for InternedString<'_> {
|
||||
type MemoIngredientMap = zalsa_::MemoIngredientSingletonIndex;
|
||||
|
||||
fn lookup_or_create_ingredient_index(aux: &Zalsa) -> salsa::plumbing::IngredientIndices {
|
||||
fn lookup_ingredient_index(aux: &Zalsa) -> salsa::plumbing::IngredientIndices {
|
||||
aux.lookup_jar_by_type::<zalsa_struct_::JarImpl<Configuration_>>()
|
||||
.get_or_create()
|
||||
.into()
|
||||
}
|
||||
|
||||
|
@ -129,6 +144,20 @@ const _: () = {
|
|||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn memo_table(
|
||||
zalsa: &zalsa_::Zalsa,
|
||||
id: zalsa_::Id,
|
||||
current_revision: zalsa_::Revision,
|
||||
) -> zalsa_::MemoTableWithTypes<'_> {
|
||||
// SAFETY: Guaranteed by caller.
|
||||
unsafe {
|
||||
zalsa
|
||||
.table()
|
||||
.memos::<zalsa_struct_::Value<Configuration_>>(id, current_revision)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl zalsa_::Update for InternedString<'_> {
|
||||
|
@ -152,7 +181,8 @@ const _: () = {
|
|||
String: zalsa_::interned::HashEqLike<T0>,
|
||||
{
|
||||
Configuration_::ingredient(db).intern(
|
||||
db.as_dyn_database(),
|
||||
db.zalsa(),
|
||||
db.zalsa_local(),
|
||||
StructKey::<'db>(data, std::marker::PhantomData::default()),
|
||||
|id, data| {
|
||||
StructData(
|
||||
|
@ -166,20 +196,20 @@ const _: () = {
|
|||
where
|
||||
Db_: ?Sized + zalsa_::Database,
|
||||
{
|
||||
let fields = Configuration_::ingredient(db).fields(db.as_dyn_database(), self);
|
||||
let fields = Configuration_::ingredient(db).fields(db.zalsa(), self);
|
||||
std::clone::Clone::clone((&fields.0))
|
||||
}
|
||||
fn other<Db_>(self, db: &'db Db_) -> InternedString<'db>
|
||||
where
|
||||
Db_: ?Sized + zalsa_::Database,
|
||||
{
|
||||
let fields = Configuration_::ingredient(db).fields(db.as_dyn_database(), self);
|
||||
let fields = Configuration_::ingredient(db).fields(db.zalsa(), self);
|
||||
std::clone::Clone::clone((&fields.1))
|
||||
}
|
||||
#[doc = r" Default debug formatting for this struct (may be useful if you define your own `Debug` impl)"]
|
||||
pub fn default_debug_fmt(this: Self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
zalsa_::with_attached_database(|db| {
|
||||
let fields = Configuration_::ingredient(db).fields(db.as_dyn_database(), this);
|
||||
let fields = Configuration_::ingredient(db).fields(db.zalsa(), this);
|
||||
let mut f = f.debug_struct("InternedString");
|
||||
let f = f.field("data", &fields.0);
|
||||
let f = f.field("other", &fields.1);
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(feature = "inventory")]
|
||||
|
||||
//! Test that a `tracked` fn with lru options
|
||||
//! compiles and executes successfully.
|
||||
|
||||
|
|
92
tests/manual_registration.rs
Normal file
92
tests/manual_registration.rs
Normal file
|
@ -0,0 +1,92 @@
|
|||
#![cfg(not(feature = "inventory"))]
|
||||
|
||||
mod ingredients {
|
||||
#[salsa::input]
|
||||
pub(super) struct MyInput {
|
||||
field: u32,
|
||||
}
|
||||
|
||||
#[salsa::tracked]
|
||||
pub(super) struct MyTracked<'db> {
|
||||
pub(super) field: u32,
|
||||
}
|
||||
|
||||
#[salsa::interned]
|
||||
pub(super) struct MyInterned<'db> {
|
||||
pub(super) field: u32,
|
||||
}
|
||||
|
||||
#[salsa::tracked]
|
||||
pub(super) fn intern<'db>(db: &'db dyn salsa::Database, input: MyInput) -> MyInterned<'db> {
|
||||
MyInterned::new(db, input.field(db))
|
||||
}
|
||||
|
||||
#[salsa::tracked]
|
||||
pub(super) fn track<'db>(db: &'db dyn salsa::Database, input: MyInput) -> MyTracked<'db> {
|
||||
MyTracked::new(db, input.field(db))
|
||||
}
|
||||
}
|
||||
|
||||
#[salsa::db]
|
||||
#[derive(Clone, Default)]
|
||||
pub struct DatabaseImpl {
|
||||
storage: salsa::Storage<Self>,
|
||||
}
|
||||
|
||||
#[salsa::db]
|
||||
impl salsa::Database for DatabaseImpl {}
|
||||
|
||||
#[test]
|
||||
fn single_database() {
|
||||
let db = DatabaseImpl {
|
||||
storage: salsa::Storage::builder()
|
||||
.ingredient::<ingredients::track>()
|
||||
.ingredient::<ingredients::intern>()
|
||||
.ingredient::<ingredients::MyInput>()
|
||||
.ingredient::<ingredients::MyTracked<'_>>()
|
||||
.ingredient::<ingredients::MyInterned<'_>>()
|
||||
.build(),
|
||||
};
|
||||
|
||||
let input = ingredients::MyInput::new(&db, 1);
|
||||
|
||||
let tracked = ingredients::track(&db, input);
|
||||
let interned = ingredients::intern(&db, input);
|
||||
|
||||
assert_eq!(tracked.field(&db), 1);
|
||||
assert_eq!(interned.field(&db), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multiple_databases() {
|
||||
let db1 = DatabaseImpl {
|
||||
storage: salsa::Storage::builder()
|
||||
.ingredient::<ingredients::intern>()
|
||||
.ingredient::<ingredients::MyInput>()
|
||||
.ingredient::<ingredients::MyInterned<'_>>()
|
||||
.build(),
|
||||
};
|
||||
|
||||
let input = ingredients::MyInput::new(&db1, 1);
|
||||
let interned = ingredients::intern(&db1, input);
|
||||
assert_eq!(interned.field(&db1), 1);
|
||||
|
||||
// Create a second database with different ingredient indices.
|
||||
let db2 = DatabaseImpl {
|
||||
storage: salsa::Storage::builder()
|
||||
.ingredient::<ingredients::track>()
|
||||
.ingredient::<ingredients::intern>()
|
||||
.ingredient::<ingredients::MyInput>()
|
||||
.ingredient::<ingredients::MyTracked<'_>>()
|
||||
.ingredient::<ingredients::MyInterned<'_>>()
|
||||
.build(),
|
||||
};
|
||||
|
||||
let input = ingredients::MyInput::new(&db2, 2);
|
||||
let interned = ingredients::intern(&db2, input);
|
||||
assert_eq!(interned.field(&db2), 2);
|
||||
|
||||
let input = ingredients::MyInput::new(&db2, 3);
|
||||
let tracked = ingredients::track(&db2, input);
|
||||
assert_eq!(tracked.field(&db2), 3);
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue