Skip to content

Commit 1dab38b

Browse files
authored
Implement sizebytes for things in the tf transform cache (#12038)
1 parent 4f63242 commit 1dab38b

File tree

9 files changed

+130
-8
lines changed

9 files changed

+130
-8
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8520,6 +8520,7 @@ name = "re_byte_size"
85208520
version = "0.28.0-alpha.1+dev"
85218521
dependencies = [
85228522
"arrow",
8523+
"glam",
85238524
"half",
85248525
"smallvec",
85258526
]
@@ -9745,6 +9746,7 @@ dependencies = [
97459746
"nohash-hasher",
97469747
"parking_lot",
97479748
"re_arrow_util",
9749+
"re_byte_size",
97489750
"re_chunk_store",
97499751
"re_entity_db",
97509752
"re_log",

crates/store/re_tf/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ all-features = true
2121

2222
[dependencies]
2323
re_arrow_util.workspace = true
24+
re_byte_size = { workspace = true, features = ["glam"] }
2425
re_chunk_store.workspace = true
2526
re_entity_db.workspace = true # It would be nice not to depend on this, but we need this in order to do queries right now.
2627
re_log.workspace = true

crates/store/re_tf/src/frame_id_registry.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use std::collections::hash_map::Entry;
22

33
use nohash_hasher::IntMap;
4+
use re_byte_size::SizeBytes;
45
use re_log_types::EntityPath;
5-
use re_types::components::TransformFrameId;
6-
use re_types::{TransformFrameIdHash, archetypes};
6+
use re_types::{TransformFrameIdHash, archetypes, components::TransformFrameId};
77

88
/// Frame id registry for resolving frame id hashes back to frame ids.
99
pub struct FrameIdRegistry {
@@ -25,6 +25,16 @@ impl Default for FrameIdRegistry {
2525
}
2626
}
2727

28+
impl SizeBytes for FrameIdRegistry {
29+
fn heap_size_bytes(&self) -> u64 {
30+
let Self {
31+
frame_id_lookup_table,
32+
} = self;
33+
34+
frame_id_lookup_table.total_size_bytes()
35+
}
36+
}
37+
2838
impl FrameIdRegistry {
2939
/// Looks up a frame ID by its hash.
3040
///

crates/store/re_tf/src/transform_resolution_cache.rs

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use glam::DAffine3;
55
use itertools::{Either, izip};
66
use nohash_hasher::IntMap;
77
use parking_lot::Mutex;
8+
use re_byte_size::SizeBytes;
89

910
use crate::frame_id_registry::FrameIdRegistry;
1011
use crate::{
@@ -17,8 +18,7 @@ use crate::{
1718
};
1819

1920
use re_arrow_util::ArrowArrayDowncastRef as _;
20-
use re_chunk_store::external::arrow;
21-
use re_chunk_store::{Chunk, LatestAtQuery};
21+
use re_chunk_store::{Chunk, LatestAtQuery, external::arrow};
2222
use re_entity_db::EntityDb;
2323
use re_log_types::{EntityPath, TimeInt, TimelineName};
2424
use re_types::{ComponentIdentifier, archetypes, components};
@@ -61,6 +61,20 @@ impl Default for TransformResolutionCache {
6161
}
6262
}
6363

64+
impl SizeBytes for TransformResolutionCache {
65+
fn heap_size_bytes(&self) -> u64 {
66+
let Self {
67+
frame_id_registry,
68+
per_timeline,
69+
static_timeline,
70+
} = self;
71+
72+
frame_id_registry.heap_size_bytes()
73+
+ per_timeline.heap_size_bytes()
74+
+ static_timeline.heap_size_bytes()
75+
}
76+
}
77+
6478
/// A transform from a child frame to a parent frame.
6579
#[derive(Clone, Debug, PartialEq)]
6680
pub struct ParentFromChildTransform {
@@ -71,6 +85,14 @@ pub struct ParentFromChildTransform {
7185
pub transform: DAffine3,
7286
}
7387

88+
impl SizeBytes for ParentFromChildTransform {
89+
fn heap_size_bytes(&self) -> u64 {
90+
let Self { parent, transform } = self;
91+
92+
parent.heap_size_bytes() + transform.heap_size_bytes()
93+
}
94+
}
95+
7496
/// Cached transforms for a single timeline.
7597
///
7698
/// Includes any static transforms that may apply globally.
@@ -243,6 +265,20 @@ impl CachedTransformsForTimeline {
243265
}
244266
}
245267

268+
impl SizeBytes for CachedTransformsForTimeline {
269+
fn heap_size_bytes(&self) -> u64 {
270+
let Self {
271+
per_child_frame_transforms,
272+
non_recursive_clears,
273+
recursive_clears,
274+
} = self;
275+
276+
per_child_frame_transforms.heap_size_bytes()
277+
+ non_recursive_clears.heap_size_bytes()
278+
+ recursive_clears.heap_size_bytes()
279+
}
280+
}
281+
246282
#[derive(Clone, Debug, PartialEq)]
247283
enum CachedTransformValue<T> {
248284
/// Cache is invalidated, we don't know what state we're in.
@@ -255,6 +291,15 @@ enum CachedTransformValue<T> {
255291
Cleared,
256292
}
257293

294+
impl<T: SizeBytes> SizeBytes for CachedTransformValue<T> {
295+
fn heap_size_bytes(&self) -> u64 {
296+
match self {
297+
Self::Resident(item) => item.heap_size_bytes(),
298+
Self::Invalidated | Self::Cleared => 0,
299+
}
300+
}
301+
}
302+
258303
type FrameTransformTimeMap = BTreeMap<TimeInt, CachedTransformValue<ParentFromChildTransform>>;
259304

260305
type PoseTransformTimeMap = BTreeMap<TimeInt, CachedTransformValue<Vec<DAffine3>>>;
@@ -344,6 +389,20 @@ impl TransformsForChildFrameEvents {
344389
}
345390
}
346391

392+
impl SizeBytes for TransformsForChildFrameEvents {
393+
fn heap_size_bytes(&self) -> u64 {
394+
let Self {
395+
frame_transforms,
396+
pose_transforms,
397+
pinhole_projections,
398+
} = self;
399+
400+
frame_transforms.heap_size_bytes()
401+
+ pose_transforms.heap_size_bytes()
402+
+ pinhole_projections.heap_size_bytes()
403+
}
404+
}
405+
347406
/// Cached transforms from a single child frame to a (potentially changing) parent frame over time.
348407
///
349408
/// Incorporates any static transforms that may apply to this entity.
@@ -391,6 +450,23 @@ impl PartialEq for TransformsForChildFrame {
391450
}
392451
}
393452

453+
impl SizeBytes for TransformsForChildFrame {
454+
fn heap_size_bytes(&self) -> u64 {
455+
let Self {
456+
associated_entity_path,
457+
child_frame,
458+
events,
459+
460+
#[cfg(debug_assertions)]
461+
timeline: _,
462+
} = self;
463+
464+
associated_entity_path.heap_size_bytes()
465+
+ child_frame.heap_size_bytes()
466+
+ events.lock().heap_size_bytes()
467+
}
468+
}
469+
394470
fn add_invalidated_entry_if_not_already_cleared<T: PartialEq>(
395471
transforms: &mut BTreeMap<TimeInt, CachedTransformValue<T>>,
396472
time: TimeInt,
@@ -424,6 +500,22 @@ pub struct ResolvedPinholeProjection {
424500
pub view_coordinates: components::ViewCoordinates,
425501
}
426502

503+
impl SizeBytes for ResolvedPinholeProjection {
504+
fn heap_size_bytes(&self) -> u64 {
505+
let Self {
506+
parent,
507+
image_from_camera,
508+
resolution,
509+
view_coordinates,
510+
} = self;
511+
512+
parent.heap_size_bytes()
513+
+ image_from_camera.heap_size_bytes()
514+
+ resolution.heap_size_bytes()
515+
+ view_coordinates.heap_size_bytes()
516+
}
517+
}
518+
427519
impl TransformsForChildFrame {
428520
fn new(
429521
associated_entity_path: EntityPath,

crates/store/re_types/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ ecolor = ["dep:ecolor"]
3434
egui_plot = ["dep:egui_plot"]
3535

3636
## Add support for some math operations using [`glam`](https://crates.io/crates/glam/) and [`macaw`](https://crates.io/crates/macaw/).
37-
glam = ["dep:glam", "dep:macaw"]
37+
glam = ["dep:glam", "dep:macaw", "re_byte_size/glam"]
3838

3939
## Integration with the [`image`](https://crates.io/crates/image/) crate, plus JPEG and TIFF support.
4040
image = ["dep:ecolor", "dep:image", "dep:tiff"]

crates/top/rerun/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ ecolor = ["re_types?/ecolor"]
6666

6767
## Add support for some math operations using [`glam`](https://crates.io/crates/glam/).
6868
## Only relevant if feature `sdk` is enabled.
69-
glam = ["re_types?/glam"]
69+
glam = ["re_types?/glam", "re_byte_size/glam"]
7070

7171
## Integration with the [`image`](https://crates.io/crates/image/) crate, plus JPEG support.
7272
image = ["re_types?/image"]

crates/utils/re_byte_size/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ all-features = true
2020

2121

2222
[features]
23+
glam = ["dep:glam"]
2324

2425

2526
[dependencies]
2627
# TODO(emilk): make some of these opt-in
2728
arrow.workspace = true
2829
half.workspace = true
2930
smallvec.workspace = true
31+
glam = { workspace = true, optional = true }

crates/utils/re_byte_size/src/primitive_sizes.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@ impl_size_bytes_pod!(
2828
u8, u16, u32, u64, u128, i8, i16, i32, i64, i128, bool, f32, f64
2929
);
3030
impl_size_bytes_pod!(half::f16);
31+
32+
#[cfg(feature = "glam")]
33+
impl_size_bytes_pod!(glam::DAffine3);

crates/viewer/re_viewer_context/src/cache/transform_database_store.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use parking_lot::{ArcMutexGuard, Mutex, RawMutex};
2+
use re_byte_size::SizeBytes;
23
use std::sync::Arc;
34

45
use super::{Cache, CacheMemoryReport};
@@ -34,6 +35,18 @@ impl TransformDatabaseStoreCache {
3435
}
3536
}
3637

38+
impl SizeBytes for TransformDatabaseStoreCache {
39+
fn heap_size_bytes(&self) -> u64 {
40+
let Self {
41+
initialized,
42+
transform_cache,
43+
} = self;
44+
let cache = transform_cache.lock();
45+
46+
initialized.heap_size_bytes() + cache.total_size_bytes()
47+
}
48+
}
49+
3750
impl Cache for TransformDatabaseStoreCache {
3851
fn purge_memory(&mut self) {
3952
// Can't purge memory from the transform cache right now and even if we could, there's
@@ -42,8 +55,7 @@ impl Cache for TransformDatabaseStoreCache {
4255

4356
fn memory_report(&self) -> CacheMemoryReport {
4457
CacheMemoryReport {
45-
// TODO(RR-2517): Implement SizeBytes for TransformResolutionCache.
46-
bytes_cpu: 0, //self.transform_cache.total_size_bytes(),
58+
bytes_cpu: self.total_size_bytes(),
4759
bytes_gpu: None,
4860
per_cache_item_info: Vec::new(),
4961
}

0 commit comments

Comments
 (0)