Commit 47766d33 authored by Luke Sandberg's avatar Luke Sandberg

Shrink InnerTaskStorage

* move some values from dyanmic storage into the bitfields
    * `Stateful`, `HasInvalidator`, `Immutable`, `Dirty` and `CurrentSessionClean` are all moved into spare bits in `InnerStorage::state`
    * none of these are super common but this will reduce the length of the `DynamicStorage` vec

* refactor `CachedDataItem::CellData` to be slightly smaller by removing a redundant `ValueTypeId`
   * this complicates encoding/decoding a bit but overall shrinks the size of `CachedDataItem` from 40->32 bytes which will impact the size of the `DynamicStorage` vec.
parent a62b7d06
...@@ -563,7 +563,7 @@ dependencies = [ ...@@ -563,7 +563,7 @@ dependencies = [
[[package]] [[package]]
name = "bincode" name = "bincode"
version = "2.0.1" version = "2.0.1"
source = "git+https://github.com/bgw/bincode.git?branch=bgw%2Fpatches#19f09c5f6895d769883c10b3d374f761ab7fe83d" source = "git+https://github.com/lukesandberg/bincode.git?branch=customize_enum_variant#673ff4a740aacd7f5bf57d5f95f3967edf82d304"
dependencies = [ dependencies = [
"bincode_derive", "bincode_derive",
"serde", "serde",
...@@ -573,7 +573,7 @@ dependencies = [ ...@@ -573,7 +573,7 @@ dependencies = [
[[package]] [[package]]
name = "bincode_derive" name = "bincode_derive"
version = "2.0.1" version = "2.0.1"
source = "git+https://github.com/bgw/bincode.git?branch=bgw%2Fpatches#19f09c5f6895d769883c10b3d374f761ab7fe83d" source = "git+https://github.com/lukesandberg/bincode.git?branch=customize_enum_variant#673ff4a740aacd7f5bf57d5f95f3967edf82d304"
dependencies = [ dependencies = [
"virtue", "virtue",
] ]
......
...@@ -482,6 +482,6 @@ vergen-gitcl = { version = "1.0.8", features = ["cargo"] } ...@@ -482,6 +482,6 @@ vergen-gitcl = { version = "1.0.8", features = ["cargo"] }
webbrowser = "1.0.6" webbrowser = "1.0.6"
[patch.crates-io] [patch.crates-io]
bincode = { git = "https://github.com/bgw/bincode.git", branch = "bgw/patches" } bincode = { git = "https://github.com/lukesandberg/bincode.git", branch = "customize_enum_variant" }
virtue = { git = "https://github.com/bgw/virtue.git", branch = "bgw/fix-generic-default-parsing" } virtue = { git = "https://github.com/bgw/virtue.git", branch = "bgw/fix-generic-default-parsing" }
mdxjs = { git = "https://github.com/mischnic/mdxjs-rs.git", branch = "swc-core-32" } mdxjs = { git = "https://github.com/mischnic/mdxjs-rs.git", branch = "swc-core-32" }
...@@ -61,7 +61,7 @@ use crate::{ ...@@ -61,7 +61,7 @@ use crate::{
backing_storage::BackingStorage, backing_storage::BackingStorage,
data::{ data::{
ActivenessState, AggregationNumber, CachedDataItem, CachedDataItemKey, CachedDataItemType, ActivenessState, AggregationNumber, CachedDataItem, CachedDataItemKey, CachedDataItemType,
CachedDataItemValueRef, CellRef, CollectibleRef, CollectiblesRef, Dirtyness, CachedDataItemValueRef, CellRef, CollectibleRef, CollectiblesRef, Dirtiness,
InProgressCellState, InProgressState, InProgressStateInner, OutputValue, RootType, InProgressCellState, InProgressState, InProgressStateInner, OutputValue, RootType,
}, },
utils::{ utils::{
...@@ -2017,7 +2017,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -2017,7 +2017,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
// find all outdated data items (removed cells, outdated edges) // find all outdated data items (removed cells, outdated edges)
// Note: For persistent tasks we only want to call extract_if when there are actual cells to // Note: For persistent tasks we only want to call extract_if when there are actual cells to
// remove to avoid tracking that as modification. // remove to avoid tracking that as modification.
if task_id.is_transient() || iter_many!(task, CellData { cell } if task_id.is_transient() || iter_many!(task, CellData { cell, }
if cell_counters.get(&cell.type_id).is_none_or(|start_index| cell.index >= *start_index) => cell if cell_counters.get(&cell.type_id).is_none_or(|start_index| cell.index >= *start_index) => cell
).count() > 0 { ).count() > 0 {
removed_data.extend(task.extract_if(CachedDataItemType::CellData, |key, _| { removed_data.extend(task.extract_if(CachedDataItemType::CellData, |key, _| {
...@@ -2370,28 +2370,28 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -2370,28 +2370,28 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
)); ));
// Grab the old dirty state // Grab the old dirty state
let old_dirtyness = get!(task, Dirty).cloned(); let old_dirtiness = get!(task, Dirty).cloned();
let (old_self_dirty, old_current_session_self_clean) = match old_dirtyness { let (old_self_dirty, old_current_session_self_clean) = match old_dirtiness {
None => (false, false), None => (false, false),
Some(Dirtyness::Dirty) => (true, false), Some(Dirtiness::Dirty) => (true, false),
Some(Dirtyness::SessionDependent) => { Some(Dirtiness::SessionDependent) => {
let clean_in_current_session = get!(task, CurrentSessionClean).is_some(); let clean_in_current_session = get!(task, CurrentSessionClean).is_some();
(true, clean_in_current_session) (true, clean_in_current_session)
} }
}; };
// Compute the new dirty state // Compute the new dirty state
let (new_dirtyness, new_self_dirty, new_current_session_self_clean) = if session_dependent { let (new_dirtiness, new_self_dirty, new_current_session_self_clean) = if session_dependent {
(Some(Dirtyness::SessionDependent), true, true) (Some(Dirtiness::SessionDependent), true, true)
} else { } else {
(None, false, false) (None, false, false)
}; };
// Update the dirty state // Update the dirty state
if old_dirtyness != new_dirtyness { if old_dirtiness != new_dirtiness {
if let Some(value) = new_dirtyness { if let Some(value) = new_dirtiness {
task.insert(CachedDataItem::Dirty { value }); task.insert(CachedDataItem::Dirty { value });
} else if old_dirtyness.is_some() { } else if old_dirtiness.is_some() {
task.remove(&CachedDataItemKey::Dirty {}); task.remove(&CachedDataItemKey::Dirty {});
} }
} }
...@@ -2403,7 +2403,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -2403,7 +2403,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
} }
} }
// Propagate dirtyness changes // Propagate dirtiness changes
let data_update = if old_self_dirty != new_self_dirty let data_update = if old_self_dirty != new_self_dirty
|| old_current_session_self_clean != new_current_session_self_clean || old_current_session_self_clean != new_current_session_self_clean
{ {
......
...@@ -14,7 +14,7 @@ use crate::{ ...@@ -14,7 +14,7 @@ use crate::{
storage::{get, get_mut, remove}, storage::{get, get_mut, remove},
}, },
data::{ data::{
CachedDataItem, CachedDataItemKey, CachedDataItemValue, Dirtyness, InProgressState, CachedDataItem, CachedDataItemKey, CachedDataItemValue, Dirtiness, InProgressState,
InProgressStateInner, InProgressStateInner,
}, },
}; };
...@@ -232,11 +232,11 @@ pub fn make_task_dirty_internal( ...@@ -232,11 +232,11 @@ pub fn make_task_dirty_internal(
*stale = true; *stale = true;
} }
let old = task.insert(CachedDataItem::Dirty { let old = task.insert(CachedDataItem::Dirty {
value: Dirtyness::Dirty, value: Dirtiness::Dirty,
}); });
let (old_self_dirty, old_current_session_self_clean) = match old { let (old_self_dirty, old_current_session_self_clean) = match old {
Some(CachedDataItemValue::Dirty { Some(CachedDataItemValue::Dirty {
value: Dirtyness::Dirty, value: Dirtiness::Dirty,
}) => { }) => {
#[cfg(feature = "trace_task_dirty")] #[cfg(feature = "trace_task_dirty")]
let _span = tracing::trace_span!( let _span = tracing::trace_span!(
...@@ -250,7 +250,7 @@ pub fn make_task_dirty_internal( ...@@ -250,7 +250,7 @@ pub fn make_task_dirty_internal(
return; return;
} }
Some(CachedDataItemValue::Dirty { Some(CachedDataItemValue::Dirty {
value: Dirtyness::SessionDependent, value: Dirtiness::SessionDependent,
}) => { }) => {
// It was a session-dependent dirty before, so we need to remove that clean count // It was a session-dependent dirty before, so we need to remove that clean count
let was_current_session_clean = remove!(task, CurrentSessionClean).is_some(); let was_current_session_clean = remove!(task, CurrentSessionClean).is_some();
......
...@@ -27,7 +27,7 @@ use crate::{ ...@@ -27,7 +27,7 @@ use crate::{
backing_storage::BackingStorage, backing_storage::BackingStorage,
data::{ data::{
CachedDataItem, CachedDataItemKey, CachedDataItemType, CachedDataItemValue, CachedDataItem, CachedDataItemKey, CachedDataItemType, CachedDataItemValue,
CachedDataItemValueRef, CachedDataItemValueRefMut, Dirtyness, CachedDataItemValueRef, CachedDataItemValueRefMut, Dirtiness,
}, },
}; };
...@@ -409,16 +409,16 @@ pub trait TaskGuard: Debug { ...@@ -409,16 +409,16 @@ pub trait TaskGuard: Debug {
fn prefetch(&mut self) -> Option<FxIndexMap<TaskId, bool>>; fn prefetch(&mut self) -> Option<FxIndexMap<TaskId, bool>>;
fn is_immutable(&self) -> bool; fn is_immutable(&self) -> bool;
fn is_dirty(&self) -> bool { fn is_dirty(&self) -> bool {
get!(self, Dirty).is_some_and(|dirtyness| match dirtyness { get!(self, Dirty).is_some_and(|dirtiness| match dirtiness {
Dirtyness::Dirty => true, Dirtiness::Dirty => true,
Dirtyness::SessionDependent => get!(self, CurrentSessionClean).is_none(), Dirtiness::SessionDependent => get!(self, CurrentSessionClean).is_none(),
}) })
} }
fn dirtyness_and_session(&self) -> Option<(Dirtyness, bool)> { fn dirtiness_and_session(&self) -> Option<(Dirtiness, bool)> {
match get!(self, Dirty)? { match get!(self, Dirty)? {
Dirtyness::Dirty => Some((Dirtyness::Dirty, false)), Dirtiness::Dirty => Some((Dirtiness::Dirty, false)),
Dirtyness::SessionDependent => Some(( Dirtiness::SessionDependent => Some((
Dirtyness::SessionDependent, Dirtiness::SessionDependent,
get!(self, CurrentSessionClean).is_some(), get!(self, CurrentSessionClean).is_some(),
)), )),
} }
...@@ -427,8 +427,8 @@ pub trait TaskGuard: Debug { ...@@ -427,8 +427,8 @@ pub trait TaskGuard: Debug {
fn dirty(&self) -> (bool, bool) { fn dirty(&self) -> (bool, bool) {
match get!(self, Dirty) { match get!(self, Dirty) {
None => (false, false), None => (false, false),
Some(Dirtyness::Dirty) => (true, false), Some(Dirtiness::Dirty) => (true, false),
Some(Dirtyness::SessionDependent) => (true, get!(self, CurrentSessionClean).is_some()), Some(Dirtiness::SessionDependent) => (true, get!(self, CurrentSessionClean).is_some()),
} }
} }
fn dirty_containers(&self) -> impl Iterator<Item = TaskId> { fn dirty_containers(&self) -> impl Iterator<Item = TaskId> {
...@@ -470,22 +470,24 @@ pub trait TaskGuard: Debug { ...@@ -470,22 +470,24 @@ pub trait TaskGuard: Debug {
is_serializable_cell_content: bool, is_serializable_cell_content: bool,
cell: CellId, cell: CellId,
) -> Option<TypedSharedReference> { ) -> Option<TypedSharedReference> {
if is_serializable_cell_content { let reference = if is_serializable_cell_content {
remove!(self, CellData { cell }) remove!(self, CellData { cell })
} else { } else {
remove!(self, TransientCellData { cell }).map(|sr| sr.into_typed(cell.type_id)) remove!(self, TransientCellData { cell })
} };
reference.map(|sr| sr.into_typed(cell.type_id))
} }
fn get_cell_data( fn get_cell_data(
&self, &self,
is_serializable_cell_content: bool, is_serializable_cell_content: bool,
cell: CellId, cell: CellId,
) -> Option<TypedSharedReference> { ) -> Option<TypedSharedReference> {
if is_serializable_cell_content { let reference = if is_serializable_cell_content {
get!(self, CellData { cell }).cloned() get!(self, CellData { cell })
} else { } else {
get!(self, TransientCellData { cell }).map(|sr| sr.clone().into_typed(cell.type_id)) get!(self, TransientCellData { cell })
} };
reference.map(|sr| sr.clone().into_typed(cell.type_id))
} }
fn has_cell_data(&self, is_serializable_cell_content: bool, cell: CellId) -> bool { fn has_cell_data(&self, is_serializable_cell_content: bool, cell: CellId) -> bool {
if is_serializable_cell_content { if is_serializable_cell_content {
......
...@@ -141,7 +141,7 @@ transient_traits!(ActivenessState); ...@@ -141,7 +141,7 @@ transient_traits!(ActivenessState);
impl Eq for ActivenessState {} impl Eq for ActivenessState {}
#[derive(Debug, Clone, Copy, Encode, Decode, PartialEq, Eq)] #[derive(Debug, Clone, Copy, Encode, Decode, PartialEq, Eq)]
pub enum Dirtyness { pub enum Dirtiness {
Dirty, Dirty,
SessionDependent, SessionDependent,
} }
...@@ -225,7 +225,7 @@ pub enum CachedDataItem { ...@@ -225,7 +225,7 @@ pub enum CachedDataItem {
// State // State
Dirty { Dirty {
value: Dirtyness, value: Dirtiness,
}, },
CurrentSessionClean { CurrentSessionClean {
// TODO: bgw: Add a way to skip the entire enum variant in bincode (generating an error // TODO: bgw: Add a way to skip the entire enum variant in bincode (generating an error
...@@ -241,9 +241,10 @@ pub enum CachedDataItem { ...@@ -241,9 +241,10 @@ pub enum CachedDataItem {
}, },
// Cells // Cells
#[bincode(with = "cell_data_bincode")]
CellData { CellData {
cell: CellId, cell: CellId,
value: TypedSharedReference, value: SharedReference,
}, },
TransientCellData { TransientCellData {
#[bincode(skip, default = "unreachable_decode")] #[bincode(skip, default = "unreachable_decode")]
...@@ -376,6 +377,45 @@ pub enum CachedDataItem { ...@@ -376,6 +377,45 @@ pub enum CachedDataItem {
}, },
} }
// Customized encoding/decoding for CellData
// This allows us to serialize a SharedReference leveraging the type_id from the cell_id
mod cell_data_bincode {
use bincode::{Decode, Encode};
use turbo_tasks::{CellId, SharedReference, TypedSharedReference};
use super::CachedDataItem;
pub fn encode<E: bincode::enc::Encoder>(
cell_id: &CellId,
value: &SharedReference,
encoder: &mut E,
) -> Result<(), bincode::error::EncodeError> {
Encode::encode(&cell_id.index, encoder)?;
// This clone seems unnecessary. We would need to inline TypedSharedReference encode to
// remove it.
value.clone().into_typed(cell_id.type_id).encode(encoder)
}
pub fn decode<Context, D: bincode::de::Decoder<Context = Context>>(
decoder: &mut D,
) -> Result<CachedDataItem, bincode::error::DecodeError> {
let index = <u32 as Decode<Context>>::decode(decoder)?;
let reference = TypedSharedReference::decode(decoder)?;
Ok(CachedDataItem::CellData {
cell: CellId {
type_id: reference.type_id,
index,
},
value: reference.into_untyped(),
})
}
pub fn borrow_decode<'de, Context, D: bincode::de::BorrowDecoder<'de, Context = Context>>(
decoder: &mut D,
) -> Result<CachedDataItem, bincode::error::DecodeError> {
decode(decoder)
}
}
fn unreachable_decode<T>() -> T { fn unreachable_decode<T>() -> T {
unreachable!("CachedDataItem variant should not have been encoded, cannot decode") unreachable!("CachedDataItem variant should not have been encoded, cannot decode")
} }
...@@ -387,7 +427,10 @@ impl CachedDataItem { ...@@ -387,7 +427,10 @@ impl CachedDataItem {
value: TypedSharedReference, value: TypedSharedReference,
) -> Self { ) -> Self {
if is_serializable_cell_content { if is_serializable_cell_content {
CachedDataItem::CellData { cell, value } CachedDataItem::CellData {
cell,
value: value.into_untyped(),
}
} else { } else {
CachedDataItem::TransientCellData { CachedDataItem::TransientCellData {
cell, cell,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment