This commit is contained in:
Niko Matsakis 2024-07-17 09:14:20 -04:00
parent b08015e639
commit 585b720fff
5 changed files with 36 additions and 64 deletions

View file

@ -14,4 +14,12 @@ macro_rules! macro_if {
(if false { $($t:tt)* } else { $($f:tt)*}) => {
$($f)*
};
(if0 0 { $($t:tt)* } else { $($f:tt)*}) => {
$($t)*
};
(if0 $n:literal { $($t:tt)* } else { $($f:tt)*}) => {
$($f)*
};
}

View file

@ -46,6 +46,9 @@ macro_rules! setup_fn {
// If true, the input needs an interner (because it has >1 argument).
needs_interner: $needs_interner:tt,
// LRU capacity (a literal, maybe 0)
lru: $lru:tt,
// Annoyingly macro-rules hygiene does not extend to items defined in the macro.
// We have the procedural macro generate names for those items that are
// not used elsewhere in the user's code.
@ -181,21 +184,21 @@ macro_rules! setup_fn {
&self,
first_index: $zalsa::IngredientIndex,
) -> Vec<Box<dyn $zalsa::Ingredient>> {
let mut fn_ingredient = <$zalsa::function::IngredientImpl<$Configuration>>::new(
first_index,
);
fn_ingredient.set_capacity($lru);
$zalsa::macro_if! {
if $needs_interner {
vec![
Box::new(<$zalsa::function::IngredientImpl<$Configuration>>::new(
first_index,
)),
Box::new(fn_ingredient),
Box::new(<$zalsa::interned::IngredientImpl<$Configuration>>::new(
first_index.successor(0)
)),
]
} else {
vec![
Box::new(<$zalsa::function::IngredientImpl<$Configuration>>::new(
first_index,
)),
Box::new(fn_ingredient),
]
}
}
@ -233,6 +236,13 @@ macro_rules! setup_fn {
)
}
}
$zalsa::macro_if! { if0 $lru { } else {
#[allow(dead_code)]
fn set_lru_capacity(db: &dyn $Db, value: usize) {
$Configuration::fn_ingredient(db).set_capacity(value);
}
} }
}
$zalsa::attach_database($db, || {

View file

@ -1,4 +1,4 @@
use proc_macro2::{Span, TokenStream};
use proc_macro2::{Literal, Span, TokenStream};
use syn::{spanned::Spanned, ItemFn};
use crate::{db_lifetime, hygiene::Hygiene, options::Options, xform::ChangeLt};
@ -98,6 +98,8 @@ impl Macro {
FunctionType::Constant | FunctionType::SalsaStruct => false,
};
let lru = Literal::usize_unsuffixed(self.args.lru.unwrap_or(0));
Ok(crate::debug::dump_tokens(
fn_name,
quote![salsa::plumbing::setup_fn! {
@ -115,6 +117,7 @@ impl Macro {
cycle_recovery_strategy: #cycle_recovery_strategy,
is_specifiable: #is_specifiable,
needs_interner: #needs_interner,
lru: #lru,
unused_names: [
#zalsa,
#Configuration,

View file

@ -1,49 +0,0 @@
#![allow(warnings)]
use expect_test::expect;
#[salsa::db]
trait Db: salsa::Database {}
#[derive(Clone, Debug)]
struct Field {}
#[salsa::input]
struct MyInput {
#[id]
id_one: u32,
#[id]
id_two: u16,
field: Field,
}
#[salsa::db]
#[derive(Default)]
struct Database {
storage: salsa::Storage<Self>,
}
#[salsa::db]
impl salsa::Database for Database {}
#[salsa::db]
impl Db for Database {}
#[test]
fn test_debug() {
let mut db = Database::default();
let input = MyInput::new(&db, 50, 10, Field {});
let actual = format!("{:?}", input.debug(&db));
let expected = expect!["MyInput { [salsa id]: 0, id_one: 50, id_two: 10, field: Field }"];
expected.assert_eq(&actual);
}
#[test]
fn test_set() {
let mut db = Database::default();
let input = MyInput::new(&mut db, 50, 10, Field {});
input.set_field(&mut db).to(Field {});
}

View file

@ -6,15 +6,13 @@ use std::sync::{
Arc,
};
use salsa::Database;
use salsa::Database as _;
mod common;
use common::{HasLogger, Logger};
use test_log::test;
#[salsa::jar(db = Db)]
struct Jar(MyInput, get_hot_potato, get_hot_potato2, get_volatile);
trait Db: salsa::DbWithJar<Jar> + HasLogger {}
#[salsa::db]
trait Db: salsa::Database + HasLogger {}
#[derive(Debug, PartialEq, Eq)]
struct HotPotato(u32);
@ -41,7 +39,7 @@ struct MyInput {
field: u32,
}
#[salsa::tracked(jar = Jar, lru = 32)]
#[salsa::tracked(lru = 32)]
fn get_hot_potato(db: &dyn Db, input: MyInput) -> Arc<HotPotato> {
db.push_log(format!("get_hot_potato({:?})", input.field(db)));
Arc::new(HotPotato::new(input.field(db)))
@ -53,22 +51,24 @@ fn get_hot_potato2(db: &dyn Db, input: MyInput) -> u32 {
get_hot_potato(db, input).0
}
#[salsa::tracked(jar = Jar, lru = 32)]
#[salsa::tracked(lru = 32)]
fn get_volatile(db: &dyn Db, _input: MyInput) -> usize {
static COUNTER: AtomicUsize = AtomicUsize::new(0);
db.report_untracked_read();
COUNTER.fetch_add(1, Ordering::SeqCst)
}
#[salsa::db(Jar)]
#[salsa::db]
#[derive(Default)]
struct DatabaseImpl {
storage: salsa::Storage<Self>,
logger: Logger,
}
#[salsa::db]
impl salsa::Database for DatabaseImpl {}
#[salsa::db]
impl Db for DatabaseImpl {}
impl HasLogger for DatabaseImpl {