Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New database design #538

Merged
merged 29 commits into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
9e8635c
remove upcast_mut
nikomatsakis Jul 27, 2024
bc72bdf
as_salsa_database => as_dyn_database
nikomatsakis Jul 27, 2024
596461c
hide internal methods behind a Zalsa trait
nikomatsakis Jul 27, 2024
1842b1d
(almost) encansulate Runtime into Zalsa
nikomatsakis Jul 27, 2024
64556e9
make event generation lazy
nikomatsakis Jul 27, 2024
daaa780
switch to new database design
nikomatsakis Jul 27, 2024
62f1587
rename Storage to ZalsaImpl, privatize
nikomatsakis Jul 27, 2024
138ca4b
merge handle into the database
nikomatsakis Jul 27, 2024
8e9ebba
improve comments
nikomatsakis Jul 27, 2024
a675810
move local-state into DatabaseImpl
nikomatsakis Jul 28, 2024
f8b1620
pacify the merciless cargo fmt
nikomatsakis Jul 28, 2024
8562824
pacify the merciless clippy
nikomatsakis Jul 28, 2024
34e109d
remove type parameter from `ZalsaImpl`
nikomatsakis Jul 28, 2024
703e12d
remove the Zalsa trait and make it a struct
nikomatsakis Jul 28, 2024
d141cd8
encapsulate Runtime within Zalsa
nikomatsakis Jul 28, 2024
9054377
rename `storage` mod to `zalsa`
nikomatsakis Jul 28, 2024
3254f46
rename LocalState to ZalsaLocal
nikomatsakis Jul 28, 2024
ab112b7
rename local_state to zalsa_local
nikomatsakis Jul 28, 2024
502716d
pacify the merciless cargo fmt
nikomatsakis Jul 28, 2024
4995ce0
Relax dependency constraints
MichaReiser Jul 29, 2024
12e0741
Implement DerefMut
MichaReiser Jul 29, 2024
4d2ccff
return to the database-wrapping-storage setup
nikomatsakis Aug 4, 2024
cafbe92
update debug output
nikomatsakis Aug 4, 2024
9f95b37
add a justfile for convenience
nikomatsakis Jul 28, 2024
bca9180
just cache the index
nikomatsakis Jul 28, 2024
83be1e4
make the Views type miri-safe
nikomatsakis Jul 28, 2024
118e89c
add `ingredient_debug_name` API
nikomatsakis Aug 4, 2024
6ff1975
Update components/salsa-macro-rules/src/setup_input_struct.rs
nikomatsakis Aug 4, 2024
1bce41f
stop ignoring miri
nikomatsakis Aug 4, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@ repository = "https://github.com/salsa-rs/salsa"
description = "A generic framework for on-demand, incrementalized computation (experimental)"

[dependencies]
arc-swap = "1.6.0"
boomphf = "0.6.0"
crossbeam = "0.8.1"
dashmap = "6.0.1"
hashlink = "0.9.1"
arc-swap = "1"
boomphf = "0.6"
crossbeam = "0.8"
dashmap = "6"
hashlink = "0.9"
indexmap = "2"
orx-concurrent-vec = "2.2.0"
orx-concurrent-vec = "2"
tracing = "0.1"
parking_lot = "0.12.1"
rustc-hash = "2.0.0"
parking_lot = "0.12"
rustc-hash = "2"
salsa-macro-rules = { version = "0.1.0", path = "components/salsa-macro-rules" }
salsa-macros = { path = "components/salsa-macros" }
smallvec = "1.0.0"
smallvec = "1"
lazy_static = "1"

