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

feature: add #[salsa::supertype] #727

Merged
merged 9 commits into from
Feb 25, 2025
85 changes: 85 additions & 0 deletions benches/compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,25 @@ pub struct InternedInput<'db> {
pub text: String,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, salsa::Supertype)]
enum SupertypeInput<'db> {
InternedInput(InternedInput<'db>),
Input(Input),
}

#[salsa::tracked]
pub fn interned_length<'db>(db: &'db dyn salsa::Database, input: InternedInput<'db>) -> usize {
input.text(db).len()
}

#[salsa::tracked]
pub fn either_length<'db>(db: &'db dyn salsa::Database, input: SupertypeInput<'db>) -> usize {
match input {
SupertypeInput::InternedInput(input) => interned_length(db, input),
SupertypeInput::Input(input) => length(db, input),
}
}

fn mutating_inputs(c: &mut Criterion) {
let mut group: codspeed_criterion_compat::BenchmarkGroup<
codspeed_criterion_compat::measurement::WallTime,
Expand Down Expand Up @@ -150,6 +164,77 @@ fn inputs(c: &mut Criterion) {
)
});

group.bench_function(BenchmarkId::new("new", "SupertypeInput"), |b| {
b.iter_batched_ref(
|| {
let db = salsa::DatabaseImpl::default();

// Prepopulate ingredients.
let input = SupertypeInput::Input(Input::new(
black_box(&db),
black_box("hello, world!".to_owned()),
));
let interned_input = SupertypeInput::InternedInput(InternedInput::new(
black_box(&db),
black_box("hello, world!".to_owned()),
));
let len = either_length(black_box(&db), black_box(input));
assert_eq!(black_box(len), 13);
let len = either_length(black_box(&db), black_box(interned_input));
assert_eq!(black_box(len), 13);

db
},
|db| {
let input = SupertypeInput::Input(Input::new(
black_box(db),
black_box("hello, world!".to_owned()),
));
let interned_input = SupertypeInput::InternedInput(InternedInput::new(
black_box(db),
black_box("hello, world!".to_owned()),
));
let len = either_length(black_box(db), black_box(input));
assert_eq!(black_box(len), 13);
let len = either_length(black_box(db), black_box(interned_input));
assert_eq!(black_box(len), 13);
},
BatchSize::SmallInput,
)
});

group.bench_function(BenchmarkId::new("amortized", "SupertypeInput"), |b| {
b.iter_batched_ref(
|| {
let db = salsa::DatabaseImpl::default();

let input = SupertypeInput::Input(Input::new(
black_box(&db),
black_box("hello, world!".to_owned()),
));
let interned_input = SupertypeInput::InternedInput(InternedInput::new(
black_box(&db),
black_box("hello, world!".to_owned()),
));
// we can't pass this along otherwise, and the lifetime is generally informational
let interned_input: SupertypeInput<'static> = unsafe { transmute(interned_input) };
let len = either_length(black_box(&db), black_box(input));
assert_eq!(black_box(len), 13);
let len = either_length(black_box(&db), black_box(interned_input));
assert_eq!(black_box(len), 13);

(db, input, interned_input)
},
|&mut (ref db, input, interned_input)| {
let len = either_length(black_box(db), black_box(input));
assert_eq!(black_box(len), 13);
let len = either_length(black_box(db), black_box(interned_input));
assert_eq!(black_box(len), 13);
},
BatchSize::SmallInput,
)
});

group.finish();
}

