-
Notifications
You must be signed in to change notification settings - Fork 27k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Turbopack] handle state serialization (#69670)
### What? State need some special handling for persistent caching. In particular we need to differ between two kind of states: * Persistent State: It will be stored in the persistent cache and value will be kept between builds. * Transient State: It's only valid for a session and will reset when restoring the persistent cache. We didn't have the separating before, so this PR introduces a new type `TransientState<T>` next to `State<T>` which handles transient state. The value will always be an `Option<T>` and resets to `None` when restoring the persistent cache. This also means all task that depend on transient state will be invalidated on restoring. Transient State can also be used when the value is not serializable. e. g. this is the case for the `last_successful_parse` state in ecmascript modules. We use that to avoid large structure changes to the module graph when introducing parse errors to modules. Using transient state for that means we only apply this optimization on a session, and it resets when restoring from persistent cache.
- Loading branch information
Showing
10 changed files
with
391 additions
and
73 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
turbopack/crates/turbo-tasks/src/serialization_invalidation.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
use std::{ | ||
hash::Hash, | ||
sync::{Arc, Weak}, | ||
}; | ||
|
||
use serde::{de::Visitor, Deserialize, Serialize}; | ||
use tokio::runtime::Handle; | ||
|
||
use crate::{manager::with_turbo_tasks, trace::TraceRawVcs, TaskId, TurboTasksApi}; | ||
|
||
pub struct SerializationInvalidator { | ||
task: TaskId, | ||
turbo_tasks: Weak<dyn TurboTasksApi>, | ||
handle: Handle, | ||
} | ||
|
||
impl Hash for SerializationInvalidator { | ||
fn hash<H: std::hash::Hasher>(&self, state: &mut H) { | ||
self.task.hash(state); | ||
} | ||
} | ||
|
||
impl PartialEq for SerializationInvalidator { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.task == other.task | ||
} | ||
} | ||
|
||
impl Eq for SerializationInvalidator {} | ||
|
||
impl SerializationInvalidator { | ||
pub fn invalidate(&self) { | ||
let SerializationInvalidator { | ||
task, | ||
turbo_tasks, | ||
handle, | ||
} = self; | ||
let _ = handle.enter(); | ||
if let Some(turbo_tasks) = turbo_tasks.upgrade() { | ||
turbo_tasks.invalidate_serialization(*task); | ||
} | ||
} | ||
|
||
pub(crate) fn new(task_id: TaskId) -> Self { | ||
Self { | ||
task: task_id, | ||
turbo_tasks: with_turbo_tasks(Arc::downgrade), | ||
handle: Handle::current(), | ||
} | ||
} | ||
} | ||
|
||
impl TraceRawVcs for SerializationInvalidator { | ||
fn trace_raw_vcs(&self, _context: &mut crate::trace::TraceRawVcsContext) { | ||
// nothing here | ||
} | ||
} | ||
|
||
impl Serialize for SerializationInvalidator { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: serde::Serializer, | ||
{ | ||
serializer.serialize_newtype_struct("SerializationInvalidator", &self.task) | ||
} | ||
} | ||
|
||
impl<'de> Deserialize<'de> for SerializationInvalidator { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: serde::Deserializer<'de>, | ||
{ | ||
struct V; | ||
|
||
impl<'de> Visitor<'de> for V { | ||
type Value = SerializationInvalidator; | ||
|
||
fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
write!(f, "an SerializationInvalidator") | ||
} | ||
|
||
fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error> | ||
where | ||
D: serde::Deserializer<'de>, | ||
{ | ||
Ok(SerializationInvalidator { | ||
task: TaskId::deserialize(deserializer)?, | ||
turbo_tasks: with_turbo_tasks(Arc::downgrade), | ||
handle: tokio::runtime::Handle::current(), | ||
}) | ||
} | ||
} | ||
deserializer.deserialize_newtype_struct("SerializationInvalidator", V) | ||
} | ||
} |
Oops, something went wrong.