From e1c17d0a8ee92e802afafdcb9a36052a42902c4d Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Mon, 4 Dec 2023 16:12:46 +0100 Subject: [PATCH] Replace `lazy_static` with `OnceLock` --- Cargo.lock | 1 - Cargo.toml | 6 ++--- crates/re_viewer_context/Cargo.toml | 1 - crates/re_viewer_context/src/annotations.rs | 23 ++++++++++--------- crates/re_viewer_context/src/lib.rs | 1 - .../src/space_view/highlights.rs | 11 ++++----- 6 files changed, 19 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a357d6c34d93..17ab91b06cb6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5157,7 +5157,6 @@ dependencies = [ "glam", "half 2.3.1", "itertools 0.11.0", - "lazy_static", "macaw", "ndarray", "nohash-hasher", diff --git a/Cargo.toml b/Cargo.toml index 55ec62bf145e..5d48d3053424 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -132,11 +132,11 @@ half = "2.3.1" hyper = "0.14" image = { version = "0.24", default-features = false } indent = "0.1" -indicatif = "0.17.7" -infer = "0.15" # infer MIME type by checking the magic number signature +indicatif = "0.17.7" # Progress bar +infer = "0.15" # infer MIME type by checking the magic number signaturefer MIME type by checking the magic number signature itertools = "0.11" js-sys = "0.3" -lazy_static = "1.4" +# No lazy_static - use `std::sync::OnceLock` or `once_cell` instead lz4_flex = "0.11" log = "0.4" log-once = "0.4" diff --git a/crates/re_viewer_context/Cargo.toml b/crates/re_viewer_context/Cargo.toml index 4c744d1e920c..2d20cf1a4cbe 100644 --- a/crates/re_viewer_context/Cargo.toml +++ b/crates/re_viewer_context/Cargo.toml @@ -37,7 +37,6 @@ egui_tiles.workspace = true glam.workspace = true half.workspace = true itertools.workspace = true -lazy_static.workspace = true macaw.workspace = true ndarray.workspace = true nohash-hasher.workspace = true diff --git a/crates/re_viewer_context/src/annotations.rs b/crates/re_viewer_context/src/annotations.rs index 438ec62f7177..1fc70d7b2109 100644 --- a/crates/re_viewer_context/src/annotations.rs +++ b/crates/re_viewer_context/src/annotations.rs @@ -1,7 +1,6 @@ use std::{collections::BTreeMap, sync::Arc}; use ahash::HashMap; -use lazy_static::lazy_static; use nohash_hasher::IntSet; use re_arrow_store::LatestAtQuery; @@ -14,6 +13,8 @@ use re_types::datatypes::{AnnotationInfo, ClassDescription, ClassId, KeypointId} use super::{auto_color, ViewerContext}; use crate::DefaultColor; +const MISSING_ROW_ID: RowId = RowId::ZERO; + #[derive(Clone, Debug)] pub struct Annotations { row_id: RowId, @@ -29,6 +30,14 @@ impl Annotations { } } + /// Fast access to an [`Arc`] sharing the same [`Annotations::missing`] instance. + pub fn missing_arc() -> Arc { + use std::sync::OnceLock; + static CELL: OnceLock> = OnceLock::new(); + CELL.get_or_init(|| Arc::new(Annotations::missing())) + .clone() + } + pub fn try_from_view(view: &ArchetypeView) -> Option { re_tracing::profile_function!(); @@ -273,7 +282,7 @@ impl AnnotationMap { } // Search through the all prefixes of this entity path until we find a - // matching annotation. If we find nothing return the default [`MISSING_ANNOTATIONS`]. + // matching annotation. If we find nothing return the default [`Annotations::missing_arc`]. pub fn find(&self, entity_path: &EntityPath) -> Arc { let mut next_parent = Some(entity_path.clone()); while let Some(parent) = next_parent { @@ -285,14 +294,6 @@ impl AnnotationMap { } // Otherwise return the missing legend - Arc::clone(&MISSING_ANNOTATIONS) + Annotations::missing_arc() } } - -// --- - -const MISSING_ROW_ID: RowId = RowId::ZERO; - -lazy_static! { - pub static ref MISSING_ANNOTATIONS: Arc = Arc::new(Annotations::missing()); -} diff --git a/crates/re_viewer_context/src/lib.rs b/crates/re_viewer_context/src/lib.rs index aeaa5c76e93c..3f963d729144 100644 --- a/crates/re_viewer_context/src/lib.rs +++ b/crates/re_viewer_context/src/lib.rs @@ -24,7 +24,6 @@ pub mod gpu_bridge; pub use annotations::{ AnnotationMap, Annotations, ResolvedAnnotationInfo, ResolvedAnnotationInfos, - MISSING_ANNOTATIONS, }; pub use app_options::AppOptions; pub use blueprint_id::{DataQueryId, SpaceViewId}; diff --git a/crates/re_viewer_context/src/space_view/highlights.rs b/crates/re_viewer_context/src/space_view/highlights.rs index 4455a998ac63..2eba944a13f1 100644 --- a/crates/re_viewer_context/src/space_view/highlights.rs +++ b/crates/re_viewer_context/src/space_view/highlights.rs @@ -1,4 +1,3 @@ -use lazy_static::lazy_static; use nohash_hasher::IntMap; use re_log_types::EntityPathHash; @@ -39,11 +38,6 @@ pub struct SpaceViewOutlineMasks { pub instances: ahash::HashMap, } -lazy_static! { - static ref SPACEVIEW_OUTLINE_MASK_NONE: SpaceViewOutlineMasks = - SpaceViewOutlineMasks::default(); -} - impl SpaceViewOutlineMasks { pub fn index_outline_mask(&self, instance_key: InstanceKey) -> OutlineMaskPreference { self.instances @@ -72,9 +66,12 @@ impl SpaceViewHighlights { } pub fn entity_outline_mask(&self, entity_path_hash: EntityPathHash) -> &SpaceViewOutlineMasks { + use std::sync::OnceLock; + static CELL: OnceLock = OnceLock::new(); + self.outlines_masks .get(&entity_path_hash) - .unwrap_or(&SPACEVIEW_OUTLINE_MASK_NONE) + .unwrap_or_else(|| CELL.get_or_init(SpaceViewOutlineMasks::default)) } pub fn any_outlines(&self) -> bool {