use cranpose_core::collections::growth_slack::GrowthSlack;
use super::SceneStore;
const REBUILT_FREE_ROWS: usize = 2048;
pub(super) fn trim(store: &mut SceneStore) -> bool {
if 2 * store.free_rows.len() > store.tree.len() && cut_free_rows(store) {
cut_free_tails(store);
}
if 2 * store.free_rows.len() > store.tree.len() && store.free_rows.len() > REBUILT_FREE_ROWS {
return true;
}
store
.items
.pack(store.tree.iter_mut().map(|row| &mut row.items));
store.lists.shrink_to(store.items.len());
#[cfg(feature = "window-damage")]
store
.marks
.pool
.pack(store.warm.iter_mut().map(|row| &mut row.own_marks));
store.trim_shapes();
let records = &store.node_records;
store.recordings.trim(|node| records.get(node).is_some());
store.journal.trim();
store.tree.trim_growth_slack();
store.geo.trim_growth_slack();
store.warm.trim_growth_slack();
store.generations.trim_growth_slack();
store.node_records.trim_growth_slack();
store.facts.trim_growth_slack();
store.items.trim_growth_slack();
#[cfg(feature = "window-damage")]
store.marks.pool.trim_growth_slack();
store.props.trim_growth_slack();
store.graphics_layers.trim_growth_slack();
store.transforms.trim_growth_slack();
store.draws.trim_growth_slack();
store.texts.trim_growth_slack();
store.runs.trim_growth_slack();
store.shape_runs.trim_growth_slack();
false
}
fn cut_free_tails(store: &mut SceneStore) {
store.props.cut_free_tail();
store.graphics_layers.cut_free_tail();
store.transforms.cut_free_tail();
store.draws.cut_free_tail();
store.texts.cut_free_tail();
store.runs.cut_free_tail();
store.shape_runs.cut_free_tail();
}
fn cut_free_rows(store: &mut SceneStore) -> bool {
let rows = store.tree.len();
let mut keep = rows;
store.free_rows.sort_unstable();
while store
.free_rows
.last()
.is_some_and(|&row| row as usize + 1 == keep)
{
store.free_rows.pop();
keep -= 1;
}
store.free_rows.reverse();
if keep == rows {
return false;
}
if let Some(&last) = store.generations[keep..].iter().max() {
store.generation_floor = store.generation_floor.max(last);
}
store.free_rows.trim_growth_slack();
store.tree.truncate(keep);
store.geo.truncate(keep);
store.warm.truncate(keep);
store.generations.truncate(keep);
store.facts.truncate_rows(keep);
true
}