Skip to content

Commit 7c8c8be

Browse files
committed
Put IngredientIndex into SyncTable
1 parent ddc7c9d commit 7c8c8be

File tree

4 files changed

+23
-21
lines changed

4 files changed

+23
-21
lines changed

src/function.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ where
160160
lru: lru::Lru::new(lru),
161161
deleted_entries: Default::default(),
162162
view_caster,
163-
sync_table: Default::default(),
163+
sync_table: SyncTable::new(index),
164164
}
165165
}
166166

@@ -252,10 +252,7 @@ where
252252
/// Attempts to claim `key_index`, returning `false` if a cycle occurs.
253253
fn wait_for(&self, db: &dyn Database, key_index: Id) -> bool {
254254
let zalsa = db.zalsa();
255-
match self
256-
.sync_table
257-
.try_claim(db, zalsa, self.database_key_index(key_index))
258-
{
255+
match self.sync_table.try_claim(db, zalsa, key_index) {
259256
ClaimResult::Retry | ClaimResult::Claimed(_) => true,
260257
ClaimResult::Cycle => false,
261258
}

src/function/fetch.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,11 @@ where
104104
id: Id,
105105
memo_ingredient_index: MemoIngredientIndex,
106106
) -> Option<&'db Memo<C::Output<'db>>> {
107-
let database_key_index = self.database_key_index(id);
108-
109107
// Try to claim this query: if someone else has claimed it already, go back and start again.
110-
let _claim_guard = match self.sync_table.try_claim(db, zalsa, database_key_index) {
108+
let _claim_guard = match self.sync_table.try_claim(db, zalsa, id) {
111109
ClaimResult::Retry => return None,
112110
ClaimResult::Cycle => {
111+
let database_key_index = self.database_key_index(id);
113112
// check if there's a provisional value for this query
114113
// Note we don't `validate_may_be_provisional` the memo here as we want to reuse an
115114
// existing provisional memo if it exists
@@ -125,7 +124,7 @@ where
125124
}
126125
// no provisional value; create/insert/return initial provisional value
127126
return self
128-
.initial_value(db, database_key_index.key_index())
127+
.initial_value(db, id)
129128
.map(|initial_value| {
130129
tracing::debug!(
131130
"hit cycle at {database_key_index:#?}, \
@@ -156,7 +155,7 @@ where
156155
};
157156

158157
// Push the query on the stack.
159-
let active_query = db.zalsa_local().push_query(database_key_index);
158+
let active_query = db.zalsa_local().push_query(self.database_key_index(id));
160159

161160
// Now that we've claimed the item, check again to see if there's a "hot" value.
162161
let opt_old_memo = self.get_memo_from_table_for(zalsa, id, memo_ingredient_index);

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
) -> Option<VerifyResult> {
9999
let database_key_index = self.database_key_index(key_index);
100100

101-
let _claim_guard = match self.sync_table.try_claim(db, zalsa, database_key_index) {
101+
let _claim_guard = match self.sync_table.try_claim(db, zalsa, key_index) {
102102
ClaimResult::Retry => return None,
103103
ClaimResult::Cycle => match C::CYCLE_STRATEGY {
104104
CycleRecoveryStrategy::Panic => panic!(

src/function/sync.rs

Lines changed: 16 additions & 10 deletions
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,
@@ -55,7 +62,7 @@ impl SyncTable {
5562
match zalsa.runtime().block_on(
5663
db.as_dyn_database(),
5764
db.zalsa_local(),
58-
database_key_index,
65+
DatabaseKeyIndex::new(self.ingredient, key_index),
5966
id,
6067
write,
6168
) {
@@ -69,7 +76,7 @@ impl SyncTable {
6976
anyone_waiting: false,
7077
});
7178
ClaimResult::Claimed(ClaimGuard {
72-
database_key_index,
79+
key_index,
7380
zalsa,
7481
sync_table: self,
7582
_padding: false,
@@ -83,7 +90,7 @@ impl SyncTable {
8390
/// released when this value is dropped.
8491
#[must_use]
8592
pub(crate) struct ClaimGuard<'me> {
86-
database_key_index: DatabaseKeyIndex,
93+
key_index: Id,
8794
zalsa: &'me Zalsa,
8895
sync_table: &'me SyncTable,
8996
// Reduce the size of ClaimResult by making more niches available in ClaimGuard; this fits into
@@ -95,14 +102,13 @@ impl ClaimGuard<'_> {
95102
fn remove_from_map_and_unblock_queries(&self) {
96103
let mut syncs = self.sync_table.syncs.lock();
97104

98-
let SyncState { anyone_waiting, .. } =
99-
syncs.remove(&self.database_key_index.key_index()).unwrap();
105+
let SyncState { anyone_waiting, .. } = syncs.remove(&self.key_index).unwrap();
100106

101107
drop(syncs);
102108

103109
if anyone_waiting {
104110
self.zalsa.runtime().unblock_queries_blocked_on(
105-
self.database_key_index,
111+
DatabaseKeyIndex::new(self.sync_table.ingredient, self.key_index),
106112
if std::thread::panicking() {
107113
WaitResult::Panicked
108114
} else {

0 commit comments

Comments
 (0)