Skip to content

Commit a8858a2

Browse files
authored
Merge pull request #709 from MichaReiser/avoid-tracking-revisions-for-untracked-fields
Track revisions for tracked-fields only
2 parents 687251f + e1884ba commit a8858a2

File tree

4 files changed

+33
-25
lines changed

4 files changed

+33
-25
lines changed

components/salsa-macro-rules/src/setup_tracked_struct.rs

+14-9
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ macro_rules! setup_tracked_struct {
5050
// Absolute indices of any untracked fields.
5151
absolute_untracked_indices: [$($absolute_untracked_index:tt),*],
5252

53-
// A set of "field options" for each field.
53+
// A set of "field options" for each tracked field.
5454
//
5555
// Each field option is a tuple `(maybe_clone, maybe_backdate)` where:
5656
//
@@ -59,16 +59,21 @@ macro_rules! setup_tracked_struct {
5959
//
6060
// These are used to drive conditional logic for each field via recursive macro invocation
6161
// (see e.g. @maybe_clone below).
62-
field_options: [$($field_option:tt),*],
63-
64-
// A set of "field options" for each tracked field.
6562
tracked_options: [$($tracked_option:tt),*],
6663

6764
// A set of "field options" for each untracked field.
65+
//
66+
// Each field option is a tuple `(maybe_clone, maybe_backdate)` where:
67+
//
68+
// * `maybe_clone` is either the identifier `clone` or `no_clone`
69+
// * `maybe_backdate` is either the identifier `backdate` or `no_backdate`
70+
//
71+
// These are used to drive conditional logic for each field via recursive macro invocation
72+
// (see e.g. @maybe_clone below).
6873
untracked_options: [$($untracked_option:tt),*],
6974

70-
// Number of fields.
71-
num_fields: $N:literal,
75+
// Number of tracked fields.
76+
num_tracked_fields: $N:literal,
7277

7378
// If true, generate a debug impl.
7479
generate_debug_impl: $generate_debug_impl:tt,
@@ -111,7 +116,7 @@ macro_rules! setup_tracked_struct {
111116
];
112117

113118
const TRACKED_FIELD_INDICES: &'static [usize] = &[
114-
$($absolute_tracked_index,)*
119+
$($relative_tracked_index,)*
115120
];
116121

117122
type Fields<$db_lt> = ($($field_ty,)*);
@@ -150,7 +155,7 @@ macro_rules! setup_tracked_struct {
150155
$tracked_ty,
151156
(*old_fields).$absolute_tracked_index,
152157
new_fields.$absolute_tracked_index,
153-
revisions[$absolute_tracked_index],
158+
revisions[$relative_tracked_index],
154159
current_revision,
155160
$zalsa,
156161
);
@@ -246,7 +251,7 @@ macro_rules! setup_tracked_struct {
246251
$Db: ?Sized + $zalsa::Database,
247252
{
248253
let db = db.as_dyn_database();
249-
let fields = $Configuration::ingredient(db).tracked_field(db, self, $absolute_tracked_index, $relative_tracked_index);
254+
let fields = $Configuration::ingredient(db).tracked_field(db, self, $relative_tracked_index);
250255
$crate::maybe_clone!(
251256
$tracked_option,
252257
$tracked_ty,

components/salsa-macros/src/salsa_struct.rs

+4
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@ where
227227
Literal::usize_unsuffixed(self.fields.len())
228228
}
229229

230+
pub(crate) fn num_tracked_fields(&self) -> Literal {
231+
Literal::usize_unsuffixed(self.tracked_fields_iter().count())
232+
}
233+
230234
pub(crate) fn required_fields(&self) -> Vec<TokenStream> {
231235
self.fields
232236
.iter()

components/salsa-macros/src/tracked_struct.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,14 @@ impl Macro {
101101

102102
let absolute_untracked_indices = salsa_struct.untracked_field_indices();
103103

104-
let field_options = salsa_struct.field_options();
105104
let tracked_options = salsa_struct.tracked_options();
106105
let untracked_options = salsa_struct.untracked_options();
107106

108107
let field_tys = salsa_struct.field_tys();
109108
let tracked_tys = salsa_struct.tracked_tys();
110109
let untracked_tys = salsa_struct.untracked_tys();
111110

112-
let num_fields = salsa_struct.num_fields();
111+
let num_tracked_fields = salsa_struct.num_tracked_fields();
113112
let generate_debug_impl = salsa_struct.generate_debug_impl();
114113

115114
let zalsa = self.hygiene.ident("zalsa");
@@ -147,11 +146,10 @@ impl Macro {
147146

148147
absolute_untracked_indices: [#(#absolute_untracked_indices),*],
149148

150-
field_options: [#(#field_options),*],
151149
tracked_options: [#(#tracked_options),*],
152150
untracked_options: [#(#untracked_options),*],
153151

154-
num_fields: #num_fields,
152+
num_tracked_fields: #num_tracked_fields,
155153
generate_debug_impl: #generate_debug_impl,
156154
unused_names: [
157155
#zalsa,

src/tracked_struct.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ pub trait Configuration: Sized + 'static {
3131
/// The debug names of any fields.
3232
const FIELD_DEBUG_NAMES: &'static [&'static str];
3333

34-
/// The absolute indices of any tracked fields.
34+
/// The relative indices of any tracked fields.
3535
const TRACKED_FIELD_INDICES: &'static [usize];
3636

3737
/// A (possibly empty) tuple of the fields for this struct.
3838
type Fields<'db>: Send + Sync;
3939

40-
/// A array of [`Revision`][] values, one per each of the value fields.
40+
/// A array of [`Revision`][] values, one per each of the tracked value fields.
4141
/// When a struct is re-recreated in a new revision, the corresponding
4242
/// entries for each field are updated to the new revision if their
4343
/// values have changed (or if the field is marked as `#[no_eq]`).
@@ -117,14 +117,16 @@ impl<C: Configuration> Jar for JarImpl<C> {
117117
) -> Vec<Box<dyn Ingredient>> {
118118
let struct_ingredient = <IngredientImpl<C>>::new(struct_index);
119119

120-
let tracked_field_ingredients = C::TRACKED_FIELD_INDICES.iter().enumerate().map(
121-
|(relative_tracked_index, &field_index)| {
122-
Box::new(<FieldIngredientImpl<C>>::new(
123-
field_index,
124-
struct_index.successor(relative_tracked_index),
125-
)) as _
126-
},
127-
);
120+
let tracked_field_ingredients =
121+
C::TRACKED_FIELD_INDICES
122+
.iter()
123+
.copied()
124+
.map(|relative_tracked_index| {
125+
Box::new(<FieldIngredientImpl<C>>::new(
126+
relative_tracked_index,
127+
struct_index.successor(relative_tracked_index),
128+
)) as _
129+
});
128130

129131
std::iter::once(Box::new(struct_ingredient) as _)
130132
.chain(tracked_field_ingredients)
@@ -663,7 +665,6 @@ where
663665
&'db self,
664666
db: &'db dyn crate::Database,
665667
s: C::Struct<'db>,
666-
field_index: usize,
667668
relative_tracked_index: usize,
668669
) -> &'db C::Fields<'db> {
669670
let (zalsa, zalsa_local) = db.zalsas();
@@ -673,7 +674,7 @@ where
673674

674675
data.read_lock(zalsa.current_revision());
675676

676-
let field_changed_at = data.revisions[field_index];
677+
let field_changed_at = data.revisions[relative_tracked_index];
677678

678679
zalsa_local.report_tracked_read(
679680
InputDependencyIndex::new(field_ingredient_index, id),

0 commit comments

Comments
 (0)