Skip to content

Commit

Permalink
Reduce method delegation duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Feb 17, 2025
1 parent 1cca261 commit eec8181
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 64 deletions.
4 changes: 2 additions & 2 deletions src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub trait Database: Send + AsDynDatabase + Any + ZalsaDatabase {
/// is owned by the current thread, this could trigger deadlock.
fn trigger_lru_eviction(&mut self) {
let zalsa_mut = self.zalsa_mut();
zalsa_mut.reset_cancellation_flag();
zalsa_mut.runtime_mut().reset_cancellation_flag();
zalsa_mut.evict_lru();
}

Expand All @@ -41,7 +41,7 @@ pub trait Database: Send + AsDynDatabase + Any + ZalsaDatabase {
fn synthetic_write(&mut self, durability: Durability) {
let zalsa_mut = self.zalsa_mut();
zalsa_mut.new_revision();
zalsa_mut.report_tracked_write(durability);
zalsa_mut.runtime_mut().report_tracked_write(durability);
}

/// Reports that the query depends on some state unknown to salsa.
Expand Down
1 change: 1 addition & 0 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ impl Runtime {
*self.revision_canceled.get_mut() = false;
}

/// Returns the [`Table`] used to store the value of salsa structs
pub(crate) fn table(&self) -> &Table {
&self.table
}
Expand Down
4 changes: 2 additions & 2 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl<Db: Database> Storage<Db> {
///
/// Needs to be paired with a call to `reset_cancellation_flag`.
fn cancel_others(&self, db: &Db) {
self.handle.zalsa_impl.set_cancellation_flag();
self.handle.zalsa_impl.runtime().set_cancellation_flag();

db.salsa_event(&|| Event::new(EventKind::DidSetCancellationFlag));

Expand All @@ -144,7 +144,7 @@ unsafe impl<T: HasStorage> ZalsaDatabase for T {
// The ref count on the `Arc` should now be 1
let zalsa = Arc::get_mut(&mut storage.handle.zalsa_impl).unwrap();
// cancellation is done, so reset the flag
zalsa.reset_cancellation_flag();
zalsa.runtime_mut().reset_cancellation_flag();
zalsa
}

Expand Down
9 changes: 8 additions & 1 deletion src/table/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ impl SyncTable {
// boolean is to decide *whether* to acquire the lock,
// not to gate future atomic reads.
anyone_waiting.store(true, Ordering::Relaxed);
zalsa.block_on_or_unwind(db, zalsa_local, database_key_index, *other_id, syncs);
zalsa.runtime().block_on_or_unwind(
db,
zalsa_local,
database_key_index,
*other_id,
syncs,
);
None
}
}
Expand Down Expand Up @@ -96,6 +102,7 @@ impl ClaimGuard<'_> {
// see `store` above for explanation.
if anyone_waiting.load(Ordering::Relaxed) {
self.zalsa
.runtime()
.unblock_queries_blocked_on(self.database_key_index, wait_result)
}
}
Expand Down
87 changes: 29 additions & 58 deletions src/zalsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@ use rustc_hash::FxHashMap;
use std::any::{Any, TypeId};
use std::marker::PhantomData;
use std::panic::RefUnwindSafe;
use std::thread::ThreadId;

use crate::cycle::CycleRecoveryStrategy;
use crate::ingredient::{Ingredient, Jar, JarAux};
use crate::nonce::{Nonce, NonceGenerator};
use crate::runtime::{Runtime, WaitResult};
use crate::runtime::Runtime;
use crate::table::memo::MemoTable;
use crate::table::sync::SyncTable;
use crate::table::Table;
use crate::views::Views;
use crate::zalsa_local::ZalsaLocal;
use crate::{Database, DatabaseKeyIndex, Durability, Id, Revision};
use crate::{Database, Durability, Id, Revision};

/// Internal plumbing trait.
///
Expand Down Expand Up @@ -178,23 +177,47 @@ impl Zalsa {
self.nonce
}

/// Returns the [`Table`][] used to store the value of salsa structs
pub(crate) fn runtime(&self) -> &Runtime {
&self.runtime
}

pub(crate) fn runtime_mut(&mut self) -> &mut Runtime {
&mut self.runtime
}

/// Returns the [`Table`] used to store the value of salsa structs
pub(crate) fn table(&self) -> &Table {
self.runtime.table()
}

/// Returns the [`MemoTable`][] for the salsa struct with the given id
pub(crate) fn memo_table_for(&self, id: Id) -> &MemoTable {
// SAFETY: We are supply the correct current revision
// SAFETY: We are supplying the correct current revision
unsafe { self.table().memos(id, self.current_revision()) }
}

/// Returns the [`SyncTable`][] for the salsa struct with the given id
pub(crate) fn sync_table_for(&self, id: Id) -> &SyncTable {
// SAFETY: We are supply the correct current revision
// SAFETY: We are supplying the correct current revision
unsafe { self.table().syncs(id, self.current_revision()) }
}

pub(crate) fn lookup_ingredient(&self, index: IngredientIndex) -> &dyn Ingredient {
&*self.ingredients_vec[index.as_usize()]
}

pub(crate) fn ingredient_index_for_memo(
&self,
struct_ingredient_index: IngredientIndex,
memo_ingredient_index: MemoIngredientIndex,
) -> IngredientIndex {
self.memo_ingredient_indices.read()[struct_ingredient_index.as_usize()]
[memo_ingredient_index.as_usize()]
}
}

/// Semver unstable APIs used by the macro expansions
impl Zalsa {
/// **NOT SEMVER STABLE**
#[doc(hidden)]
pub fn add_or_lookup_jar_by_type(&self, jar: &dyn Jar) -> IngredientIndex {
Expand Down Expand Up @@ -238,10 +261,6 @@ impl Zalsa {
}
}

pub(crate) fn lookup_ingredient(&self, index: IngredientIndex) -> &dyn Ingredient {
&*self.ingredients_vec[index.as_usize()]
}

/// **NOT SEMVER STABLE**
#[doc(hidden)]
pub fn lookup_ingredient_mut(
Expand All @@ -260,28 +279,12 @@ impl Zalsa {
self.runtime.current_revision()
}

pub(crate) fn load_cancellation_flag(&self) -> bool {
self.runtime.load_cancellation_flag()
}

pub(crate) fn reset_cancellation_flag(&mut self) {
self.runtime.reset_cancellation_flag();
}

pub(crate) fn report_tracked_write(&mut self, durability: Durability) {
self.runtime.report_tracked_write(durability)
}

/// **NOT SEMVER STABLE**
#[doc(hidden)]
pub fn last_changed_revision(&self, durability: Durability) -> Revision {
self.runtime.last_changed_revision(durability)
}

pub(crate) fn set_cancellation_flag(&self) {
self.runtime.set_cancellation_flag()
}

/// **NOT SEMVER STABLE**
/// Triggers a new revision.
#[doc(hidden)]
Expand All @@ -302,38 +305,6 @@ impl Zalsa {
self.ingredients_vec[index.as_usize()].reset_for_new_revision(self.runtime.table_mut());
}
}

/// See [`Runtime::block_on_or_unwind`][]
pub(crate) fn block_on_or_unwind<QueryMutexGuard>(
&self,
db: &dyn Database,
local_state: &ZalsaLocal,
database_key: DatabaseKeyIndex,
other_id: ThreadId,
query_mutex_guard: QueryMutexGuard,
) {
self.runtime
.block_on_or_unwind(db, local_state, database_key, other_id, query_mutex_guard)
}

/// See [`Runtime::unblock_queries_blocked_on`][]
pub(crate) fn unblock_queries_blocked_on(
&self,
database_key: DatabaseKeyIndex,
wait_result: WaitResult,
) {
self.runtime
.unblock_queries_blocked_on(database_key, wait_result)
}

pub(crate) fn ingredient_index_for_memo(
&self,
struct_ingredient_index: IngredientIndex,
memo_ingredient_index: MemoIngredientIndex,
) -> IngredientIndex {
self.memo_ingredient_indices.read()[struct_ingredient_index.as_usize()]
[memo_ingredient_index.as_usize()]
}
}

struct JarAuxImpl<'a>(&'a Zalsa, &'a FxHashMap<TypeId, IngredientIndex>);
Expand Down
2 changes: 1 addition & 1 deletion src/zalsa_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ impl ZalsaLocal {
pub(crate) fn unwind_if_revision_cancelled(&self, db: &dyn Database) {
db.salsa_event(&|| Event::new(EventKind::WillCheckCancellation));
let zalsa = db.zalsa();
if zalsa.load_cancellation_flag() {
if zalsa.runtime().load_cancellation_flag() {
self.unwind_cancelled(zalsa.current_revision());
}
}
Expand Down

0 comments on commit eec8181

Please sign in to comment.