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

Coarse-grained tracked structs #657

Merged
merged 4 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
87 changes: 67 additions & 20 deletions components/salsa-macro-rules/src/setup_tracked_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,57 @@
#[macro_export]
macro_rules! setup_tracked_struct {
(
// Attributes on the function
// Attributes on the function.
attrs: [$(#[$attr:meta]),*],

// Visibility of the struct
// Visibility of the struct.
vis: $vis:vis,

// Name of the struct
// Name of the struct.
Struct: $Struct:ident,

// Name of the `'db` lifetime that the user gave
// Name of the `'db` lifetime that the user gave.
db_lt: $db_lt:lifetime,

// Name user gave for `new`
// Name user gave for `new`.
new_fn: $new_fn:ident,

// Field names
// Field names.
field_ids: [$($field_id:ident),*],

// Field names
field_getters: [$($field_getter_vis:vis $field_getter_id:ident),*],
// Tracked field names.
tracked_ids: [$($tracked_id:ident),*],

// Field types, may reference `db_lt`
// Visibility and names of tracked fields.
tracked_getters: [$($tracked_getter_vis:vis $tracked_getter_id:ident),*],

// Visibility and names of untracked fields.
untracked_getters: [$($untracked_getter_vis:vis $untracked_getter_id:ident),*],

// Field types, may reference `db_lt`.
field_tys: [$($field_ty:ty),*],

// Tracked field types.
tracked_tys: [$($tracked_ty:ty),*],

// Untracked field types.
untracked_tys: [$($untracked_ty:ty),*],

// Indices for each field from 0..N -- must be unsuffixed (e.g., `0`, `1`).
field_indices: [$($field_index:tt),*],

// Indices of fields to be used for id computations
id_field_indices: [$($id_field_index:tt),*],
// Absolute indices of any tracked fields, relative to all other fields of this struct.
absolute_tracked_indices: [$($absolute_tracked_index:tt),*],

// Indices of any tracked fields, relative to only tracked fields on this struct.
relative_tracked_indices: [$($relative_tracked_index:tt),*],

// Absolute indices of any untracked fields.
absolute_untracked_indices: [$($absolute_untracked_index:tt),*],

// A set of "field options". Each field option is a tuple `(maybe_clone, maybe_backdate)` where:
// A set of "field options" for each field.
//
// Each field option is a tuple `(maybe_clone, maybe_backdate)` where:
//
// * `maybe_clone` is either the identifier `clone` or `no_clone`
// * `maybe_backdate` is either the identifier `backdate` or `no_backdate`
Expand All @@ -41,7 +61,13 @@ macro_rules! setup_tracked_struct {
// (see e.g. @maybe_clone below).
field_options: [$($field_option:tt),*],

// Number of fields
// A set of "field options" for each tracked field.
tracked_options: [$($tracked_option:tt),*],

// A set of "field options" for each untracked field.
untracked_options: [$($untracked_option:tt),*],

// Number of fields.
num_fields: $N:literal,

// If true, generate a debug impl.
Expand Down Expand Up @@ -84,6 +110,10 @@ macro_rules! setup_tracked_struct {
$(stringify!($field_id),)*
];

const TRACKED_FIELD_INDICES: &'static [usize] = &[
$($absolute_tracked_index,)*
];

type Fields<$db_lt> = ($($field_ty,)*);

type Revisions = $zalsa::Array<$Revision, $N>;
Expand All @@ -98,8 +128,8 @@ macro_rules! setup_tracked_struct {
s.0
}

fn id_fields(fields: &Self::Fields<'_>) -> impl std::hash::Hash {
( $( &fields.$id_field_index ),* )
fn untracked_fields(fields: &Self::Fields<'_>) -> impl std::hash::Hash {
( $( &fields.$absolute_untracked_index ),* )
}

fn new_revisions(current_revision: $Revision) -> Self::Revisions {
Expand Down Expand Up @@ -133,6 +163,7 @@ macro_rules! setup_tracked_struct {
pub fn ingredient(db: &dyn $zalsa::Database) -> &$zalsa_struct::IngredientImpl<$Configuration> {
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())
})
Expand Down Expand Up @@ -199,17 +230,33 @@ macro_rules! setup_tracked_struct {
}

$(
$field_getter_vis fn $field_getter_id<$Db>(self, db: &$db_lt $Db) -> $crate::maybe_cloned_ty!($field_option, $db_lt, $field_ty)
$tracked_getter_vis fn $tracked_getter_id<$Db>(self, db: &$db_lt $Db) -> $crate::maybe_cloned_ty!($tracked_option, $db_lt, $tracked_ty)
where
// 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, $absolute_tracked_index, $relative_tracked_index);
$crate::maybe_clone!(
$tracked_option,
$tracked_ty,
&fields.$absolute_tracked_index,
)
}
)*

$(
$untracked_getter_vis fn $untracked_getter_id<$Db>(self, db: &$db_lt $Db) -> $crate::maybe_cloned_ty!($untracked_option, $db_lt, $untracked_ty)
where
// 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).field(db, self, $field_index);
let fields = $Configuration::ingredient(db).untracked_field(db, self);
$crate::maybe_clone!(
$field_option,
$field_ty,
&fields.$field_index,
$untracked_option,
$untracked_ty,
&fields.$absolute_untracked_index,
)
}
)*
Expand Down
2 changes: 1 addition & 1 deletion components/salsa-macros/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl crate::options::AllowedOptions for InputStruct {
impl SalsaStructAllowedOptions for InputStruct {
const KIND: &'static str = "input";

const ALLOW_ID: bool = false;
const ALLOW_TRACKED: bool = false;

const HAS_LIFETIME: bool = false;

Expand Down
2 changes: 1 addition & 1 deletion components/salsa-macros/src/interned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl crate::options::AllowedOptions for InternedStruct {
impl SalsaStructAllowedOptions for InternedStruct {
const KIND: &'static str = "interned";

const ALLOW_ID: bool = false;
const ALLOW_TRACKED: bool = false;

const HAS_LIFETIME: bool = true;

Expand Down
Loading