Skip to content

Commit

Permalink
Replace lazy_static with OnceLock
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Dec 4, 2023
1 parent d74d3d3 commit e1c17d0
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 24 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 0 additions & 1 deletion crates/re_viewer_context/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 12 additions & 11 deletions crates/re_viewer_context/src/annotations.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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,
Expand All @@ -29,6 +30,14 @@ impl Annotations {
}
}

/// Fast access to an [`Arc`] sharing the same [`Annotations::missing`] instance.
pub fn missing_arc() -> Arc<Annotations> {
use std::sync::OnceLock;
static CELL: OnceLock<Arc<Annotations>> = OnceLock::new();
CELL.get_or_init(|| Arc::new(Annotations::missing()))
.clone()
}

pub fn try_from_view(view: &ArchetypeView<AnnotationContext>) -> Option<Self> {
re_tracing::profile_function!();

Expand Down Expand Up @@ -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<Annotations> {
let mut next_parent = Some(entity_path.clone());
while let Some(parent) = next_parent {
Expand All @@ -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<Annotations> = Arc::new(Annotations::missing());
}
1 change: 0 additions & 1 deletion crates/re_viewer_context/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
11 changes: 4 additions & 7 deletions crates/re_viewer_context/src/space_view/highlights.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use lazy_static::lazy_static;
use nohash_hasher::IntMap;

use re_log_types::EntityPathHash;
Expand Down Expand Up @@ -39,11 +38,6 @@ pub struct SpaceViewOutlineMasks {
pub instances: ahash::HashMap<InstanceKey, OutlineMaskPreference>,
}

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
Expand Down Expand Up @@ -72,9 +66,12 @@ impl SpaceViewHighlights {
}

pub fn entity_outline_mask(&self, entity_path_hash: EntityPathHash) -> &SpaceViewOutlineMasks {
use std::sync::OnceLock;
static CELL: OnceLock<SpaceViewOutlineMasks> = 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 {
Expand Down

0 comments on commit e1c17d0

Please sign in to comment.