Skip to content

Remove borrow checks from ZalsaLocal #939

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

Merged
Merged
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
14 changes: 9 additions & 5 deletions src/function/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ where
let _span = crate::tracing::debug_span!("fetch", query = ?database_key_index).entered();

let memo = self.refresh_memo(db, zalsa, zalsa_local, id);

// SAFETY: We just refreshed the memo so it is guaranteed to contain a value now.
let memo_value = unsafe { memo.value.as_ref().unwrap_unchecked() };

Expand Down Expand Up @@ -162,13 +163,16 @@ where
}
// no provisional value; create/insert/return initial provisional value
return match C::CYCLE_STRATEGY {
CycleRecoveryStrategy::Panic => zalsa_local.with_query_stack(|stack| {
panic!(
"dependency graph cycle when querying {database_key_index:#?}, \
// SAFETY: We do not access the query stack reentrantly.
CycleRecoveryStrategy::Panic => unsafe {
zalsa_local.with_query_stack_unchecked(|stack| {
panic!(
"dependency graph cycle when querying {database_key_index:#?}, \
set cycle_fn/cycle_initial to fixpoint iterate.\n\
Query stack:\n{stack:#?}",
);
}),
);
})
},
CycleRecoveryStrategy::Fixpoint => {
crate::tracing::debug!(
"hit cycle at {database_key_index:#?}, \
Expand Down
67 changes: 38 additions & 29 deletions src/function/maybe_changed_after.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,16 @@ where
return None;
}
ClaimResult::Cycle { .. } => match C::CYCLE_STRATEGY {
CycleRecoveryStrategy::Panic => db.zalsa_local().with_query_stack(|stack| {
panic!(
"dependency graph cycle when validating {database_key_index:#?}, \
// SAFETY: We do not access the query stack reentrantly.
CycleRecoveryStrategy::Panic => unsafe {
db.zalsa_local().with_query_stack_unchecked(|stack| {
panic!(
"dependency graph cycle when validating {database_key_index:#?}, \
set cycle_fn/cycle_initial to fixpoint iterate.\n\
Query stack:\n{stack:#?}",
);
}),
);
})
},
CycleRecoveryStrategy::FallbackImmediate => {
return Some(VerifyResult::unchanged());
}
Expand Down Expand Up @@ -336,32 +339,38 @@ where
return true;
}

zalsa_local.with_query_stack(|stack| {
cycle_heads.iter().all(|cycle_head| {
stack
.iter()
.rev()
.find(|query| query.database_key_index == cycle_head.database_key_index)
.map(|query| query.iteration_count())
.or_else(|| {
// If this is a cycle head is owned by another thread that is blocked by this ingredient,
// check if it has the same iteration count.
let ingredient = zalsa
.lookup_ingredient(cycle_head.database_key_index.ingredient_index());
let wait_result =
ingredient.wait_for(zalsa, cycle_head.database_key_index.key_index());

if !wait_result.is_cycle_with_other_thread() {
return None;
}
// SAFETY: We do not access the query stack reentrantly.
unsafe {
zalsa_local.with_query_stack_unchecked(|stack| {
cycle_heads.iter().all(|cycle_head| {
stack
.iter()
.rev()
.find(|query| query.database_key_index == cycle_head.database_key_index)
.map(|query| query.iteration_count())
.or_else(|| {
// If this is a cycle head is owned by another thread that is blocked by this ingredient,
// check if it has the same iteration count.
let ingredient = zalsa.lookup_ingredient(
cycle_head.database_key_index.ingredient_index(),
);
let wait_result = ingredient
.wait_for(zalsa, cycle_head.database_key_index.key_index());

if !wait_result.is_cycle_with_other_thread() {
return None;
}

let provisional_status = ingredient
.provisional_status(zalsa, cycle_head.database_key_index.key_index())?;
provisional_status.iteration()
})
== Some(cycle_head.iteration_count)
let provisional_status = ingredient.provisional_status(
zalsa,
cycle_head.database_key_index.key_index(),
)?;
provisional_status.iteration()
})
== Some(cycle_head.iteration_count)
})
})
})
}
}

/// VerifyResult::Unchanged if the memo's value and `changed_at` time is up-to-date in the
Expand Down
17 changes: 10 additions & 7 deletions src/function/memo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,17 @@ impl<'db, C: Configuration> Memo<'db, C> {
return true;
}

zalsa_local.with_query_stack(|stack| {
cycle_heads.iter().all(|cycle_head| {
stack
.iter()
.rev()
.any(|query| query.database_key_index == cycle_head.database_key_index)
// SAFETY: We do not access the query stack reentrantly.
unsafe {
zalsa_local.with_query_stack_unchecked(|stack| {
cycle_heads.iter().all(|cycle_head| {
stack
.iter()
.rev()
.any(|query| query.database_key_index == cycle_head.database_key_index)
})
})
})
}
}

/// Cycle heads that should be propagated to dependent queries.
Expand Down
Loading