Skip to content

Commit b918b7f

Browse files
committed
Put IngredientIndex into SyncTable
1 parent fe07f3f commit b918b7f

File tree

6 files changed

+36
-33
lines changed

6 files changed

+36
-33
lines changed

src/function.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ where
164164
lru: lru::Lru::new(lru),
165165
deleted_entries: Default::default(),
166166
view_caster,
167-
sync_table: Default::default(),
167+
sync_table: SyncTable::new(index),
168168
}
169169
}
170170

@@ -273,10 +273,7 @@ where
273273
/// Attempts to claim `key_index`, returning `false` if a cycle occurs.
274274
fn wait_for(&self, db: &dyn Database, key_index: Id) -> bool {
275275
let zalsa = db.zalsa();
276-
match self
277-
.sync_table
278-
.try_claim(db, zalsa, self.database_key_index(key_index))
279-
{
276+
match self.sync_table.try_claim(db, zalsa, key_index) {
280277
ClaimResult::Retry | ClaimResult::Claimed(_) => true,
281278
ClaimResult::Cycle => false,
282279
}

src/function/fetch.rs

+10-15
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,11 @@ where
9696
id: Id,
9797
memo_ingredient_index: MemoIngredientIndex,
9898
) -> Option<&'db Memo<C::Output<'db>>> {
99-
let database_key_index = self.database_key_index(id);
100-
10199
// Try to claim this query: if someone else has claimed it already, go back and start again.
102-
let _claim_guard = match self.sync_table.try_claim(db, zalsa, database_key_index) {
100+
let _claim_guard = match self.sync_table.try_claim(db, zalsa, id) {
103101
ClaimResult::Retry => return None,
104102
ClaimResult::Cycle => {
103+
let database_key_index = self.database_key_index(id);
105104
// check if there's a provisional value for this query
106105
// Note we don't `validate_may_be_provisional` the memo here as we want to reuse an
107106
// existing provisional memo if it exists
@@ -144,12 +143,10 @@ where
144143
database_key_index,
145144
zalsa.current_revision(),
146145
);
147-
let initial_value = self
148-
.initial_value(db, database_key_index.key_index())
149-
.expect(
150-
"`CycleRecoveryStrategy::Fixpoint` \
146+
let initial_value = self.initial_value(db, id).expect(
147+
"`CycleRecoveryStrategy::Fixpoint` \
151148
should have initial_value",
152-
);
149+
);
153150
Some(self.insert_memo(
154151
zalsa,
155152
id,
@@ -162,12 +159,10 @@ where
162159
"hit a `FallbackImmediate` cycle at {database_key_index:#?}"
163160
);
164161
let active_query = db.zalsa_local().push_query(database_key_index, 0);
165-
let fallback_value = self
166-
.initial_value(db, database_key_index.key_index())
167-
.expect(
168-
"`CycleRecoveryStrategy::FallbackImmediate` \
162+
let fallback_value = self.initial_value(db, id).expect(
163+
"`CycleRecoveryStrategy::FallbackImmediate` \
169164
should have initial_value",
170-
);
165+
);
171166
let mut revisions = active_query.pop();
172167
revisions.cycle_heads = CycleHeads::initial(database_key_index);
173168
// We need this for `cycle_heads()` to work. We will unset this in the outer `execute()`.
@@ -189,7 +184,7 @@ where
189184
if let Some(old_memo) = opt_old_memo {
190185
if old_memo.value.is_some() {
191186
if let VerifyResult::Unchanged(_, cycle_heads) =
192-
self.deep_verify_memo(db, zalsa, old_memo, database_key_index)
187+
self.deep_verify_memo(db, zalsa, old_memo, self.database_key_index(id))
193188
{
194189
if cycle_heads.is_empty() {
195190
// SAFETY: memo is present in memo_map and we have verified that it is
@@ -202,7 +197,7 @@ where
202197

203198
let memo = self.execute(
204199
db,
205-
db.zalsa_local().push_query(database_key_index, 0),
200+
db.zalsa_local().push_query(self.database_key_index(id), 0),
206201
opt_old_memo,
207202
);
208203

src/function/maybe_changed_after.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ where
102102
) -> Option<VerifyResult> {
103103
let database_key_index = self.database_key_index(key_index);
104104

105-
let _claim_guard = match self.sync_table.try_claim(db, zalsa, database_key_index) {
105+
let _claim_guard = match self.sync_table.try_claim(db, zalsa, key_index) {
106106
ClaimResult::Retry => return None,
107107
ClaimResult::Cycle => match C::CYCLE_STRATEGY {
108108
CycleRecoveryStrategy::Panic => db.zalsa_local().with_query_stack(|stack| {

src/function/sync.rs

+21-10
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ use crate::{
77
key::DatabaseKeyIndex,
88
runtime::{BlockResult, WaitResult},
99
zalsa::Zalsa,
10-
Database, Id,
10+
Database, Id, IngredientIndex,
1111
};
1212

1313
/// Tracks the keys that are currently being processed; used to coordinate between
1414
/// worker threads.
15-
#[derive(Default)]
1615
pub(crate) struct SyncTable {
1716
syncs: Mutex<FxHashMap<Id, SyncState>>,
17+
ingredient: IngredientIndex,
1818
}
1919

2020
pub(crate) enum ClaimResult<'a> {
@@ -32,14 +32,21 @@ struct SyncState {
3232
}
3333

3434
impl SyncTable {
35+
pub(crate) fn new(ingredient: IngredientIndex) -> Self {
36+
Self {
37+
syncs: Default::default(),
38+
ingredient,
39+
}
40+
}
41+
3542
pub(crate) fn try_claim<'me>(
3643
&'me self,
3744
db: &'me (impl ?Sized + Database),
3845
zalsa: &'me Zalsa,
39-
database_key_index: DatabaseKeyIndex,
46+
key_index: Id,
4047
) -> ClaimResult<'me> {
4148
let mut write = self.syncs.lock();
42-
match write.entry(database_key_index.key_index()) {
49+
match write.entry(key_index) {
4350
std::collections::hash_map::Entry::Occupied(occupied_entry) => {
4451
let &mut SyncState {
4552
id,
@@ -52,7 +59,12 @@ impl SyncTable {
5259
// boolean is to decide *whether* to acquire the lock,
5360
// not to gate future atomic reads.
5461
*anyone_waiting = true;
55-
match zalsa.runtime().block_on(db, database_key_index, id, write) {
62+
match zalsa.runtime().block_on(
63+
db,
64+
DatabaseKeyIndex::new(self.ingredient, key_index),
65+
id,
66+
write,
67+
) {
5668
BlockResult::Completed => ClaimResult::Retry,
5769
BlockResult::Cycle => ClaimResult::Cycle,
5870
}
@@ -63,7 +75,7 @@ impl SyncTable {
6375
anyone_waiting: false,
6476
});
6577
ClaimResult::Claimed(ClaimGuard {
66-
database_key_index,
78+
key_index,
6779
zalsa,
6880
sync_table: self,
6981
_padding: false,
@@ -77,7 +89,7 @@ impl SyncTable {
7789
/// released when this value is dropped.
7890
#[must_use]
7991
pub(crate) struct ClaimGuard<'me> {
80-
database_key_index: DatabaseKeyIndex,
92+
key_index: Id,
8193
zalsa: &'me Zalsa,
8294
sync_table: &'me SyncTable,
8395
// Reduce the size of ClaimResult by making more niches available in ClaimGuard; this fits into
@@ -89,12 +101,11 @@ impl ClaimGuard<'_> {
89101
fn remove_from_map_and_unblock_queries(&self) {
90102
let mut syncs = self.sync_table.syncs.lock();
91103

92-
let SyncState { anyone_waiting, .. } =
93-
syncs.remove(&self.database_key_index.key_index()).unwrap();
104+
let SyncState { anyone_waiting, .. } = syncs.remove(&self.key_index).unwrap();
94105

95106
if anyone_waiting {
96107
self.zalsa.runtime().unblock_queries_blocked_on(
97-
self.database_key_index,
108+
DatabaseKeyIndex::new(self.sync_table.ingredient, self.key_index),
98109
if std::thread::panicking() {
99110
WaitResult::Panicked
100111
} else {

src/runtime.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl Runtime {
171171
other_id: ThreadId,
172172
query_mutex_guard: QueryMutexGuard,
173173
) -> BlockResult {
174-
let mut dg = self.dependency_graph.lock();
174+
let dg = self.dependency_graph.lock();
175175
let thread_id = std::thread::current().id();
176176

177177
if dg.depends_on(other_id, thread_id) {

src/runtime/dependency_graph.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl DependencyGraph {
3131
/// True if `from_id` depends on `to_id`.
3232
///
3333
/// (i.e., there is a path from `from_id` to `to_id` in the graph.)
34-
pub(super) fn depends_on(&mut self, from_id: ThreadId, to_id: ThreadId) -> bool {
34+
pub(super) fn depends_on(&self, from_id: ThreadId, to_id: ThreadId) -> bool {
3535
let mut p = from_id;
3636
while let Some(q) = self.edges.get(&p).map(|edge| edge.blocked_on_id) {
3737
if q == to_id {

0 commit comments

Comments
 (0)