Skip to content

Commit 5b718d7

Browse files
committed
Simplify CycleHeads ref iterator
1 parent a13bcba commit 5b718d7

File tree

3 files changed

+35
-38
lines changed

3 files changed

+35
-38
lines changed

src/cycle.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,21 +127,16 @@ impl CycleHeads {
127127
}
128128
}
129129
}
130-
}
131130

132-
impl std::iter::Extend<DatabaseKeyIndex> for CycleHeads {
133-
fn extend<T: IntoIterator<Item = DatabaseKeyIndex>>(&mut self, iter: T) {
134-
let mut iter = iter.into_iter();
135-
if let Some(first) = iter.next() {
131+
pub(crate) fn extend(&mut self, other: &Self) {
132+
if let Some(other) = &other.0 {
136133
let heads = &mut **self.0.get_or_insert_with(|| Box::new(Vec::new()));
137-
if !heads.contains(&first) {
138-
heads.push(first);
139-
}
140-
for head in iter {
134+
heads.reserve(other.len());
135+
other.iter().for_each(|&head| {
141136
if !heads.contains(&head) {
142137
heads.push(head);
143138
}
144-
}
139+
});
145140
}
146141
}
147142
}

src/function/maybe_changed_after.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,12 @@ where
239239
zalsa: &Zalsa,
240240
memo: &Memo<C::Output<'_>>,
241241
) -> bool {
242-
for cycle_head in &memo.revisions.cycle_heads {
243-
if zalsa
242+
if (&memo.revisions.cycle_heads).into_iter().any(|cycle_head| {
243+
zalsa
244244
.lookup_ingredient(cycle_head.ingredient_index)
245245
.is_provisional_cycle_head(db.as_dyn_database(), cycle_head.key_index)
246-
{
247-
return false;
248-
}
246+
}) {
247+
return false;
249248
}
250249
// Relaxed is sufficient here because there are no other writes we need to ensure have
251250
// happened before marking this memo as verified-final.

src/function/memo.rs

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -177,29 +177,32 @@ impl<V> Memo<V> {
177177
database_key_index: DatabaseKeyIndex,
178178
) -> bool {
179179
let mut retry = false;
180-
for head in self.cycle_heads() {
181-
if head == database_key_index {
182-
continue;
183-
}
184-
let ingredient = db.zalsa().lookup_ingredient(head.ingredient_index);
185-
if !ingredient.is_provisional_cycle_head(db, head.key_index) {
186-
// This cycle is already finalized, so we don't need to wait on it;
187-
// keep looping through cycle heads.
188-
retry = true;
189-
continue;
190-
}
191-
if ingredient.wait_for(db, head.key_index) {
192-
// There's a new memo available for the cycle head; fetch our own
193-
// updated memo and see if it's still provisional or if the cycle
194-
// has resolved.
195-
retry = true;
196-
continue;
197-
} else {
198-
// We hit a cycle blocking on the cycle head; this means it's in
199-
// our own active query stack and we are responsible to resolve the
200-
// cycle, so go ahead and return the provisional memo.
201-
return false;
202-
}
180+
let hit_cycle = self
181+
.cycle_heads()
182+
.into_iter()
183+
.filter(|&head| head != database_key_index)
184+
.any(|head| {
185+
let ingredient = db.zalsa().lookup_ingredient(head.ingredient_index);
186+
if !ingredient.is_provisional_cycle_head(db, head.key_index) {
187+
// This cycle is already finalized, so we don't need to wait on it;
188+
// keep looping through cycle heads.
189+
retry = true;
190+
false
191+
} else if ingredient.wait_for(db, head.key_index) {
192+
// There's a new memo available for the cycle head; fetch our own
193+
// updated memo and see if it's still provisional or if the cycle
194+
// has resolved.
195+
retry = true;
196+
false
197+
} else {
198+
// We hit a cycle blocking on the cycle head; this means it's in
199+
// our own active query stack and we are responsible to resolve the
200+
// cycle, so go ahead and return the provisional memo.
201+
true
202+
}
203+
});
204+
if hit_cycle {
205+
return false;
203206
}
204207
// If `retry` is `true`, all our cycle heads (barring ourself) are complete; re-fetch
205208
// and we should get a non-provisional memo. If we get here and `retry` is still

0 commit comments

Comments
 (0)