Skip to content

Commit

Permalink
avoid cloning a large list of task ids (vercel/turborepo#5821)
Browse files Browse the repository at this point in the history
### Description

avoids cloning a vec/set for invalidation


Closes WEB-1461
  • Loading branch information
sokra authored Aug 29, 2023
1 parent 789749a commit abba56e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
16 changes: 14 additions & 2 deletions crates/turbo-tasks-memory/src/memory_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,22 @@ impl Backend for MemoryBackend {

fn invalidate_tasks(
&self,
tasks: Vec<TaskId>,
tasks: &[TaskId],
turbo_tasks: &dyn TurboTasksBackendApi<MemoryBackend>,
) {
for task in tasks.into_iter() {
for &task in tasks {
self.with_task(task, |task| {
task.invalidate(self, turbo_tasks);
});
}
}

fn invalidate_tasks_set(
&self,
tasks: &AutoSet<TaskId, BuildNoHashHasher<TaskId>>,
turbo_tasks: &dyn TurboTasksBackendApi<MemoryBackend>,
) {
for &task in tasks {
self.with_task(task, |task| {
task.invalidate(self, turbo_tasks);
});
Expand Down
14 changes: 12 additions & 2 deletions crates/turbo-tasks-memory/src/memory_backend_with_pg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1030,10 +1030,20 @@ impl<P: PersistedGraph> Backend for MemoryBackendWithPersistedGraph<P> {

fn invalidate_tasks(
&self,
tasks: Vec<TaskId>,
tasks: &[TaskId],
turbo_tasks: &dyn TurboTasksBackendApi<MemoryBackendWithPersistedGraph<P>>,
) {
for task in tasks {
for &task in tasks {
self.invalidate_task(task, turbo_tasks);
}
}

fn invalidate_tasks_set(
&self,
tasks: &AutoSet<TaskId, BuildNoHashHasher<TaskId>>,
turbo_tasks: &dyn TurboTasksBackendApi<MemoryBackendWithPersistedGraph<P>>,
) {
for &task in tasks {
self.invalidate_task(task, turbo_tasks);
}
}
Expand Down
8 changes: 7 additions & 1 deletion crates/turbo-tasks/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::{

use anyhow::{anyhow, bail, Result};
use auto_hash_map::AutoSet;
use nohash_hasher::BuildNoHashHasher;
use serde::{Deserialize, Serialize};

pub use crate::id::BackendJobId;
Expand Down Expand Up @@ -192,7 +193,12 @@ pub trait Backend: Sync + Send {

fn invalidate_task(&self, task: TaskId, turbo_tasks: &dyn TurboTasksBackendApi<Self>);

fn invalidate_tasks(&self, tasks: Vec<TaskId>, turbo_tasks: &dyn TurboTasksBackendApi<Self>);
fn invalidate_tasks(&self, tasks: &[TaskId], turbo_tasks: &dyn TurboTasksBackendApi<Self>);
fn invalidate_tasks_set(
&self,
tasks: &AutoSet<TaskId, BuildNoHashHasher<TaskId>>,
turbo_tasks: &dyn TurboTasksBackendApi<Self>,
);

fn get_task_description(&self, task: TaskId) -> String;

Expand Down
9 changes: 4 additions & 5 deletions crates/turbo-tasks/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ impl<B: Backend + 'static> TurboTasks<B> {
let tasks = take(tasks_to_notify);
if !tasks.is_empty() {
let _guard = trace_span!("finish_current_task_state").entered();
self.backend.invalidate_tasks(tasks, self);
self.backend.invalidate_tasks(&tasks, self);
}
*stateful
})
Expand Down Expand Up @@ -903,7 +903,7 @@ impl<B: Backend + 'static> TurboTasksApi for TurboTasks<B> {
if tasks.is_empty() {
return;
}
self.backend.invalidate_tasks(tasks, self);
self.backend.invalidate_tasks(&tasks, self);
});
}

Expand Down Expand Up @@ -1096,7 +1096,7 @@ impl<B: Backend + 'static> TurboTasksBackendApi<B> for TurboTasks<B> {
});
if result.is_err() {
let _guard = trace_span!("schedule_notify_tasks", count = tasks.len()).entered();
self.backend.invalidate_tasks(tasks.to_vec(), self);
self.backend.invalidate_tasks(tasks, self);
}
}

Expand All @@ -1111,8 +1111,7 @@ impl<B: Backend + 'static> TurboTasksBackendApi<B> for TurboTasks<B> {
});
if result.is_err() {
let _guard = trace_span!("schedule_notify_tasks_set", count = tasks.len()).entered();
self.backend
.invalidate_tasks(tasks.iter().copied().collect(), self);
self.backend.invalidate_tasks_set(tasks, self);
};
}

Expand Down

0 comments on commit abba56e

Please sign in to comment.