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 all 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
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ jobs:

miri:
name: Miri
continue-on-error: true # FIXME: https://github.com/salsa-rs/salsa/issues/520
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand Down
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
24 changes: 13 additions & 11 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,9 +159,9 @@ 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, revision) = $Configuration::ingredient_mut(db.as_dyn_database_mut());
$zalsa::input::SetterImpl::new(
runtime,
revision,
self,
$field_index,
ingredient,
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
2 changes: 1 addition & 1 deletion components/salsa-macros/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,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
38 changes: 19 additions & 19 deletions examples/calc/db.rs
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
use std::sync::{Arc, Mutex};

// ANCHOR: db_struct
#[derive(Default)]
#[salsa::db]
pub(crate) struct Database {
#[derive(Default)]
pub struct CalcDatabaseImpl {
storage: salsa::Storage<Self>,

// The logs are only used for testing and demonstrating reuse:
//
logs: Option<Arc<Mutex<Vec<String>>>>,
logs: Arc<Mutex<Option<Vec<String>>>>,
}
// ANCHOR_END: db_struct

impl Database {
impl CalcDatabaseImpl {
/// Enable logging of each salsa event.
#[cfg(test)]
pub fn enable_logging(self) -> Self {
assert!(self.logs.is_none());
Self {
storage: self.storage,
logs: Some(Default::default()),
pub fn enable_logging(&self) {
let mut logs = self.logs.lock().unwrap();
if logs.is_none() {
*logs = Some(vec![]);
}
}

#[cfg(test)]
pub fn take_logs(&mut self) -> Vec<String> {
if let Some(logs) = &self.logs {
std::mem::take(&mut *logs.lock().unwrap())
pub fn take_logs(&self) -> Vec<String> {
let mut logs = self.logs.lock().unwrap();
if let Some(logs) = &mut *logs {
std::mem::take(logs)
} else {
panic!("logs not enabled");
vec![]
}
}
}

// ANCHOR: db_impl
#[salsa::db]
impl salsa::Database for Database {
fn salsa_event(&self, event: salsa::Event) {
impl salsa::Database for CalcDatabaseImpl {
fn salsa_event(&self, event: &dyn Fn() -> salsa::Event) {
let event = event();
eprintln!("Event: {event:?}");
// Log interesting events, if logging is enabled
if let Some(logs) = &self.logs {
// don't log boring events
if let Some(logs) = &mut *self.logs.lock().unwrap() {
// only log interesting events
if let salsa::EventKind::WillExecute { .. } = event.kind {
logs.lock().unwrap().push(format!("Event: {event:?}"));
logs.push(format!("Event: {event:?}"));
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion examples/calc/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use db::CalcDatabaseImpl;
use ir::{Diagnostic, SourceProgram};
use salsa::Database as Db;

Expand All @@ -8,7 +9,7 @@ mod parser;
mod type_check;

pub fn main() {
let db = db::Database::default();
let db: CalcDatabaseImpl = Default::default();
let source_program = SourceProgram::new(&db, String::new());
compile::compile(&db, source_program);
let diagnostics = compile::compile::accumulated::<Diagnostic>(&db, source_program);
Expand Down
Loading
Loading