Expand Down
3 changes: 2 additions & 1 deletion components/salsa-macro-rules/src/setup_accumulator_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ macro_rules! setup_accumulator_impl {

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

Expand Down
21 changes: 16 additions & 5 deletions components/salsa-macro-rules/src/setup_input_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ macro_rules! setup_input_struct {
static CACHE: $zalsa::IngredientCache<$zalsa_struct::IngredientImpl<$Configuration>> =
$zalsa::IngredientCache::new();
CACHE.get_or_create(db, || {
db.zalsa().add_or_lookup_jar_by_type(&<$zalsa_struct::JarImpl<$Configuration>>::default())
db.zalsa().add_or_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();
let current_revision = zalsa_mut.new_revision();
let index = zalsa_mut.add_or_lookup_jar_by_type(&<$zalsa_struct::JarImpl<$Configuration>>::default());
let index = zalsa_mut.add_or_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)
Expand Down Expand Up @@ -135,8 +135,19 @@ macro_rules! setup_input_struct {
}

impl $zalsa::SalsaStructInDb for $Struct {
fn lookup_ingredient_index(aux: &dyn $zalsa::JarAux) -> core::option::Option<$zalsa::IngredientIndex> {
aux.lookup_jar_by_type(&<$zalsa_struct::JarImpl<$Configuration>>::default())
type MemoIngredientMap = $zalsa::MemoIngredientSingletonIndex;

fn lookup_or_create_ingredient_index(aux: &$zalsa::Zalsa) -> $zalsa::IngredientIndices {
aux.add_or_lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>().into()
}

#[inline]
fn cast(id: $zalsa::Id, type_id: $zalsa::TypeId) -> $zalsa::Option<Self> {
if type_id == $zalsa::TypeId::of::<$Struct>() {
$zalsa::Some($Struct(id))
} else {
$zalsa::None
}
}
}

Expand Down Expand Up @@ -198,7 +209,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_dyn_database()).get_singleton_input()
$Configuration::ingredient(db.as_dyn_database()).get_singleton_input(db)
}

#[track_caller]
Expand Down
17 changes: 14 additions & 3 deletions components/salsa-macro-rules/src/setup_interned_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ macro_rules! setup_interned_struct {
static CACHE: $zalsa::IngredientCache<$zalsa_struct::IngredientImpl<$Configuration>> =
$zalsa::IngredientCache::new();
CACHE.get_or_create(db.as_dyn_database(), || {
db.zalsa().add_or_lookup_jar_by_type(&<$zalsa_struct::JarImpl<$Configuration>>::default())
db.zalsa().add_or_lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>()
})
}
}
Expand Down Expand Up @@ -171,8 +171,19 @@ macro_rules! setup_interned_struct {
}

impl< $($db_lt_arg)? > $zalsa::SalsaStructInDb for $Struct< $($db_lt_arg)? > {
fn lookup_ingredient_index(aux: &dyn $zalsa::JarAux) -> core::option::Option<$zalsa::IngredientIndex> {
aux.lookup_jar_by_type(&<$zalsa_struct::JarImpl<$Configuration>>::default())
type MemoIngredientMap = $zalsa::MemoIngredientSingletonIndex;

fn lookup_or_create_ingredient_index(aux: &$zalsa::Zalsa) -> $zalsa::IngredientIndices {
aux.add_or_lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>().into()
}

#[inline]
fn cast(id: $zalsa::Id, type_id: $zalsa::TypeId) -> $zalsa::Option<Self> {
if type_id == $zalsa::TypeId::of::<$Struct>() {
$zalsa::Some(<$Struct as $zalsa::FromId>::from_id(id))
} else {
$zalsa::None
}
}
}

Expand Down
57 changes: 39 additions & 18 deletions components/salsa-macro-rules/src/setup_tracked_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,19 @@ macro_rules! setup_tracked_fn {
$zalsa::IngredientCache::new();

impl $zalsa::SalsaStructInDb for $InternedData<'_> {
fn lookup_ingredient_index(_aux: &dyn $zalsa::JarAux) -> core::option::Option<$zalsa::IngredientIndex> {
None
type MemoIngredientMap = $zalsa::MemoIngredientSingletonIndex;

fn lookup_or_create_ingredient_index(aux: &$zalsa::Zalsa) -> $zalsa::IngredientIndices {
$zalsa::IngredientIndices::empty()
}

#[inline]
fn cast(id: $zalsa::Id, type_id: ::core::any::TypeId) -> Option<Self> {
if type_id == ::core::any::TypeId::of::<$InternedData>() {
Some($InternedData(id, ::core::marker::PhantomData))
} else {
None
}
}
}

Expand Down Expand Up @@ -132,13 +143,13 @@ macro_rules! setup_tracked_fn {
fn fn_ingredient(db: &dyn $Db) -> &$zalsa::function::IngredientImpl<$Configuration> {
$FN_CACHE.get_or_create(db.as_dyn_database(), || {
<dyn $Db as $Db>::zalsa_db(db);
db.zalsa().add_or_lookup_jar_by_type(&$Configuration)
db.zalsa().add_or_lookup_jar_by_type::<$Configuration>()
})
}

pub fn fn_ingredient_mut(db: &mut dyn $Db) -> &mut $zalsa::function::IngredientImpl<Self> {
let zalsa_mut = db.zalsa_mut();
let index = zalsa_mut.add_or_lookup_jar_by_type(&$Configuration);
let index = zalsa_mut.add_or_lookup_jar_by_type::<$Configuration>();
let (ingredient, _) = zalsa_mut.lookup_ingredient_mut(index);
ingredient.assert_type_mut::<$zalsa::function::IngredientImpl<Self>>()
}
Expand All @@ -148,7 +159,7 @@ macro_rules! setup_tracked_fn {
db: &dyn $Db,
) -> &$zalsa::interned::IngredientImpl<$Configuration> {
$INTERN_CACHE.get_or_create(db.as_dyn_database(), || {
db.zalsa().add_or_lookup_jar_by_type(&$Configuration).successor(0)
db.zalsa().add_or_lookup_jar_by_type::<$Configuration>().successor(0)
})
}
}
Expand Down Expand Up @@ -201,33 +212,43 @@ macro_rules! setup_tracked_fn {
if $needs_interner {
$Configuration::intern_ingredient(db).data(db.as_dyn_database(), key).clone()
} else {
$zalsa::FromId::from_id(key)
$zalsa::FromIdWithDb::from_id(key, db)
}
}
}
}

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)
}
}
}

