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 = [
[[package]]
name = "bincode"
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 = [
"bincode_derive",
"serde",
......@@ -573,7 +573,7 @@ dependencies = [
[[package]]
name = "bincode_derive"
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 = [
"virtue",
]
......
......@@ -482,6 +482,6 @@ vergen-gitcl = { version = "1.0.8", features = ["cargo"] }
webbrowser = "1.0.6"
[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" }
mdxjs = { git = "https://github.com/mischnic/mdxjs-rs.git", branch = "swc-core-32" }
......@@ -61,7 +61,7 @@ use crate::{
backing_storage::BackingStorage,
data::{
ActivenessState, AggregationNumber, CachedDataItem, CachedDataItemKey, CachedDataItemType,
CachedDataItemValueRef, CellRef, CollectibleRef, CollectiblesRef, Dirtyness,
CachedDataItemValueRef, CellRef, CollectibleRef, CollectiblesRef, Dirtiness,
InProgressCellState, InProgressState, InProgressStateInner, OutputValue, RootType,
},
utils::{
......@@ -2017,7 +2017,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
// 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
// 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
).count() > 0 {
removed_data.extend(task.extract_if(CachedDataItemType::CellData, |key, _| {
......@@ -2370,28 +2370,28 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
));
// Grab the old dirty state
let old_dirtyness = get!(task, Dirty).cloned();
let (old_self_dirty, old_current_session_self_clean) = match old_dirtyness {
let old_dirtiness = get!(task, Dirty).cloned();
let (old_self_dirty, old_current_session_self_clean) = match old_dirtiness {
None => (false, false),
Some(Dirtyness::Dirty) => (true, false),
Some(Dirtyness::SessionDependent) => {
Some(Dirtiness::Dirty) => (true, false),
Some(Dirtiness::SessionDependent) => {
let clean_in_current_session = get!(task, CurrentSessionClean).is_some();
(true, clean_in_current_session)
}
};
// Compute the new dirty state
let (new_dirtyness, new_self_dirty, new_current_session_self_clean) = if session_dependent {
(Some(Dirtyness::SessionDependent), true, true)
let (new_dirtiness, new_self_dirty, new_current_session_self_clean) = if session_dependent {
(Some(Dirtiness::SessionDependent), true, true)
} else {
(None, false, false)
};
// Update the dirty state
if old_dirtyness != new_dirtyness {
if let Some(value) = new_dirtyness {
if old_dirtiness != new_dirtiness {
if let Some(value) = new_dirtiness {
task.insert(CachedDataItem::Dirty { value });
} else if old_dirtyness.is_some() {
} else if old_dirtiness.is_some() {
task.remove(&CachedDataItemKey::Dirty {});
}
}
......@@ -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
|| old_current_session_self_clean != new_current_session_self_clean
{
......
......@@ -14,7 +14,7 @@ use crate::{
storage::{get, get_mut, remove},
},
data::{
CachedDataItem, CachedDataItemKey, CachedDataItemValue, Dirtyness, InProgressState,
CachedDataItem, CachedDataItemKey, CachedDataItemValue, Dirtiness, InProgressState,
InProgressStateInner,
},
};
......@@ -232,11 +232,11 @@ pub fn make_task_dirty_internal(
*stale = true;
}
let old = task.insert(CachedDataItem::Dirty {
value: Dirtyness::Dirty,
value: Dirtiness::Dirty,
});
let (old_self_dirty, old_current_session_self_clean) = match old {
Some(CachedDataItemValue::Dirty {
value: Dirtyness::Dirty,
value: Dirtiness::Dirty,
}) => {
#[cfg(feature = "trace_task_dirty")]
let _span = tracing::trace_span!(
......@@ -250,7 +250,7 @@ pub fn make_task_dirty_internal(
return;
}
Some(CachedDataItemValue::Dirty {
value: Dirtyness::SessionDependent,
value: Dirtiness::SessionDependent,
}) => {
// 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();
......
......@@ -27,7 +27,7 @@ use crate::{
backing_storage::BackingStorage,
data::{
CachedDataItem, CachedDataItemKey, CachedDataItemType, CachedDataItemValue,
CachedDataItemValueRef, CachedDataItemValueRefMut, Dirtyness,
CachedDataItemValueRef, CachedDataItemValueRefMut, Dirtiness,
},
};
......@@ -409,16 +409,16 @@ pub trait TaskGuard: Debug {
fn prefetch(&mut self) -> Option<FxIndexMap<TaskId, bool>>;
fn is_immutable(&self) -> bool;
fn is_dirty(&self) -> bool {
get!(self, Dirty).is_some_and(|dirtyness| match dirtyness {
Dirtyness::Dirty => true,
Dirtyness::SessionDependent => get!(self, CurrentSessionClean).is_none(),
get!(self, Dirty).is_some_and(|dirtiness| match dirtiness {
Dirtiness::Dirty => true,
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)? {
Dirtyness::Dirty => Some((Dirtyness::Dirty, false)),
Dirtyness::SessionDependent => Some((
Dirtyness::SessionDependent,
Dirtiness::Dirty => Some((Dirtiness::Dirty, false)),
Dirtiness::SessionDependent => Some((
Dirtiness::SessionDependent,
get!(self, CurrentSessionClean).is_some(),
)),
}
......@@ -427,8 +427,8 @@ pub trait TaskGuard: Debug {
fn dirty(&self) -> (bool, bool) {
match get!(self, Dirty) {
None => (false, false),
Some(Dirtyness::Dirty) => (true, false),
Some(Dirtyness::SessionDependent) => (true, get!(self, CurrentSessionClean).is_some()),
Some(Dirtiness::Dirty) => (true, false),
Some(Dirtiness::SessionDependent) => (true, get!(self, CurrentSessionClean).is_some()),
}
}
fn dirty_containers(&self) -> impl Iterator<Item = TaskId> {
......@@ -470,22 +470,24 @@ pub trait TaskGuard: Debug {
is_serializable_cell_content: bool,
cell: CellId,
) -> Option<TypedSharedReference> {
if is_serializable_cell_content {
let reference = if is_serializable_cell_content {
remove!(self, CellData { cell })
} 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(
&self,
is_serializable_cell_content: bool,
cell: CellId,
) -> Option<TypedSharedReference> {
if is_serializable_cell_content {
get!(self, CellData { cell }).cloned()
let reference = if is_serializable_cell_content {
get!(self, CellData { cell })
} 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 {
if is_serializable_cell_content {
......
......@@ -141,7 +141,7 @@ transient_traits!(ActivenessState);
impl Eq for ActivenessState {}
#[derive(Debug, Clone, Copy, Encode, Decode, PartialEq, Eq)]
pub enum Dirtyness {
pub enum Dirtiness {
Dirty,
SessionDependent,
}
......@@ -225,7 +225,7 @@ pub enum CachedDataItem {
// State
Dirty {
value: Dirtyness,
value: Dirtiness,
},
CurrentSessionClean {
// TODO: bgw: Add a way to skip the entire enum variant in bincode (generating an error
......@@ -241,9 +241,10 @@ pub enum CachedDataItem {
},
// Cells
#[bincode(with = "cell_data_bincode")]
CellData {
cell: CellId,
value: TypedSharedReference,
value: SharedReference,
},
TransientCellData {
#[bincode(skip, default = "unreachable_decode")]
......@@ -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 {
unreachable!("CachedDataItem variant should not have been encoded, cannot decode")
}
......@@ -387,7 +427,10 @@ impl CachedDataItem {
value: TypedSharedReference,
) -> Self {
if is_serializable_cell_content {
CachedDataItem::CellData { cell, value }
CachedDataItem::CellData {
cell,
value: value.into_untyped(),
}
} else {
CachedDataItem::TransientCellData {
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