Skip to content
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

make sure that removing collectibles will cleanup helper entries #2872

Merged
merged 4 commits into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 9 additions & 0 deletions crates/turbo-tasks-memory/src/count_hash_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,22 @@ impl<T, H: Default> CountHashSet<T, H> {
}

impl<T, H> CountHashSet<T, H> {
/// Get the number of positive entries
pub fn len(&self) -> usize {
self.inner.len() - self.negative_entries
}

/// Checks if the set looks empty from outside. It might still have negative
/// entries, but they should be treated as not existing.
pub fn is_empty(&self) -> bool {
self.len() == 0
}

/// Checks if this set is equal to a fresh created set, meaning it has no
/// positive but also no negative entries.
pub fn is_unset(&self) -> bool {
self.inner.is_empty()
}
}

impl<T: Eq + Hash, H: BuildHasher + Default> CountHashSet<T, H> {
Expand Down
73 changes: 55 additions & 18 deletions crates/turbo-tasks-memory/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
sync::atomic::{AtomicIsize, AtomicUsize, Ordering},
};

use auto_hash_map::{AutoMap, AutoSet};
use auto_hash_map::{map::Entry, AutoMap, AutoSet};
use nohash_hasher::BuildNoHashHasher;
use parking_lot::Mutex;
use turbo_tasks::{
Expand Down Expand Up @@ -387,8 +387,12 @@ impl TaskScope {
reader: TaskId,
) {
let mut state = self.state.lock();
if let Some((_, dependent_tasks)) = state.collectibles.get_mut(&trait_type) {
if let Entry::Occupied(mut entry) = state.collectibles.entry(trait_type) {
let (collectibles, dependent_tasks) = entry.get_mut();
dependent_tasks.remove(&reader);
if collectibles.is_unset() && dependent_tasks.is_empty() {
entry.remove();
}
}
}
}
Expand Down Expand Up @@ -535,14 +539,32 @@ impl TaskScopeState {
collectible: RawVc,
count: usize,
) -> Option<ScopeCollectibleChangeEffect> {
let (collectibles, dependent_tasks) = self.collectibles.entry(trait_id).or_default();
if collectibles.add_count(collectible, count) {
log_scope_update!("add_collectible {} -> {}", *self.id, collectible);
Some(ScopeCollectibleChangeEffect {
notify: take(dependent_tasks),
})
} else {
None
match self.collectibles.entry(trait_id) {
Entry::Occupied(mut entry) => {
let (collectibles, dependent_tasks) = entry.get_mut();
if collectibles.add_count(collectible, count) {
log_scope_update!("add_collectible {} -> {}", *self.id, collectible);
Some(ScopeCollectibleChangeEffect {
notify: take(dependent_tasks),
})
} else {
if collectibles.is_unset() && dependent_tasks.is_empty() {
entry.remove();
}
None
}
}
Entry::Vacant(entry) => {
let result = entry
.insert(Default::default())
.0
.add_count(collectible, count);
debug_assert!(result, "this must be always a new entry");
log_scope_update!("add_collectible {} -> {}", *self.id, collectible);
Some(ScopeCollectibleChangeEffect {
notify: AutoSet::new(),
})
}
}
}

Expand All @@ -568,14 +590,29 @@ impl TaskScopeState {
collectible: RawVc,
count: usize,
) -> Option<ScopeCollectibleChangeEffect> {
let (collectibles, dependent_tasks) = self.collectibles.entry(trait_id).or_default();
if collectibles.remove_count(collectible, count) {
log_scope_update!("remove_collectible {} -> {}", *self.id, collectible);
Some(ScopeCollectibleChangeEffect {
notify: take(dependent_tasks),
})
} else {
None
match self.collectibles.entry(trait_id) {
Entry::Occupied(mut entry) => {
let (collectibles, dependent_tasks) = entry.get_mut();
if collectibles.remove_count(collectible, count) {
let notify = take(dependent_tasks);
if collectibles.is_unset() {
entry.remove();
}
log_scope_update!("remove_collectible {} -> {}", *self.id, collectible);
Some(ScopeCollectibleChangeEffect { notify })
} else {
None
}
}
Entry::Vacant(e) => {
let result = e
.insert(Default::default())
.0
.remove_count(collectible, count);

debug_assert!(!result, "this must never be visible from outside");
None
}
}
}

Expand Down