fn create_ingredients(
&self,
aux: &dyn $zalsa::JarAux,
zalsa: &$zalsa::Zalsa,
first_index: $zalsa::IngredientIndex,
struct_index: $zalsa::IngredientIndices,
) -> Vec<Box<dyn $zalsa::Ingredient>> {
let struct_index = $zalsa::macro_if! {
let struct_index: $zalsa::IngredientIndices = $zalsa::macro_if! {
if $needs_interner {
first_index.successor(0)
first_index.successor(0).into()
} else {
<$InternedData as $zalsa::SalsaStructInDb>::lookup_ingredient_index(aux)
.expect(
"Salsa struct is passed as an argument of a tracked function, but its ingredient hasn't been added!"
)
struct_index
}
};
let memo_ingredient_indices = From::from((zalsa, struct_index, first_index));

let fn_ingredient = <$zalsa::function::IngredientImpl<$Configuration>>::new(
struct_index,
first_index,
aux,
memo_ingredient_indices,
$lru
);
$zalsa::macro_if! {
Expand All @@ -246,8 +267,8 @@ macro_rules! setup_tracked_fn {
}
}

fn salsa_struct_type_id(&self) -> Option<core::any::TypeId> {
None
fn id_struct_type_id() -> $zalsa::TypeId {
$zalsa::TypeId::of::<$InternedData<'static>>()
}
}

Expand Down
17 changes: 14 additions & 3 deletions components/salsa-macro-rules/src/setup_tracked_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ macro_rules! setup_tracked_struct {
$zalsa::IngredientCache::new();

CACHE.get_or_create(db, || {
db.zalsa().add_or_lookup_jar_by_type(&<$zalsa_struct::JarImpl::<$Configuration>>::default())
db.zalsa().add_or_lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>()
})
}
}
Expand All @@ -198,8 +198,19 @@ macro_rules! setup_tracked_struct {
}

impl $zalsa::SalsaStructInDb for $Struct<'_> {
fn lookup_ingredient_index(aux: &dyn $zalsa::JarAux) -> core::option::Option<$zalsa::IngredientIndex> {
aux.lookup_jar_by_type(&<$zalsa_struct::JarImpl<$Configuration>>::default())
type MemoIngredientMap = $zalsa::MemoIngredientSingletonIndex;

fn lookup_or_create_ingredient_index(aux: &$zalsa::Zalsa) -> $zalsa::IngredientIndices {
aux.add_or_lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>().into()
}

#[inline]
fn cast(id: $zalsa::Id, type_id: $zalsa::TypeId) -> $zalsa::Option<Self> {
if type_id == $zalsa::TypeId::of::<$Struct>() {
$zalsa::Some(<$Struct as $zalsa::FromId>::from_id(id))
} else {
$zalsa::None
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions components/salsa-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ mod input;
mod interned;
mod options;
mod salsa_struct;
mod supertype;
mod tracked;
mod tracked_fn;
mod tracked_impl;
Expand All @@ -66,6 +67,11 @@ pub fn interned(args: TokenStream, input: TokenStream) -> TokenStream {
interned::interned(args, input)
}

#[proc_macro_derive(Supertype)]
pub fn supertype(input: TokenStream) -> TokenStream {
supertype::supertype(input)
}

#[proc_macro_attribute]
pub fn input(args: TokenStream, input: TokenStream) -> TokenStream {
input::input(args, input)
Expand Down
Loading