Skip to content

Replace ingredient cache with faster ingredient map #921

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ hashbrown = "0.15"
hashlink = "0.10"
indexmap = "2"
intrusive-collections = "0.9.7"
papaya = "0.2.1"
parking_lot = "0.12"
portable-atomic = "1"
rustc-hash = "2"
Expand Down Expand Up @@ -51,7 +52,7 @@ salsa-macros = { version = "=0.22.0", path = "components/salsa-macros" }
[dev-dependencies]
# examples
crossbeam-channel = "0.5.14"
dashmap = { version = "6", features = ["raw-api"] }
dashmap = "6"
eyre = "0.6.8"
notify-debouncer-mini = "0.4.1"
ordered-float = "4.2.1"
Expand Down
8 changes: 2 additions & 6 deletions components/salsa-macro-rules/src/setup_accumulator_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,8 @@ macro_rules! setup_accumulator_impl {
use salsa::plumbing::accumulator as $zalsa_struct;

fn $ingredient(zalsa: &$zalsa::Zalsa) -> &$zalsa_struct::IngredientImpl<$Struct> {
static $CACHE: $zalsa::IngredientCache<$zalsa_struct::IngredientImpl<$Struct>> =
$zalsa::IngredientCache::new();

$CACHE.get_or_create(zalsa, || {
zalsa.add_or_lookup_jar_by_type::<$zalsa_struct::JarImpl<$Struct>>()
})
let index = zalsa.add_or_lookup_jar_by_type::<$zalsa_struct::JarImpl<$Struct>>();
zalsa.lookup_ingredient(index).assert_type()
}

impl $zalsa::Accumulator for $Struct {
Expand Down
8 changes: 2 additions & 6 deletions components/salsa-macro-rules/src/setup_input_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,8 @@ macro_rules! setup_input_struct {
}

fn ingredient_(zalsa: &$zalsa::Zalsa) -> &$zalsa_struct::IngredientImpl<Self> {
static CACHE: $zalsa::IngredientCache<$zalsa_struct::IngredientImpl<$Configuration>> =
$zalsa::IngredientCache::new();

CACHE.get_or_create(zalsa, || {
zalsa.add_or_lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>()
})
let index = zalsa.add_or_lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>();
zalsa.lookup_ingredient(index).assert_type()
}

pub fn ingredient_mut(db: &mut dyn $zalsa::Database) -> (&mut $zalsa_struct::IngredientImpl<Self>, &mut $zalsa::Runtime) {
Expand Down
8 changes: 2 additions & 6 deletions components/salsa-macro-rules/src/setup_interned_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,9 @@ macro_rules! setup_interned_struct {
where
Db: ?Sized + $zalsa::Database,
{
static CACHE: $zalsa::IngredientCache<$zalsa_struct::IngredientImpl<$Configuration>> =
$zalsa::IngredientCache::new();

let zalsa = db.zalsa();
CACHE.get_or_create(zalsa, || {
zalsa.add_or_lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>()
})
let index = zalsa.add_or_lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>();
zalsa.lookup_ingredient(index).assert_type()
}
}

Expand Down
28 changes: 13 additions & 15 deletions components/salsa-macro-rules/src/setup_tracked_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,6 @@ macro_rules! setup_tracked_fn {

struct $Configuration;

static $FN_CACHE: $zalsa::IngredientCache<$zalsa::function::IngredientImpl<$Configuration>> =
$zalsa::IngredientCache::new();

$zalsa::macro_if! {
if $needs_interner {
#[derive(Copy, Clone)]
Expand All @@ -97,9 +94,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 @@ -150,10 +144,15 @@ macro_rules! setup_tracked_fn {
impl $Configuration {
fn fn_ingredient(db: &dyn $Db) -> &$zalsa::function::IngredientImpl<$Configuration> {
let zalsa = db.zalsa();
$FN_CACHE.get_or_create(zalsa, || {
<dyn $Db as $Db>::zalsa_register_downcaster(db);
zalsa.add_or_lookup_jar_by_type::<$Configuration>()
})
<dyn $Db as $Db>::zalsa_register_downcaster(db);
let index = zalsa.add_or_lookup_jar_by_type::<$Configuration>();
zalsa.lookup_ingredient(index).assert_type()
}

fn fn_ingredient_no_register(db: &dyn $Db) -> &$zalsa::function::IngredientImpl<$Configuration> {
let zalsa = db.zalsa();
let index = zalsa.add_or_lookup_jar_by_type::<$Configuration>();
zalsa.lookup_ingredient(index).assert_type()
}

pub fn fn_ingredient_mut(db: &mut dyn $Db) -> &mut $zalsa::function::IngredientImpl<Self> {
Expand All @@ -169,10 +168,9 @@ macro_rules! setup_tracked_fn {
db: &dyn $Db,
) -> &$zalsa::interned::IngredientImpl<$Configuration> {
let zalsa = db.zalsa();
$INTERN_CACHE.get_or_create(zalsa, || {
<dyn $Db as $Db>::zalsa_register_downcaster(db);
zalsa.add_or_lookup_jar_by_type::<$Configuration>().successor(0)
})
<dyn $Db as $Db>::zalsa_register_downcaster(db);
let index = zalsa.add_or_lookup_jar_by_type::<$Configuration>().successor(0);
zalsa.lookup_ingredient(index).assert_type()
}
}
}
Expand Down Expand Up @@ -356,7 +354,7 @@ macro_rules! setup_tracked_fn {
if $needs_interner {
{
let key = $Configuration::intern_ingredient($db).intern_id($db.as_dyn_database(), ($($input_id),*), |_, data| data);
$Configuration::fn_ingredient($db).fetch($db, key)
$Configuration::fn_ingredient_no_register($db).fetch($db, key)
}
} else {
$Configuration::fn_ingredient($db).fetch($db, $zalsa::AsId::as_id(&($($input_id),*)))
Expand Down
8 changes: 2 additions & 6 deletions components/salsa-macro-rules/src/setup_tracked_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,8 @@ macro_rules! setup_tracked_struct {
}

fn ingredient_(zalsa: &$zalsa::Zalsa) -> &$zalsa_struct::IngredientImpl<Self> {
static CACHE: $zalsa::IngredientCache<$zalsa_struct::IngredientImpl<$Configuration>> =
$zalsa::IngredientCache::new();

CACHE.get_or_create(zalsa, || {
zalsa.add_or_lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>()
})
let index = zalsa.add_or_lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>();
zalsa.lookup_ingredient(index).assert_type()
}
}

Expand Down
23 changes: 22 additions & 1 deletion src/hash.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::hash::{BuildHasher, Hash};
use std::hash::{BuildHasher, Hash, Hasher};

pub(crate) type FxHasher = std::hash::BuildHasherDefault<rustc_hash::FxHasher>;
pub(crate) type FxIndexSet<K> = indexmap::IndexSet<K, FxHasher>;
Expand All @@ -8,3 +8,24 @@ pub(crate) type FxHashSet<K> = std::collections::HashSet<K, FxHasher>;
pub(crate) fn hash<T: Hash>(t: &T) -> u64 {
FxHasher::default().hash_one(t)
}

// `TypeId` is a 128-bit hash internally, and it's `Hash` implementation
// writes the lower 64-bits. Hashing it again would be unnecessary.
#[derive(Default)]
pub(crate) struct TypeIdHasher(u64);

impl Hasher for TypeIdHasher {
fn write(&mut self, _: &[u8]) {
unreachable!("`TypeId` calls `write_u64`");
}

#[inline]
fn write_u64(&mut self, id: u64) {
self.0 = id;
}

#[inline]
fn finish(&self) -> u64 {
self.0
}
}
1 change: 1 addition & 0 deletions src/ingredient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub trait Jar: Any {
}

/// Create the ingredients given the index of the first one.
///
/// All subsequent ingredients will be assigned contiguous indices.
fn create_ingredients(
zalsa: &Zalsa,
Expand Down
5 changes: 1 addition & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ mod input;
mod interned;
mod key;
mod memo_ingredient_indices;
mod nonce;
#[cfg(feature = "rayon")]
mod parallel;
mod return_mode;
Expand Down Expand Up @@ -99,9 +98,7 @@ pub mod plumbing {
pub use crate::tracked_struct::TrackedStructInDb;
pub use crate::update::helper::{Dispatch as UpdateDispatch, Fallback as UpdateFallback};
pub use crate::update::{always_update, Update};
pub use crate::zalsa::{
transmute_data_ptr, views, IngredientCache, IngredientIndex, Zalsa, ZalsaDatabase,
};
pub use crate::zalsa::{transmute_data_ptr, views, IngredientIndex, Zalsa, ZalsaDatabase};
pub use crate::zalsa_local::ZalsaLocal;

pub mod accumulator {
Expand Down
43 changes: 0 additions & 43 deletions src/nonce.rs

This file was deleted.

Loading
Loading