Skip to content

Commit db61adc

Browse files
authored
refactor: Remove some unnecessary panicking paths in cycle execution (#765)
1 parent 67dd29a commit db61adc

File tree

1 file changed

+13
-24
lines changed

1 file changed

+13
-24
lines changed

src/function/execute.rs

+13-24
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ where
2626
mut active_query: ActiveQueryGuard<'db>,
2727
opt_old_memo: Option<&Memo<C::Output<'_>>>,
2828
) -> &'db Memo<C::Output<'db>> {
29-
let (zalsa, zalsa_local) = db.zalsas();
29+
let zalsa = db.zalsa();
3030
let revision_now = zalsa.current_revision();
3131
let database_key_index = active_query.database_key_index;
3232
let id = database_key_index.key_index();
@@ -65,32 +65,21 @@ where
6565
if C::CYCLE_STRATEGY == CycleRecoveryStrategy::Fixpoint
6666
&& revisions.cycle_heads.contains(&database_key_index)
6767
{
68-
let opt_owned_last_provisional;
6968
let last_provisional_value = if let Some(last_provisional) = opt_last_provisional {
7069
// We have a last provisional value from our previous time around the loop.
71-
last_provisional
72-
.value
73-
.as_ref()
74-
.expect("provisional value should not be evicted by LRU")
70+
last_provisional.value.as_ref()
7571
} else {
7672
// This is our first time around the loop; a provisional value must have been
7773
// inserted into the memo table when the cycle was hit, so let's pull our
7874
// initial provisional value from there.
79-
opt_owned_last_provisional =
80-
self.get_memo_from_table_for(zalsa, id, memo_ingredient_index);
81-
debug_assert!(opt_owned_last_provisional
82-
.as_ref()
83-
.unwrap()
84-
.may_be_provisional());
85-
opt_owned_last_provisional
86-
.expect(
87-
"{database_key_index:#?} is a cycle head, \
88-
but no provisional memo found",
89-
)
90-
.value
91-
.as_ref()
92-
.expect("provisional value should not be evicted by LRU")
75+
let memo =
76+
self.get_memo_from_table_for(zalsa, id, memo_ingredient_index)
77+
.unwrap_or_else(|| panic!("{database_key_index:#?} is a cycle head, but no provisional memo found"));
78+
debug_assert!(memo.may_be_provisional());
79+
memo.value.as_ref()
9380
};
81+
// SAFETY: The `LRU` does not run mid-execution, so the value remains filled
82+
let last_provisional_value = unsafe { last_provisional_value.unwrap_unchecked() };
9483
tracing::debug!(
9584
"{database_key_index:?}: execute: \
9685
I am a cycle head, comparing last provisional value with new value"
@@ -130,9 +119,9 @@ where
130119
fell_back = true;
131120
}
132121
}
133-
iteration_count = iteration_count
134-
.checked_add(1)
135-
.expect("fixpoint iteration should converge before u32::MAX iterations");
122+
// `iteration_count` can't overflow as we check it against `MAX_ITERATIONS`
123+
// which is less than `u32::MAX`.
124+
iteration_count += 1;
136125
if iteration_count > MAX_ITERATIONS {
137126
panic!("{database_key_index:?}: execute: too many cycle iterations");
138127
}
@@ -143,7 +132,7 @@ where
143132
memo_ingredient_index,
144133
));
145134

146-
active_query = zalsa_local.push_query(database_key_index);
135+
active_query = db.zalsa_local().push_query(database_key_index);
147136

148137
continue;
149138
}

0 commit comments

Comments
 (0)