Skip to content

Remove some unnecessary dynamic dispatch calls #802

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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
10 changes: 5 additions & 5 deletions components/salsa-macro-rules/src/setup_interned_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,17 @@ macro_rules! setup_interned_struct {
}
}

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

fn deref_struct(s: Self::Struct<'_>) -> salsa::Id {
use salsa::plumbing::AsId;
use $zalsa::AsId;
s.0.as_id()
}
}
Expand Down Expand Up @@ -218,7 +218,7 @@ macro_rules! setup_interned_struct {
// FIXME(rust-lang/rust#65991): The `db` argument *should* have the type `dyn Database`
$Db: ?Sized + $zalsa::Database,
{
let fields = $Configuration::ingredient(db).fields(db.as_dyn_database(), self);
let fields = $Configuration::ingredient(db).data_zalsa(db.zalsa(), <$StructWithStatic as $zalsa::interned::Configuration>::deref_struct(self));
$zalsa::maybe_clone!(
$field_option,
$field_ty,
Expand All @@ -230,7 +230,7 @@ macro_rules! setup_interned_struct {
/// Default debug formatting for this struct (may be useful if you define your own `Debug` impl)
pub fn default_debug_fmt(this: Self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
$zalsa::with_attached_database(|db| {
let fields = $Configuration::ingredient(db).fields(db.as_dyn_database(), this);
let fields = $Configuration::ingredient(db).data_zalsa(db.zalsa(), <$StructWithStatic as $zalsa::interned::Configuration>::deref_struct(this));
let mut f = f.debug_struct(stringify!($Struct));
$(
let f = f.field(stringify!($field_id), &fields.$field_index);
Expand Down
18 changes: 8 additions & 10 deletions components/salsa-macro-rules/src/setup_tracked_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ macro_rules! setup_tracked_fn {
$Configuration:ident,
$InternedData:ident,
$FN_CACHE:ident,
$INTERN_CACHE:ident,
$inner:ident,
]
) => {
Expand Down Expand Up @@ -100,9 +99,6 @@ macro_rules! setup_tracked_fn {
std::marker::PhantomData<&$db_lt $zalsa::interned::Value<$Configuration>>,
);

static $INTERN_CACHE: $zalsa::IngredientCache<$zalsa::interned::IngredientImpl<$Configuration>> =
$zalsa::IngredientCache::new();

impl $zalsa::SalsaStructInDb for $InternedData<'_> {
type MemoIngredientMap = $zalsa::MemoIngredientSingletonIndex;

Expand Down Expand Up @@ -164,10 +160,11 @@ macro_rules! setup_tracked_fn {
db: &dyn $Db,
) -> &$zalsa::interned::IngredientImpl<$Configuration> {
let zalsa = db.zalsa();
$INTERN_CACHE.get_or_create(zalsa, || {
let index = $FN_CACHE.get_or_create_index(zalsa, || {
<dyn $Db as $Db>::zalsa_register_downcaster(db);
zalsa.add_or_lookup_jar_by_type::<$Configuration>().successor(0)
})
db.zalsa().add_or_lookup_jar_by_type::<$Configuration>()
}).successor(0);
zalsa.lookup_ingredient(index).assert_type()
}
}
}
Expand Down Expand Up @@ -219,12 +216,13 @@ macro_rules! setup_tracked_fn {
$($cycle_recovery_fn)*(db, value, count, $($input_id),*)
}

fn id_to_input<$db_lt>(db: &$db_lt Self::DbView, key: salsa::Id) -> Self::Input<$db_lt> {
#[inline]
fn id_to_input<$db_lt>(db: &$db_lt Self::DbView, zalsa: &$db_lt $zalsa::Zalsa, key: salsa::Id) -> Self::Input<$db_lt> {
$zalsa::macro_if! {
if $needs_interner {
$Configuration::intern_ingredient(db).data(db.as_dyn_database(), key).clone()
$Configuration::intern_ingredient(db).data_zalsa(zalsa, key).clone()
} else {
$zalsa::FromIdWithDb::from_id(key, db)
$zalsa::FromIdWithDb::from_id_zalsa(key, zalsa)
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions components/salsa-macros/src/supertype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,12 @@ fn enum_impl(enum_item: syn::ItemEnum) -> syn::Result<TokenStream> {
#[inline]
fn from_id(__id: zalsa::Id, __db: &(impl ?Sized + zalsa::Database)) -> Self {
let __zalsa = __db.zalsa();
let __type_id = __zalsa.lookup_page_type_id(__id);
<Self as zalsa::SalsaStructInDb>::cast(__id, __type_id).expect("invalid enum variant")
Self::from_id_zalsa( __id,__zalsa)
}
#[inline]
fn from_id_zalsa(id: zalsa::Id, zalsa: &zalsa::Zalsa) -> Self {
let type_id = zalsa.lookup_page_type_id(id);
<Self as zalsa::SalsaStructInDb>::cast(id, type_id).expect("invalid enum variant")
}
}
};
Expand Down
2 changes: 0 additions & 2 deletions components/salsa-macros/src/tracked_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ impl Macro {
let Configuration = self.hygiene.ident("Configuration");
let InternedData = self.hygiene.ident("InternedData");
let FN_CACHE = self.hygiene.ident("FN_CACHE");
let INTERN_CACHE = self.hygiene.ident("INTERN_CACHE");
let inner = &inner_fn.sig.ident;

let function_type = function_type(&item);
Expand Down Expand Up @@ -175,7 +174,6 @@ impl Macro {
#Configuration,
#InternedData,
#FN_CACHE,
#INTERN_CACHE,
#inner,
]
}],
Expand Down
2 changes: 1 addition & 1 deletion src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub trait Configuration: Any {

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

/// Invoked when we need to compute the value for the given key, either because we've never
/// computed it before or because the old one relied on inputs that have changed.
Expand Down
16 changes: 9 additions & 7 deletions src/function/diff_outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ where
key: DatabaseKeyIndex,
old_memo: &Memo<C::Output<'_>>,
revisions: &mut QueryRevisions,
provisional: bool,
) {
// Iterate over the outputs of the `old_memo` and put them into a hashset
let mut old_outputs: FxHashSet<_> = old_memo.revisions.origin.outputs().collect();
Expand All @@ -35,14 +34,17 @@ where
old_outputs.remove(&new_output);
}

if !old_outputs.is_empty() {
// Remove the outputs that are no longer present in the current revision
// to prevent that the next revision is seeded with a id mapping that no longer exists.
revisions.tracked_struct_ids.retain(|&k, &mut value| {
!old_outputs.contains(&DatabaseKeyIndex::new(k.ingredient_index(), value))
});
if old_outputs.is_empty() {
return;
}

// Remove the outputs that are no longer present in the current revision
// to prevent that the next revision is seeded with a id mapping that no longer exists.
revisions.tracked_struct_ids.retain(|&k, &mut value| {
!old_outputs.contains(&DatabaseKeyIndex::new(k.ingredient_index(), value))
});

let provisional = !revisions.cycle_heads.is_empty();
for old_output in old_outputs {
Self::report_stale_output(zalsa, db, key, old_output, provisional);
}
Expand Down
Loading