[dev-dependencies]
annotate-snippets = "0.11.4"
Expand Down
2 changes: 1 addition & 1 deletion benches/incremental.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn many_tracked_structs(criterion: &mut Criterion) {
criterion.bench_function("many_tracked_structs", |b| {
b.iter_batched_ref(
|| {
let db = salsa::default_database();
let db = salsa::DatabaseImpl::new();

let input = Input::new(&db, 1_000);
let input2 = Input::new(&db, 1);
Expand Down
4 changes: 2 additions & 2 deletions components/salsa-macro-rules/src/setup_accumulator_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ macro_rules! setup_accumulator_impl {

fn $ingredient(db: &dyn $zalsa::Database) -> &$zalsa_struct::IngredientImpl<$Struct> {
$CACHE.get_or_create(db, || {
db.add_or_lookup_jar_by_type(&<$zalsa_struct::JarImpl<$Struct>>::default())
db.zalsa().add_or_lookup_jar_by_type(&<$zalsa_struct::JarImpl<$Struct>>::default())
})
}

Expand All @@ -35,7 +35,7 @@ macro_rules! setup_accumulator_impl {
where
Db: ?Sized + $zalsa::Database,
{
let db = db.as_salsa_database();
let db = db.as_dyn_database();
$ingredient(db).push(db, self);
}
}
Expand Down
22 changes: 12 additions & 10 deletions components/salsa-macro-rules/src/setup_input_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,17 @@ macro_rules! setup_input_struct {
static CACHE: $zalsa::IngredientCache<$zalsa_struct::IngredientImpl<$Configuration>> =
$zalsa::IngredientCache::new();
CACHE.get_or_create(db, || {
db.add_or_lookup_jar_by_type(&<$zalsa_struct::JarImpl<$Configuration>>::default())
db.zalsa().add_or_lookup_jar_by_type(&<$zalsa_struct::JarImpl<$Configuration>>::default())
})
}

pub fn ingredient_mut(db: &mut dyn $zalsa::Database) -> (&mut $zalsa_struct::IngredientImpl<Self>, &mut $zalsa::Runtime) {
let index = db.add_or_lookup_jar_by_type(&<$zalsa_struct::JarImpl<$Configuration>>::default());
let (ingredient, runtime) = db.lookup_ingredient_mut(index);
pub fn ingredient_mut(db: &mut dyn $zalsa::Database) -> (&mut $zalsa_struct::IngredientImpl<Self>, $zalsa::Revision) {
let zalsa_mut = db.zalsa_mut();
let index = zalsa_mut.add_or_lookup_jar_by_type(&<$zalsa_struct::JarImpl<$Configuration>>::default());
let current_revision = zalsa_mut.current_revision();
let ingredient = zalsa_mut.lookup_ingredient_mut(index);
let ingredient = ingredient.assert_type_mut::<$zalsa_struct::IngredientImpl<Self>>();
(ingredient, runtime)
(ingredient, current_revision)
}
}

Expand Down Expand Up @@ -128,7 +130,7 @@ macro_rules! setup_input_struct {
{
let current_revision = $zalsa::current_revision(db);
let stamps = $zalsa::Array::new([$zalsa::stamp(current_revision, Default::default()); $N]);
$Configuration::ingredient(db.as_salsa_database()).new_input(($($field_id,)*), stamps)
$Configuration::ingredient(db.as_dyn_database()).new_input(($($field_id,)*), stamps)
}

$(
Expand All @@ -137,8 +139,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 fields = $Configuration::ingredient(db.as_salsa_database()).field(
db.as_salsa_database(),
let fields = $Configuration::ingredient(db.as_dyn_database()).field(
db.as_dyn_database(),
self,
$field_index,
);
Expand All @@ -157,7 +159,7 @@ 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, runtime) = $Configuration::ingredient_mut(db.as_salsa_database_mut());
let (ingredient, runtime) = $Configuration::ingredient_mut(db.as_dyn_database_mut());
nikomatsakis marked this conversation as resolved.
Show resolved Hide resolved
$zalsa::input::SetterImpl::new(
runtime,
self,
Expand All @@ -174,7 +176,7 @@ macro_rules! setup_input_struct {
// FIXME(rust-lang/rust#65991): The `db` argument *should* have the type `dyn Database`
$Db: ?Sized + salsa::Database,
{
$Configuration::ingredient(db.as_salsa_database()).get_singleton_input()
$Configuration::ingredient(db.as_dyn_database()).get_singleton_input()
}

#[track_caller]
Expand Down
7 changes: 3 additions & 4 deletions components/salsa-macro-rules/src/setup_interned_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ macro_rules! setup_interned_struct {
{
static CACHE: $zalsa::IngredientCache<$zalsa_struct::IngredientImpl<$Configuration>> =
$zalsa::IngredientCache::new();
CACHE.get_or_create(db.as_salsa_database(), || {
db.add_or_lookup_jar_by_type(&<$zalsa_struct::JarImpl<$Configuration>>::default())
CACHE.get_or_create(db.as_dyn_database(), || {
db.zalsa().add_or_lookup_jar_by_type(&<$zalsa_struct::JarImpl<$Configuration>>::default())
})
}
}
Expand Down Expand Up @@ -135,7 +135,7 @@ macro_rules! setup_interned_struct {
$Db: ?Sized + salsa::Database,
{
let current_revision = $zalsa::current_revision(db);
$Configuration::ingredient(db).intern(db.as_salsa_database(), ($($field_id,)*))
$Configuration::ingredient(db).intern(db.as_dyn_database(), ($($field_id,)*))
}

$(
Expand All @@ -144,7 +144,6 @@ macro_rules! setup_interned_struct {
// FIXME(rust-lang/rust#65991): The `db` argument *should* have the type `dyn Database`
$Db: ?Sized + $zalsa::Database,
{
let runtime = db.runtime();
let fields = $Configuration::ingredient(db).fields(self);
$zalsa::maybe_clone!(
$field_option,
Expand Down
44 changes: 23 additions & 21 deletions components/salsa-macro-rules/src/setup_tracked_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,18 @@ macro_rules! setup_tracked_fn {

impl $Configuration {
fn fn_ingredient(db: &dyn $Db) -> &$zalsa::function::IngredientImpl<$Configuration> {
$FN_CACHE.get_or_create(db.as_salsa_database(), || {
$FN_CACHE.get_or_create(db.as_dyn_database(), || {
<dyn $Db as $Db>::zalsa_db(db);
db.add_or_lookup_jar_by_type(&$Configuration)
db.zalsa().add_or_lookup_jar_by_type(&$Configuration)
})
}

$zalsa::macro_if! { $needs_interner =>
fn intern_ingredient(
db: &dyn $Db,
) -> &$zalsa::interned::IngredientImpl<$Configuration> {
$INTERN_CACHE.get_or_create(db.as_salsa_database(), || {
db.add_or_lookup_jar_by_type(&$Configuration).successor(0)
$INTERN_CACHE.get_or_create(db.as_dyn_database(), || {
db.zalsa().add_or_lookup_jar_by_type(&$Configuration).successor(0)
})
}
}
Expand Down Expand Up @@ -193,7 +193,7 @@ macro_rules! setup_tracked_fn {
if $needs_interner {
$Configuration::intern_ingredient(db).data(key).clone()
} else {
$zalsa::LookupId::lookup_id(key, db.as_salsa_database())
$zalsa::LookupId::lookup_id(key, db.as_dyn_database())
}
}
}
Expand Down Expand Up @@ -233,7 +233,7 @@ macro_rules! setup_tracked_fn {
use salsa::plumbing as $zalsa;
let key = $zalsa::macro_if! {
if $needs_interner {
$Configuration::intern_ingredient($db).intern_id($db.as_salsa_database(), ($($input_id),*))
$Configuration::intern_ingredient($db).intern_id($db.as_dyn_database(), ($($input_id),*))
} else {
$zalsa::AsId::as_id(&($($input_id),*))
}
Expand Down Expand Up @@ -265,24 +265,26 @@ macro_rules! setup_tracked_fn {
} }
}

let result = $zalsa::macro_if! {
if $needs_interner {
{
let key = $Configuration::intern_ingredient($db).intern_id($db.as_salsa_database(), ($($input_id),*));
$Configuration::fn_ingredient($db).fetch($db, key)
$zalsa::attach($db, || {
let result = $zalsa::macro_if! {
if $needs_interner {
{
let key = $Configuration::intern_ingredient($db).intern_id($db.as_dyn_database(), ($($input_id),*));
$Configuration::fn_ingredient($db).fetch($db, key)
}
} else {
$Configuration::fn_ingredient($db).fetch($db, $zalsa::AsId::as_id(&($($input_id),*)))
}
} else {
$Configuration::fn_ingredient($db).fetch($db, $zalsa::AsId::as_id(&($($input_id),*)))
}
};
};

$zalsa::macro_if! {
if $return_ref {
result
} else {
<$output_ty as std::clone::Clone>::clone(result)
$zalsa::macro_if! {
if $return_ref {
result
} else {
<$output_ty as std::clone::Clone>::clone(result)
}
}
}
})
}
};
}
10 changes: 5 additions & 5 deletions components/salsa-macro-rules/src/setup_tracked_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,14 @@ macro_rules! setup_tracked_struct {
static CACHE: $zalsa::IngredientCache<$zalsa_struct::IngredientImpl<$Configuration>> =
$zalsa::IngredientCache::new();
CACHE.get_or_create(db, || {
db.add_or_lookup_jar_by_type(&<$zalsa_struct::JarImpl::<$Configuration>>::default())
db.zalsa().add_or_lookup_jar_by_type(&<$zalsa_struct::JarImpl::<$Configuration>>::default())
})
}
}

impl<$db_lt> $zalsa::LookupId<$db_lt> for $Struct<$db_lt> {
fn lookup_id(id: salsa::Id, db: &$db_lt dyn $zalsa::Database) -> Self {
$Configuration::ingredient(db).lookup_struct(db.runtime(), id)
$Configuration::ingredient(db).lookup_struct(db, id)
}
}

Expand Down Expand Up @@ -192,8 +192,8 @@ 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_salsa_database()).new_struct(
db.as_salsa_database(),
$Configuration::ingredient(db.as_dyn_database()).new_struct(
db.as_dyn_database(),
($($field_id,)*)
)
}
Expand All @@ -204,7 +204,7 @@ macro_rules! setup_tracked_struct {
// FIXME(rust-lang/rust#65991): The `db` argument *should* have the type `dyn Database`
$Db: ?Sized + $zalsa::Database,
{
let fields = unsafe { self.0.as_ref() }.field(db.as_salsa_database(), $field_index);
let fields = unsafe { self.0.as_ref() }.field(db.as_dyn_database(), $field_index);
$crate::maybe_clone!(
$field_option,
$field_ty,
Expand Down
54 changes: 2 additions & 52 deletions components/salsa-macros/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ struct DbMacro {
impl DbMacro {
fn try_db(self, input: syn::Item) -> syn::Result<TokenStream> {
match input {
syn::Item::Struct(input) => {
let has_storage_impl = self.has_storage_impl(&input)?;
Ok(quote! {
#has_storage_impl
#input
})
}
syn::Item::Trait(mut input) => {
self.add_salsa_view_method(&mut input)?;
Ok(quote! {
Expand All @@ -53,54 +46,11 @@ impl DbMacro {
}
_ => Err(syn::Error::new_spanned(
input,
"`db` must be applied to a struct, trait, or impl",
"`db` must be applied to a trait or impl",
)),
}
}

fn find_storage_field(&self, input: &syn::ItemStruct) -> syn::Result<syn::Ident> {
let storage = "storage";
for field in input.fields.iter() {
if let Some(i) = &field.ident {
if i == storage {
return Ok(i.clone());
}
} else {
return Err(syn::Error::new_spanned(
field,
"database struct must be a braced struct (`{}`) with a field named `storage`",
));
}
}

Err(syn::Error::new_spanned(
&input.ident,
"database struct must be a braced struct (`{}`) with a field named `storage`",
))
}

fn has_storage_impl(&self, input: &syn::ItemStruct) -> syn::Result<TokenStream> {
let storage = self.find_storage_field(input)?;
let db = &input.ident;
let zalsa = self.hygiene.ident("zalsa");

Ok(quote! {
const _: () = {
use salsa::plumbing as #zalsa;

unsafe impl #zalsa::HasStorage for #db {
fn storage(&self) -> &#zalsa::Storage<Self> {
&self.#storage
}

fn storage_mut(&mut self) -> &mut #zalsa::Storage<Self> {
&mut self.#storage
}
}
};
})
}

fn add_salsa_view_method(&self, input: &mut syn::ItemTrait) -> syn::Result<()> {
input.items.push(parse_quote! {
#[doc(hidden)]
Expand All @@ -123,7 +73,7 @@ impl DbMacro {
#[doc(hidden)]
fn zalsa_db(&self) {
use salsa::plumbing as #zalsa;
#zalsa::views(self).add::<Self, dyn #TraitPath>(|t| t, |t| t);
#zalsa::views(self).add::<Self, dyn #TraitPath>(|t| t);
}
});
Ok(())
Expand Down
Loading
Loading