Skip to content

Commit 76ac72d

Browse files
committed
Less zalsa calls
1 parent a8c07bd commit 76ac72d

15 files changed

+95
-61
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ macro_rules! setup_accumulator_impl {
2323
$zalsa::IngredientCache::new();
2424

2525
fn $ingredient(db: &dyn $zalsa::Database) -> &$zalsa_struct::IngredientImpl<$Struct> {
26-
$CACHE.get_or_create(db, || {
27-
db.zalsa()
28-
.add_or_lookup_jar_by_type::<$zalsa_struct::JarImpl<$Struct>>()
26+
let zalsa = db.zalsa();
27+
$CACHE.get_or_create(zalsa, || {
28+
zalsa.add_or_lookup_jar_by_type::<$zalsa_struct::JarImpl<$Struct>>()
2929
})
3030
}
3131

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@ macro_rules! setup_input_struct {
8888
pub fn ingredient(db: &dyn $zalsa::Database) -> &$zalsa_struct::IngredientImpl<Self> {
8989
static CACHE: $zalsa::IngredientCache<$zalsa_struct::IngredientImpl<$Configuration>> =
9090
$zalsa::IngredientCache::new();
91-
CACHE.get_or_create(db, || {
92-
db.zalsa().add_or_lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>()
91+
let zalsa = db.zalsa();
92+
CACHE.get_or_create(zalsa, || {
93+
zalsa.add_or_lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>()
9394
})
9495
}
9596

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

+8-7
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,17 @@ macro_rules! setup_interned_struct {
118118
}
119119
}
120120

121-
impl salsa::plumbing::interned::Configuration for $StructWithStatic {
121+
impl $zalsa::interned::Configuration for $StructWithStatic {
122122
const DEBUG_NAME: &'static str = stringify!($Struct);
123123
type Fields<'a> = $StructDataIdent<'a>;
124124
type Struct<'db> = $Struct< $($db_lt_arg)? >;
125125
fn struct_from_id<'db>(id: salsa::Id) -> Self::Struct<'db> {
126-
use salsa::plumbing::FromId;
126+
use $zalsa::FromId;
127127
$Struct(<$Id>::from_id(id), std::marker::PhantomData)
128128
}
129129

130130
fn deref_struct(s: Self::Struct<'_>) -> salsa::Id {
131-
use salsa::plumbing::AsId;
131+
use $zalsa::AsId;
132132
s.0.as_id()
133133
}
134134
}
@@ -140,8 +140,9 @@ macro_rules! setup_interned_struct {
140140
{
141141
static CACHE: $zalsa::IngredientCache<$zalsa_struct::IngredientImpl<$Configuration>> =
142142
$zalsa::IngredientCache::new();
143-
CACHE.get_or_create(db.as_dyn_database(), || {
144-
db.zalsa().add_or_lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>()
143+
let zalsa = db.zalsa();
144+
CACHE.get_or_create(zalsa, || {
145+
zalsa.add_or_lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>()
145146
})
146147
}
147148
}
@@ -217,7 +218,7 @@ macro_rules! setup_interned_struct {
217218
// FIXME(rust-lang/rust#65991): The `db` argument *should* have the type `dyn Database`
218219
$Db: ?Sized + $zalsa::Database,
219220
{
220-
let fields = $Configuration::ingredient(db).fields(db.as_dyn_database(), self);
221+
let fields = $Configuration::ingredient(db).data_zalsa(db.zalsa(), <$StructWithStatic as $zalsa::interned::Configuration>::deref_struct(self));
221222
$zalsa::maybe_clone!(
222223
$field_option,
223224
$field_ty,
@@ -229,7 +230,7 @@ macro_rules! setup_interned_struct {
229230
/// Default debug formatting for this struct (may be useful if you define your own `Debug` impl)
230231
pub fn default_debug_fmt(this: Self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
231232
$zalsa::with_attached_database(|db| {
232-
let fields = $Configuration::ingredient(db).fields(db.as_dyn_database(), this);
233+
let fields = $Configuration::ingredient(db).data_zalsa(db.zalsa(), <$StructWithStatic as $zalsa::interned::Configuration>::deref_struct(this));
233234
let mut f = f.debug_struct(stringify!($Struct));
234235
$(
235236
let f = f.field(stringify!($field_id), &fields.$field_index);

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

+12-12
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ macro_rules! setup_tracked_fn {
6868
$Configuration:ident,
6969
$InternedData:ident,
7070
$FN_CACHE:ident,
71-
$INTERN_CACHE:ident,
7271
$inner:ident,
7372
]
7473
) => {
@@ -100,9 +99,6 @@ macro_rules! setup_tracked_fn {
10099
std::marker::PhantomData<&$db_lt $zalsa::interned::Value<$Configuration>>,
101100
);
102101

103-
static $INTERN_CACHE: $zalsa::IngredientCache<$zalsa::interned::IngredientImpl<$Configuration>> =
104-
$zalsa::IngredientCache::new();
105-
106102
impl $zalsa::SalsaStructInDb for $InternedData<'_> {
107103
type MemoIngredientMap = $zalsa::MemoIngredientSingletonIndex;
108104

@@ -144,9 +140,10 @@ macro_rules! setup_tracked_fn {
144140

145141
impl $Configuration {
146142
fn fn_ingredient(db: &dyn $Db) -> &$zalsa::function::IngredientImpl<$Configuration> {
147-
$FN_CACHE.get_or_create(db.as_dyn_database(), || {
143+
let zalsa = db.zalsa();
144+
$FN_CACHE.get_or_create(zalsa, || {
148145
<dyn $Db as $Db>::zalsa_register_downcaster(db);
149-
db.zalsa().add_or_lookup_jar_by_type::<$Configuration>()
146+
zalsa.add_or_lookup_jar_by_type::<$Configuration>()
150147
})
151148
}
152149

@@ -162,10 +159,12 @@ macro_rules! setup_tracked_fn {
162159
fn intern_ingredient(
163160
db: &dyn $Db,
164161
) -> &$zalsa::interned::IngredientImpl<$Configuration> {
165-
$INTERN_CACHE.get_or_create(db.as_dyn_database(), || {
162+
let zalsa = db.zalsa();
163+
let index = $FN_CACHE.get_or_create_index(zalsa, || {
166164
<dyn $Db as $Db>::zalsa_register_downcaster(db);
167-
db.zalsa().add_or_lookup_jar_by_type::<$Configuration>().successor(0)
168-
})
165+
db.zalsa().add_or_lookup_jar_by_type::<$Configuration>()
166+
}).successor(0);
167+
zalsa.lookup_ingredient(index).assert_type()
169168
}
170169
}
171170
}
@@ -217,12 +216,13 @@ macro_rules! setup_tracked_fn {
217216
$($cycle_recovery_fn)*(db, value, count, $($input_id),*)
218217
}
219218

220-
fn id_to_input<$db_lt>(db: &$db_lt Self::DbView, key: salsa::Id) -> Self::Input<$db_lt> {
219+
#[inline]
220+
fn id_to_input<$db_lt>(db: &$db_lt Self::DbView, zalsa: &$db_lt $zalsa::Zalsa, key: salsa::Id) -> Self::Input<$db_lt> {
221221
$zalsa::macro_if! {
222222
if $needs_interner {
223-
$Configuration::intern_ingredient(db).data(db.as_dyn_database(), key).clone()
223+
$Configuration::intern_ingredient(db).data_zalsa(zalsa, key).clone()
224224
} else {
225-
$zalsa::FromIdWithDb::from_id(key, db)
225+
$zalsa::FromIdWithDb::from_id_zalsa(key, zalsa)
226226
}
227227
}
228228
}

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,9 @@ macro_rules! setup_tracked_struct {
179179
static CACHE: $zalsa::IngredientCache<$zalsa_struct::IngredientImpl<$Configuration>> =
180180
$zalsa::IngredientCache::new();
181181

182-
CACHE.get_or_create(db, || {
183-
db.zalsa().add_or_lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>()
182+
let zalsa = db.zalsa();
183+
CACHE.get_or_create(zalsa, || {
184+
zalsa.add_or_lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>()
184185
})
185186
}
186187
}

components/salsa-macros/src/supertype.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,12 @@ fn enum_impl(enum_item: syn::ItemEnum) -> syn::Result<TokenStream> {
6161
#[inline]
6262
fn from_id(__id: zalsa::Id, __db: &(impl ?Sized + zalsa::Database)) -> Self {
6363
let __zalsa = __db.zalsa();
64-
let __type_id = __zalsa.lookup_page_type_id(__id);
65-
<Self as zalsa::SalsaStructInDb>::cast(__id, __type_id).expect("invalid enum variant")
64+
Self::from_id_zalsa( __id,__zalsa)
65+
}
66+
#[inline]
67+
fn from_id_zalsa(id: zalsa::Id, zalsa: &zalsa::Zalsa) -> Self {
68+
let type_id = zalsa.lookup_page_type_id(id);
69+
<Self as zalsa::SalsaStructInDb>::cast(id, type_id).expect("invalid enum variant")
6670
}
6771
}
6872
};

components/salsa-macros/src/tracked_fn.rs

-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ impl Macro {
100100
let Configuration = self.hygiene.ident("Configuration");
101101
let InternedData = self.hygiene.ident("InternedData");
102102
let FN_CACHE = self.hygiene.ident("FN_CACHE");
103-
let INTERN_CACHE = self.hygiene.ident("INTERN_CACHE");
104103
let inner = &inner_fn.sig.ident;
105104

106105
let function_type = function_type(&item);
@@ -175,7 +174,6 @@ impl Macro {
175174
#Configuration,
176175
#InternedData,
177176
#FN_CACHE,
178-
#INTERN_CACHE,
179177
#inner,
180178
]
181179
}],

src/function.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub trait Configuration: Any {
6060

6161
/// Convert from the id used internally to the value that execute is expecting.
6262
/// This is a no-op if the input to the function is a salsa struct.
63-
fn id_to_input(db: &Self::DbView, key: Id) -> Self::Input<'_>;
63+
fn id_to_input<'db>(db: &'db Self::DbView, zalsa: &'db Zalsa, key: Id) -> Self::Input<'db>;
6464

6565
/// Invoked when we need to compute the value for the given key, either because we've never
6666
/// computed it before or because the old one relied on inputs that have changed.

src/function/execute.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ where
5757

5858
// Query was not previously executed, or value is potentially
5959
// stale, or value is absent. Let's execute!
60-
let mut new_value = C::execute(db, C::id_to_input(db, id));
60+
let mut new_value = C::execute(db, C::id_to_input(db, zalsa, id));
6161
let mut revisions = active_query.pop();
6262

6363
// Did the new result we got depend on our own provisional value, in a cycle?
@@ -105,7 +105,7 @@ where
105105
db,
106106
&new_value,
107107
iteration_count,
108-
C::id_to_input(db, id),
108+
C::id_to_input(db, zalsa, id),
109109
) {
110110
crate::CycleRecoveryAction::Iterate => {
111111
tracing::debug!(
@@ -169,7 +169,7 @@ where
169169

170170
// Query was not previously executed, or value is potentially
171171
// stale, or value is absent. Let's execute!
172-
let new_value = C::execute(db, C::id_to_input(db, id));
172+
let new_value = C::execute(db, C::id_to_input(db, zalsa, id));
173173
let revisions = active_query.pop();
174174

175175
(new_value, revisions)

src/function/memo.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::function::{Configuration, IngredientImpl};
1010
use crate::key::DatabaseKeyIndex;
1111
use crate::revision::AtomicRevision;
1212
use crate::table::memo::MemoTable;
13-
use crate::zalsa::{MemoIngredientIndex, Zalsa};
13+
use crate::zalsa::{MemoIngredientIndex, Zalsa, ZalsaDatabase};
1414
use crate::zalsa_local::{QueryOrigin, QueryRevisions};
1515
use crate::{Event, EventKind, Id, Revision};
1616

@@ -113,7 +113,9 @@ impl<C: Configuration> IngredientImpl<C> {
113113
key: Id,
114114
) -> Option<C::Output<'db>> {
115115
match C::CYCLE_STRATEGY {
116-
CycleRecoveryStrategy::Fixpoint => Some(C::cycle_initial(db, C::id_to_input(db, key))),
116+
CycleRecoveryStrategy::Fixpoint => {
117+
Some(C::cycle_initial(db, C::id_to_input(db, db.zalsa(), key)))
118+
}
117119
CycleRecoveryStrategy::Panic => None,
118120
}
119121
}

src/id.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,14 @@ pub trait FromId: AsId + Copy + Eq + Hash {
7272
}
7373

7474
impl AsId for Id {
75+
#[inline]
7576
fn as_id(&self) -> Id {
7677
*self
7778
}
7879
}
7980

8081
impl FromId for Id {
82+
#[inline]
8183
fn from_id(id: Id) -> Self {
8284
id
8385
}
@@ -87,11 +89,17 @@ impl FromId for Id {
8789
/// so they use this trait instead, that has a blanket implementation for `FromId`.
8890
pub trait FromIdWithDb: AsId + Copy + Eq + Hash {
8991
fn from_id(id: Id, db: &(impl ?Sized + Database)) -> Self;
92+
#[doc(hidden)]
93+
fn from_id_zalsa(id: Id, zalsa: &crate::zalsa::Zalsa) -> Self;
9094
}
9195

9296
impl<T: FromId> FromIdWithDb for T {
9397
#[inline]
94-
fn from_id(id: Id, _db: &(impl ?Sized + Database)) -> Self {
98+
fn from_id(id: Id, _: &(impl ?Sized + Database)) -> Self {
99+
FromId::from_id(id)
100+
}
101+
#[inline]
102+
fn from_id_zalsa(id: Id, _: &crate::zalsa::Zalsa) -> Self {
95103
FromId::from_id(id)
96104
}
97105
}

src/input.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ impl<C: Configuration> IngredientImpl<C> {
108108
})
109109
});
110110

111-
FromIdWithDb::from_id(id, db)
111+
FromIdWithDb::from_id_zalsa(id, zalsa)
112112
}
113113

114114
/// Change the value of the field `field_index` to a new value.

src/interned.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,11 @@ where
335335
/// Rarely used since end-users generally carry a struct with a pointer directly
336336
/// to the interned item.
337337
pub fn data<'db>(&'db self, db: &'db dyn Database, id: Id) -> &'db C::Fields<'db> {
338-
let zalsa = db.zalsa();
338+
self.data_zalsa(db.zalsa(), id)
339+
}
340+
341+
#[doc(hidden)]
342+
pub fn data_zalsa<'db>(&'db self, zalsa: &'db Zalsa, id: Id) -> &'db C::Fields<'db> {
339343
let internal_data = zalsa.table().get::<Value<C>>(id);
340344
let last_changed_revision = zalsa.last_changed_revision(internal_data.durability());
341345

@@ -350,7 +354,7 @@ where
350354
/// Lookup the fields from an interned struct.
351355
/// Note that this is not "leaking" since no dependency edge is required.
352356
pub fn fields<'db>(&'db self, db: &'db dyn Database, s: C::Struct<'db>) -> &'db C::Fields<'db> {
353-
self.data(db, C::deref_struct(s))
357+
self.data_zalsa(db.zalsa(), C::deref_struct(s))
354358
}
355359

356360
pub fn reset(&mut self, db: &mut dyn Database) {

0 commit comments

Comments
 (0)