Skip to content

Commit 703e12d

Browse files
committed
remove the Zalsa trait and make it a struct
1 parent 34e109d commit 703e12d

File tree

4 files changed

+95
-141
lines changed

4 files changed

+95
-141
lines changed

src/database.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use parking_lot::{Condvar, Mutex};
55
use crate::{
66
self as salsa,
77
local_state::{self, LocalState},
8-
storage::{Zalsa, ZalsaImpl},
8+
storage::Zalsa,
99
Durability, Event, EventKind, Revision,
1010
};
1111

@@ -61,14 +61,14 @@ pub unsafe trait Database: Send + AsDynDatabase + Any {
6161

6262
/// Plumbing method: Access the internal salsa methods.
6363
#[doc(hidden)]
64-
fn zalsa(&self) -> &dyn Zalsa;
64+
fn zalsa(&self) -> &Zalsa;
6565

6666
/// Plumbing method: Access the internal salsa methods for mutating the database.
6767
///
6868
/// **WARNING:** Triggers a new revision, canceling other database handles.
6969
/// This can lead to deadlock!
7070
#[doc(hidden)]
71-
fn zalsa_mut(&mut self) -> &mut dyn Zalsa;
71+
fn zalsa_mut(&mut self) -> &mut Zalsa;
7272

7373
/// Access the thread-local state associated with this database
7474
#[doc(hidden)]
@@ -113,10 +113,10 @@ impl dyn Database {
113113
/// Takes an optional type parameter `U` that allows you to thread your own data.
114114
pub struct DatabaseImpl<U: UserData = ()> {
115115
/// Reference to the database. This is always `Some` except during destruction.
116-
zalsa_impl: Option<Arc<ZalsaImpl>>,
116+
zalsa_impl: Option<Arc<Zalsa>>,
117117

118118
/// Coordination data for cancellation of other handles when `zalsa_mut` is called.
119-
/// This could be stored in ZalsaImpl but it makes things marginally cleaner to keep it separate.
119+
/// This could be stored in Zalsa but it makes things marginally cleaner to keep it separate.
120120
coordinate: Arc<Coordinate>,
121121

122122
/// Per-thread state
@@ -147,7 +147,7 @@ impl<U: UserData> DatabaseImpl<U> {
147147
/// You can also use the [`Default`][] trait if your userdata implements it.
148148
pub fn with(u: U) -> Self {
149149
Self {
150-
zalsa_impl: Some(Arc::new(ZalsaImpl::with(u))),
150+
zalsa_impl: Some(Arc::new(Zalsa::with(u))),
151151
coordinate: Arc::new(Coordinate {
152152
clones: Mutex::new(1),
153153
cvar: Default::default(),
@@ -157,10 +157,10 @@ impl<U: UserData> DatabaseImpl<U> {
157157
}
158158
}
159159

160-
/// Access the `Arc<ZalsaImpl>`. This should always be
160+
/// Access the `Arc<Zalsa>`. This should always be
161161
/// possible as `zalsa_impl` only becomes
162162
/// `None` once we are in the `Drop` impl.
163-
fn zalsa_impl(&self) -> &Arc<ZalsaImpl> {
163+
fn zalsa_impl(&self) -> &Arc<Zalsa> {
164164
self.zalsa_impl.as_ref().unwrap()
165165
}
166166

@@ -200,11 +200,11 @@ impl<U: UserData + RefUnwindSafe> RefUnwindSafe for DatabaseImpl<U> {}
200200

201201
#[salsa_macros::db]
202202
unsafe impl<U: UserData> Database for DatabaseImpl<U> {
203-
fn zalsa(&self) -> &dyn Zalsa {
203+
fn zalsa(&self) -> &Zalsa {
204204
&**self.zalsa_impl()
205205
}
206206

207-
fn zalsa_mut(&mut self) -> &mut dyn Zalsa {
207+
fn zalsa_mut(&mut self) -> &mut Zalsa {
208208
self.cancel_others();
209209

210210
// The ref count on the `Arc` should now be 1

src/function/maybe_changed_after.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ where
9898
pub(super) fn shallow_verify_memo(
9999
&self,
100100
db: &C::DbView,
101-
zalsa: &dyn Zalsa,
101+
zalsa: &Zalsa,
102102
database_key_index: DatabaseKeyIndex,
103103
memo: &Memo<C::Output<'_>>,
104104
) -> bool {

src/function/memo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl<V> Memo<V> {
129129
}
130130
}
131131
/// True if this memo is known not to have changed based on its durability.
132-
pub(super) fn check_durability(&self, zalsa: &dyn Zalsa) -> bool {
132+
pub(super) fn check_durability(&self, zalsa: &Zalsa) -> bool {
133133
let last_changed = zalsa.last_changed_revision(self.revisions.durability);
134134
let verified_at = self.verified_at.load();
135135
tracing::debug!(

src/storage.rs

Lines changed: 83 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -16,131 +16,6 @@ pub fn views<Db: ?Sized + Database>(db: &Db) -> &Views {
1616
db.zalsa().views()
1717
}
1818

19-
/// The "plumbing interface" to the Salsa database.
20-
///
21-
/// **NOT SEMVER STABLE.**
22-
pub trait Zalsa {
23-
/// Returns a reference to the underlying.
24-
fn views(&self) -> &Views;
25-
26-
/// Returns the nonce for the underyling storage.
27-
///
28-
/// # Safety
29-
///
30-
/// This nonce is guaranteed to be unique for the database and never to be reused.
31-
fn nonce(&self) -> Nonce<StorageNonce>;
32-
33-
/// Lookup the index assigned to the given jar (if any). This lookup is based purely on the jar's type.
34-
fn lookup_jar_by_type(&self, jar: &dyn Jar) -> Option<IngredientIndex>;
35-
36-
/// Adds a jar to the database, returning the index of the first ingredient.
37-
/// If a jar of this type is already present, returns the existing index.
38-
fn add_or_lookup_jar_by_type(&self, jar: &dyn Jar) -> IngredientIndex;
39-
40-
/// Gets an `&`-ref to an ingredient by index
41-
fn lookup_ingredient(&self, index: IngredientIndex) -> &dyn Ingredient;
42-
43-
/// Gets an `&mut`-ref to an ingredient by index.
44-
fn lookup_ingredient_mut(&mut self, index: IngredientIndex) -> &mut dyn Ingredient;
45-
46-
fn runtimex(&self) -> &Runtime;
47-
48-
/// Return the current revision
49-
fn current_revision(&self) -> Revision;
50-
51-
/// Return the time when an input of durability `durability` last changed
52-
fn last_changed_revision(&self, durability: Durability) -> Revision;
53-
54-
/// True if any threads have signalled for cancellation
55-
fn load_cancellation_flag(&self) -> bool;
56-
57-
/// Signal for cancellation, indicating current thread is trying to get unique access.
58-
fn set_cancellation_flag(&self);
59-
60-
/// Reports a (synthetic) tracked write to "some input of the given durability".
61-
fn report_tracked_write(&mut self, durability: Durability);
62-
}
63-
64-
impl Zalsa for ZalsaImpl {
65-
fn views(&self) -> &Views {
66-
&self.views_of
67-
}
68-
69-
fn nonce(&self) -> Nonce<StorageNonce> {
70-
self.nonce
71-
}
72-
73-
fn lookup_jar_by_type(&self, jar: &dyn Jar) -> Option<IngredientIndex> {
74-
self.jar_map.lock().get(&jar.type_id()).copied()
75-
}
76-
77-
fn add_or_lookup_jar_by_type(&self, jar: &dyn Jar) -> IngredientIndex {
78-
{
79-
let jar_type_id = jar.type_id();
80-
let mut jar_map = self.jar_map.lock();
81-
*jar_map
82-
.entry(jar_type_id)
83-
.or_insert_with(|| {
84-
let index = IngredientIndex::from(self.ingredients_vec.len());
85-
let ingredients = jar.create_ingredients(index);
86-
for ingredient in ingredients {
87-
let expected_index = ingredient.ingredient_index();
88-
89-
if ingredient.requires_reset_for_new_revision() {
90-
self.ingredients_requiring_reset.push(expected_index);
91-
}
92-
93-
let actual_index = self
94-
.ingredients_vec
95-
.push(ingredient);
96-
assert_eq!(
97-
expected_index.as_usize(),
98-
actual_index,
99-
"ingredient `{:?}` was predicted to have index `{:?}` but actually has index `{:?}`",
100-
self.ingredients_vec.get(actual_index).unwrap(),
101-
expected_index,
102-
actual_index,
103-
);
104-
105-
}
106-
index
107-
})
108-
}
109-
}
110-
111-
fn lookup_ingredient(&self, index: IngredientIndex) -> &dyn Ingredient {
112-
&**self.ingredients_vec.get(index.as_usize()).unwrap()
113-
}
114-
115-
fn lookup_ingredient_mut(&mut self, index: IngredientIndex) -> &mut dyn Ingredient {
116-
&mut **self.ingredients_vec.get_mut(index.as_usize()).unwrap()
117-
}
118-
119-
fn current_revision(&self) -> Revision {
120-
self.runtime.current_revision()
121-
}
122-
123-
fn load_cancellation_flag(&self) -> bool {
124-
self.runtime.load_cancellation_flag()
125-
}
126-
127-
fn report_tracked_write(&mut self, durability: Durability) {
128-
self.runtime.report_tracked_write(durability)
129-
}
130-
131-
fn runtimex(&self) -> &Runtime {
132-
&self.runtime
133-
}
134-
135-
fn last_changed_revision(&self, durability: Durability) -> Revision {
136-
self.runtime.last_changed_revision(durability)
137-
}
138-
139-
fn set_cancellation_flag(&self) {
140-
self.runtime.set_cancellation_flag()
141-
}
142-
}
143-
14419
/// Nonce type representing the underlying database storage.
14520
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
14621
pub struct StorageNonce;
@@ -180,9 +55,10 @@ impl IngredientIndex {
18055
}
18156
}
18257

183-
/// The "storage" struct stores all the data for the jars.
184-
/// It is shared between the main database and any active snapshots.
185-
pub(crate) struct ZalsaImpl {
58+
/// The "plumbing interface" to the Salsa database. Stores all the ingredients and other data.
59+
///
60+
/// **NOT SEMVER STABLE.**
61+
pub struct Zalsa {
18662
user_data: Box<dyn Any + Send + Sync>,
18763

18864
views_of: Views,
@@ -210,7 +86,7 @@ pub(crate) struct ZalsaImpl {
21086
runtime: Runtime,
21187
}
21288

213-
impl ZalsaImpl {
89+
impl Zalsa {
21490
pub(crate) fn with<U: UserData>(user_data: U) -> Self {
21591
Self {
21692
views_of: Views::new::<DatabaseImpl<U>>(),
@@ -223,6 +99,84 @@ impl ZalsaImpl {
22399
}
224100
}
225101

102+
pub(crate) fn views(&self) -> &Views {
103+
&self.views_of
104+
}
105+
106+
pub(crate) fn nonce(&self) -> Nonce<StorageNonce> {
107+
self.nonce
108+
}
109+
110+
/// **NOT SEMVER STABLE**
111+
pub fn add_or_lookup_jar_by_type(&self, jar: &dyn Jar) -> IngredientIndex {
112+
{
113+
let jar_type_id = jar.type_id();
114+
let mut jar_map = self.jar_map.lock();
115+
*jar_map
116+
.entry(jar_type_id)
117+
.or_insert_with(|| {
118+
let index = IngredientIndex::from(self.ingredients_vec.len());
119+
let ingredients = jar.create_ingredients(index);
120+
for ingredient in ingredients {
121+
let expected_index = ingredient.ingredient_index();
122+
123+
if ingredient.requires_reset_for_new_revision() {
124+
self.ingredients_requiring_reset.push(expected_index);
125+
}
126+
127+
let actual_index = self
128+
.ingredients_vec
129+
.push(ingredient);
130+
assert_eq!(
131+
expected_index.as_usize(),
132+
actual_index,
133+
"ingredient `{:?}` was predicted to have index `{:?}` but actually has index `{:?}`",
134+
self.ingredients_vec.get(actual_index).unwrap(),
135+
expected_index,
136+
actual_index,
137+
);
138+
139+
}
140+
index
141+
})
142+
}
143+
}
144+
145+
pub(crate) fn lookup_ingredient(&self, index: IngredientIndex) -> &dyn Ingredient {
146+
&**self.ingredients_vec.get(index.as_usize()).unwrap()
147+
}
148+
149+
/// **NOT SEMVER STABLE**
150+
pub fn lookup_ingredient_mut(&mut self, index: IngredientIndex) -> &mut dyn Ingredient {
151+
&mut **self.ingredients_vec.get_mut(index.as_usize()).unwrap()
152+
}
153+
154+
/// **NOT SEMVER STABLE**
155+
pub fn current_revision(&self) -> Revision {
156+
self.runtime.current_revision()
157+
}
158+
159+
pub(crate) fn load_cancellation_flag(&self) -> bool {
160+
self.runtime.load_cancellation_flag()
161+
}
162+
163+
pub(crate) fn report_tracked_write(&mut self, durability: Durability) {
164+
self.runtime.report_tracked_write(durability)
165+
}
166+
167+
pub(crate) fn runtimex(&self) -> &Runtime {
168+
&self.runtime
169+
}
170+
171+
/// **NOT SEMVER STABLE**
172+
pub fn last_changed_revision(&self, durability: Durability) -> Revision {
173+
self.runtime.last_changed_revision(durability)
174+
}
175+
176+
pub(crate) fn set_cancellation_flag(&self) {
177+
self.runtime.set_cancellation_flag()
178+
}
179+
226180
pub(crate) fn user_data(&self) -> &(dyn Any + Send + Sync) {
227181
&*self.user_data
228182
}

0 commit comments

Comments
 (0)