Skip to content

Commit 11767e1

Browse files
committed
Less zalsa calls
1 parent a8c07bd commit 11767e1

File tree

10 files changed

+42
-23
lines changed

10 files changed

+42
-23
lines changed

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

+5-5
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
}
@@ -217,7 +217,7 @@ macro_rules! setup_interned_struct {
217217
// FIXME(rust-lang/rust#65991): The `db` argument *should* have the type `dyn Database`
218218
$Db: ?Sized + $zalsa::Database,
219219
{
220-
let fields = $Configuration::ingredient(db).fields(db.as_dyn_database(), self);
220+
let fields = $Configuration::ingredient(db).data_zalsa(db.zalsa(), <$StructWithStatic as $zalsa::interned::Configuration>::deref_struct(self));
221221
$zalsa::maybe_clone!(
222222
$field_option,
223223
$field_ty,
@@ -229,7 +229,7 @@ macro_rules! setup_interned_struct {
229229
/// Default debug formatting for this struct (may be useful if you define your own `Debug` impl)
230230
pub fn default_debug_fmt(this: Self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
231231
$zalsa::with_attached_database(|db| {
232-
let fields = $Configuration::ingredient(db).fields(db.as_dyn_database(), this);
232+
let fields = $Configuration::ingredient(db).data_zalsa(db.zalsa(), <$StructWithStatic as $zalsa::interned::Configuration>::deref_struct(this));
233233
let mut f = f.debug_struct(stringify!($Struct));
234234
$(
235235
let f = f.field(stringify!($field_id), &fields.$field_index);

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,13 @@ macro_rules! setup_tracked_fn {
217217
$($cycle_recovery_fn)*(db, value, count, $($input_id),*)
218218
}
219219

220-
fn id_to_input<$db_lt>(db: &$db_lt Self::DbView, key: salsa::Id) -> Self::Input<$db_lt> {
220+
#[inline]
221+
fn id_to_input<$db_lt>(db: &$db_lt Self::DbView, zalsa: &$db_lt $zalsa::Zalsa, key: salsa::Id) -> Self::Input<$db_lt> {
221222
$zalsa::macro_if! {
222223
if $needs_interner {
223-
$Configuration::intern_ingredient(db).data(db.as_dyn_database(), key).clone()
224+
$Configuration::intern_ingredient(db).data_zalsa(zalsa, key).clone()
224225
} else {
225-
$zalsa::FromIdWithDb::from_id(key, db)
226+
$zalsa::FromIdWithDb::from_id_zalsa(key, zalsa)
226227
}
227228
}
228229
}

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

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

tests/interned-structs_self_ref.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -164,20 +164,20 @@ const _: () = {
164164
where
165165
Db_: ?Sized + zalsa_::Database,
166166
{
167-
let fields = Configuration_::ingredient(db).fields(db.as_dyn_database(), self);
167+
let fields = Configuration_::ingredient(db).data_zalsa(db.zalsa(), self.0);
168168
std::clone::Clone::clone((&fields.0))
169169
}
170170
fn other<Db_>(self, db: &'db Db_) -> InternedString<'db>
171171
where
172172
Db_: ?Sized + zalsa_::Database,
173173
{
174-
let fields = Configuration_::ingredient(db).fields(db.as_dyn_database(), self);
174+
let fields = Configuration_::ingredient(db).data_zalsa(db.zalsa(), self.0);
175175
std::clone::Clone::clone((&fields.1))
176176
}
177177
#[doc = r" Default debug formatting for this struct (may be useful if you define your own `Debug` impl)"]
178178
pub fn default_debug_fmt(this: Self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
179179
zalsa_::with_attached_database(|db| {
180-
let fields = Configuration_::ingredient(db).fields(db.as_dyn_database(), this);
180+
let fields = Configuration_::ingredient(db).data_zalsa(db.zalsa(), this.0);
181181
let mut f = f.debug_struct("InternedString");
182182
let f = f.field("data", &fields.0);
183183
let f = f.field("other", &fields.1);

0 commit comments

Comments
 (0)