Skip to content

Commit

Permalink
Optimize {run_add_to,run_remove_from}_scope_queue (vercel#2423)
Browse files Browse the repository at this point in the history
* Optimize {run_add_to,run_remove_from}_scope_queue

* Use an actual queue instead of a stack

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
alexkirsz and kodiakhq[bot] authored Oct 28, 2022
1 parent 794d6ab commit 95f8ffa
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 165 deletions.
6 changes: 3 additions & 3 deletions crates/turbo-tasks-memory/src/count_hash_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,20 @@ impl<T: Eq + Hash, H: BuildHasher> CountHashSet<T, H> {
}
}

/// Returns true, when the value has become visible from outside
/// Returns true when the value has become visible from outside
pub fn add(&mut self, item: T) -> bool {
self.add_count(item, 1)
}

/// Returns true, when the value is no longer visible from outside
/// Returns true when the value is no longer visible from outside
pub fn remove_count(&mut self, item: T, count: usize) -> bool {
match self.inner.entry(item) {
Entry::Occupied(mut e) => {
let value = e.get_mut();
let old = *value;
*value -= count as isize;
if *value > 0 {
// It's was and still is positive
// It was and still is positive
false
} else if *value == 0 {
// It was positive and has become zero
Expand Down
25 changes: 11 additions & 14 deletions crates/turbo-tasks-memory/src/memory_backend.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{
borrow::Cow,
cell::RefCell,
collections::HashSet,
collections::{HashSet, VecDeque},
future::Future,
pin::Pin,
sync::atomic::{AtomicUsize, Ordering},
Expand Down Expand Up @@ -521,8 +521,12 @@ pub(crate) enum Job {
RemoveFromScopes(HashSet<TaskId>, Vec<TaskScopeId>),
RemoveFromScope(HashSet<TaskId>, TaskScopeId),
ScheduleWhenDirty(Vec<TaskId>),
AddToScopeQueue(Vec<(Vec<TaskId>, usize)>, usize, TaskScopeId, bool),
RemoveFromScopeQueue(Vec<Vec<TaskId>>, usize, TaskScopeId),
/// Add tasks from a scope. Scheduled by `run_add_from_scope_queue` to
/// split off work.
AddToScopeQueue(VecDeque<(TaskId, usize)>, TaskScopeId, bool),
/// Remove tasks from a scope. Scheduled by `run_remove_from_scope_queue` to
/// split off work.
RemoveFromScopeQueue(VecDeque<TaskId>, TaskScopeId),
}

impl Job {
Expand All @@ -549,18 +553,11 @@ impl Job {
})
}
}
Job::AddToScopeQueue(queue, queue_size, id, is_optimization_scope) => {
run_add_to_scope_queue(
queue,
queue_size,
id,
is_optimization_scope,
backend,
turbo_tasks,
);
Job::AddToScopeQueue(queue, id, is_optimization_scope) => {
run_add_to_scope_queue(queue, id, is_optimization_scope, backend, turbo_tasks);
}
Job::RemoveFromScopeQueue(queue, queue_size, id) => {
run_remove_from_scope_queue(queue, queue_size, id, backend, turbo_tasks);
Job::RemoveFromScopeQueue(queue, id) => {
run_remove_from_scope_queue(queue, id, backend, turbo_tasks);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions crates/turbo-tasks-memory/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,15 +433,15 @@ impl TaskScopeState {
}
}

/// Add a child scope. Returns true, when the child scope need to have it's
/// active counter increased.
/// Add a child scope. Returns a [ScopeChildChangeEffect] when the child
/// scope need to have its active counter increased.
#[must_use]
pub fn add_child(&mut self, child: TaskScopeId) -> Option<ScopeChildChangeEffect> {
self.add_child_count(child, 1)
}

/// Add a child scope. Returns true, when the child scope need to have it's
/// active counter increased.
/// Add a child scope. Returns a [ScopeChildChangeEffect] when the child
/// scope need to have its active counter increased.
#[must_use]
pub fn add_child_count(
&mut self,
Expand Down
Loading

0 comments on commit 95f8ffa

Please sign in to comment.