diff --git a/crates/viewer/re_viewport/src/viewport.rs b/crates/viewer/re_viewport/src/viewport.rs index 2cdc2e8607b9..9e117f93c1ea 100644 --- a/crates/viewer/re_viewport/src/viewport.rs +++ b/crates/viewer/re_viewport/src/viewport.rs @@ -74,10 +74,15 @@ impl<'a> Viewport<'a> { // If the blueprint tree is empty/missing we need to auto-layout. let tree = if blueprint.tree.is_empty() { edited = true; - super::auto_layout::tree_from_space_views( + let auto_tree = super::auto_layout::tree_from_space_views( space_view_class_registry, &blueprint.space_views, - ) + ); + re_log::trace!( + "New auto-tree: {auto_tree:#?} based on {} space_views", + blueprint.space_views.len() + ); + auto_tree } else { blueprint.tree.clone() }; @@ -429,6 +434,8 @@ impl<'a> Viewport<'a> { if self.tree_edited { // TODO(#4687): Be extra careful here. If we mark edited inappropriately we can create an infinite edit loop. + re_log::trace!("Saving edited tree: {:#?}", self.tree); + // Simplify before we save the tree. Normally additional simplification will // happen on the next render loop, but that's too late -- unsimplified // changes will be baked into the tree. diff --git a/crates/viewer/re_viewport_blueprint/src/viewport_blueprint.rs b/crates/viewer/re_viewport_blueprint/src/viewport_blueprint.rs index 2b6e0e006c0f..ea0716a7543f 100644 --- a/crates/viewer/re_viewport_blueprint/src/viewport_blueprint.rs +++ b/crates/viewer/re_viewport_blueprint/src/viewport_blueprint.rs @@ -4,12 +4,12 @@ use std::sync::atomic::{AtomicBool, Ordering}; use ahash::HashMap; use egui_tiles::{SimplificationOptions, TileId}; use nohash_hasher::IntSet; -use re_types::{Archetype as _, SpaceViewClassIdentifier}; use smallvec::SmallVec; use re_chunk_store::LatestAtQuery; use re_entity_db::EntityPath; use re_types::blueprint::components::ViewerRecommendationHash; +use re_types::{Archetype as _, SpaceViewClassIdentifier}; use re_types_blueprint::blueprint::archetypes as blueprint_archetypes; use re_types_blueprint::blueprint::components::{ AutoLayout, AutoSpaceViews, IncludedSpaceView, RootContainer, SpaceViewMaximized, @@ -91,19 +91,28 @@ impl ViewportBlueprint { past_viewer_recommendations: results.component_batch(), }; - // This visits all space views that ever has been, which is likely more than exist now. - // TODO(emilk): optimize this by starting at the root and only visit reachable space viewa. - let all_space_view_ids: Vec = blueprint_db - .tree() - .children - .get(SpaceViewId::registry_part()) - .map(|tree| { - tree.children - .values() - .map(|subtree| SpaceViewId::from_entity_path(&subtree.path)) - .collect() - }) - .unwrap_or_default(); + let root_container = root_container.map(|id| id.0.into()); + + let mut containers: BTreeMap = Default::default(); + let mut all_space_view_ids: Vec = Default::default(); + + if let Some(root_container) = root_container { + re_tracing::profile_scope!("visit_all_containers"); + let mut container_ids_to_visit = vec![root_container]; + while let Some(id) = container_ids_to_visit.pop() { + if let Some(container) = ContainerBlueprint::try_from_db(blueprint_db, query, id) { + for &content in &container.contents { + match content { + Contents::Container(id) => container_ids_to_visit.push(id), + Contents::SpaceView(id) => { + all_space_view_ids.push(id); + } + } + } + containers.insert(id, container); + } + } + } let space_views: BTreeMap = all_space_view_ids .into_iter() @@ -113,26 +122,6 @@ impl ViewportBlueprint { .map(|sv| (sv.id, sv)) .collect(); - let all_container_ids: Vec = blueprint_db - .tree() - .children - .get(ContainerId::registry_part()) - .map(|tree| { - tree.children - .values() - .map(|subtree| ContainerId::from_entity_path(&subtree.path)) - .collect() - }) - .unwrap_or_default(); - - let containers: BTreeMap = all_container_ids - .into_iter() - .filter_map(|id| ContainerBlueprint::try_from_db(blueprint_db, query, id)) - .map(|c| (c.id, c)) - .collect(); - - let root_container = root_container.map(|id| id.0.into()); - // Auto layouting and auto space view are only enabled if no blueprint has been provided by the user. // Only enable auto-space-views if this is the app-default blueprint let is_app_default_blueprint = blueprint_db @@ -893,8 +882,10 @@ impl ViewportBlueprint { .and_then(|contents| contents.as_container_id()) .map(|container_id| RootContainer((container_id).into())) { + re_log::trace!("Saving with a root container"); ctx.save_blueprint_component(&VIEWPORT_PATH.into(), &root_container); } else { + re_log::trace!("Saving empty viewport"); ctx.save_empty_blueprint_component::(&VIEWPORT_PATH.into()); } }