re_entity_db 0.30.2

In-memory storage of Rerun entities
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
use std::collections::BTreeMap;
use std::ops::Bound;

use emath::lerp;
use itertools::Itertools as _;
use re_byte_size::{MemUsageNode, MemUsageTree, MemUsageTreeCapture, SizeBytes as _};
use re_chunk::{TimeInt, Timeline, TimelineName};
use re_chunk_store::{ChunkDirectLineage, ChunkStore, ChunkStoreDiff, ChunkStoreEvent};
use re_log_types::{AbsoluteTimeRange, AbsoluteTimeRangeF, TimeReal};

use crate::RrdManifestIndex;

// ---

/// Number of messages per time.
// TODO(RR-3784): get rid of TimeHistogram completely
#[derive(Clone)]
pub struct TimeHistogram {
    timeline: Timeline,
    hist: re_int_histogram::Int64Histogram,
}

impl std::ops::Deref for TimeHistogram {
    type Target = re_int_histogram::Int64Histogram;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.hist
    }
}

impl TimeHistogram {
    pub fn new(timeline: Timeline) -> Self {
        Self {
            timeline,
            hist: Default::default(),
        }
    }

    pub fn timeline(&self) -> Timeline {
        self.timeline
    }

    pub fn num_rows(&self) -> u64 {
        self.hist.total_count()
    }

    pub fn insert(&mut self, time: TimeInt, count: u64) {
        self.hist.increment(time.as_i64(), count as _);
    }

    pub fn increment(&mut self, time: i64, n: u32) {
        self.hist.increment(time, n);
    }

    pub fn decrement(&mut self, time: i64, n: u32) {
        self.hist.decrement(time, n);
    }

    fn min_opt(&self) -> Option<TimeInt> {
        self.min_key().map(TimeInt::new_temporal)
    }

    pub fn min(&self) -> TimeInt {
        self.min_opt().unwrap_or(TimeInt::MIN)
    }

    fn max_opt(&self) -> Option<TimeInt> {
        self.max_key().map(TimeInt::new_temporal)
    }

    pub fn max(&self) -> TimeInt {
        self.max_opt().unwrap_or(TimeInt::MIN)
    }

    pub fn full_range(&self) -> AbsoluteTimeRange {
        AbsoluteTimeRange::new(self.min(), self.max())
    }

    pub fn step_fwd_time(&self, time: TimeReal) -> TimeInt {
        self.next_key_after(time.floor().as_i64())
            .map(TimeInt::new_temporal)
            .unwrap_or_else(|| self.min())
    }

    pub fn step_back_time(&self, time: TimeReal) -> TimeInt {
        self.prev_key_before(time.ceil().as_i64())
            .map(TimeInt::new_temporal)
            .unwrap_or_else(|| self.max())
    }

    pub fn step_fwd_time_looped(
        &self,
        time: TimeReal,
        loop_range: &AbsoluteTimeRangeF,
    ) -> TimeReal {
        if time < loop_range.min || loop_range.max <= time {
            loop_range.min
        } else if let Some(next) = self
            .range(
                (
                    Bound::Excluded(time.floor().as_i64()),
                    Bound::Included(loop_range.max.floor().as_i64()),
                ),
                1,
            )
            .next()
            .map(|(r, _)| r.min)
        {
            TimeReal::from(next)
        } else {
            self.step_fwd_time(time).into()
        }
    }

    pub fn step_back_time_looped(
        &self,
        time: TimeReal,
        loop_range: &AbsoluteTimeRangeF,
    ) -> TimeReal {
        re_tracing::profile_function!();

        if time <= loop_range.min || loop_range.max < time {
            loop_range.max
        } else {
            // Collect all keys in the range and take the last one.
            // Yes, this could be slow :/
            let mut prev_key = None;
            for (range, _) in self.range(
                (
                    Bound::Included(loop_range.min.ceil().as_i64()),
                    Bound::Excluded(time.ceil().as_i64()),
                ),
                1,
            ) {
                prev_key = Some(range.max);
            }
            if let Some(prev) = prev_key {
                TimeReal::from(TimeInt::new_temporal(prev))
            } else {
                self.step_back_time(time).into()
            }
        }
    }
}

/// Number of messages per time per timeline.
///
/// Does NOT include static data.
#[derive(Default, Clone)]
pub struct TimeHistogramPerTimeline {
    /// When do we have data? Ignores static data.
    times: BTreeMap<TimelineName, TimeHistogram>,

    /// Extra bookkeeping used to seed any timelines that include static msgs.
    has_static: bool,
}

impl TimeHistogramPerTimeline {
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.times.is_empty() && !self.has_static
    }

    #[inline]
    pub fn timelines(&self) -> impl ExactSizeIterator<Item = Timeline> {
        self.times.values().map(|h| h.timeline())
    }

    pub fn histograms(&self) -> impl ExactSizeIterator<Item = &TimeHistogram> {
        self.times.values()
    }

    #[inline]
    pub fn get(&self, timeline: &TimelineName) -> Option<&TimeHistogram> {
        self.times.get(timeline)
    }

    #[inline]
    pub fn has_timeline(&self, timeline: &TimelineName) -> bool {
        self.times.contains_key(timeline)
    }

    #[inline]
    pub fn iter(&self) -> impl ExactSizeIterator<Item = (&TimelineName, &TimeHistogram)> {
        self.times.iter()
    }

    /// Total number of temporal messages over all timelines.
    pub fn num_temporal_messages(&self) -> u64 {
        self.times.values().map(|hist| hist.total_count()).sum()
    }

    /// Increments `n` for each specified time.
    ///
    /// I.e. this adds a total of `n*times.len()`.
    fn add_temporal(&mut self, timeline: &Timeline, times: &[i64], n: u32) {
        re_tracing::profile_function!();

        let histogram = self
            .times
            .entry(*timeline.name())
            .or_insert_with(|| TimeHistogram::new(*timeline));
        for &time in times {
            histogram.increment(time, n);
        }
    }

    /// Decrements `n` for each specified time.
    ///
    /// I.e. this removes a total of `n*times.len()`.
    fn remove_temporal(&mut self, timeline: &Timeline, times: &[i64], n: u32) {
        re_tracing::profile_function!();

        if let Some(histogram) = self.times.get_mut(timeline.name()) {
            for &time in times {
                histogram.decrement(time, n);
            }
            if histogram.is_empty() {
                self.times.remove(timeline.name());
            }
        }
    }

    /// If we know the manifest ahead of time, we can pre-populate
    /// the histogram with a rough estimate of the final form.
    pub fn on_rrd_manifest(&mut self, rrd_manifest_index: &RrdManifestIndex) {
        re_tracing::profile_function!();

        for chunk in rrd_manifest_index.root_chunks() {
            if chunk.temporals.is_empty() {
                self.has_static = true;
            }

            for info in chunk.temporals.values() {
                let histogram = self
                    .times
                    .entry(*info.timeline.name())
                    .or_insert_with(|| TimeHistogram::new(info.timeline));

                apply_estimate(
                    Application::Add,
                    histogram,
                    info.time_range,
                    info.num_rows_for_all_entities_all_components,
                );
            }
        }
    }

    pub fn on_events(
        &mut self,
        store: &ChunkStore,
        rrd_manifest_index: &RrdManifestIndex,
        events: &[ChunkStoreEvent],
    ) {
        re_tracing::profile_function!();

        for event in events {
            match &event.diff {
                ChunkStoreDiff::Addition(add) => {
                    let delta_chunk = add.delta_chunk();

                    let root_chunk_id = add.chunk_before_processing.id();
                    let root_chunk_info = rrd_manifest_index.root_chunk_info(&root_chunk_id);

                    if delta_chunk.is_static() {
                        self.has_static = true;
                    } else {
                        for time_column in delta_chunk.timelines().values() {
                            let times = time_column.times_raw();
                            let timeline = time_column.timeline();

                            if let Some(chunk_info) = root_chunk_info
                                && let Some(timeline_info) =
                                    &chunk_info.temporals.get(timeline.name())
                            {
                                // We added an estimated value for this before, based on the RRD manifest.
                                // Now that we have the whole chunk we need to subtract those fake values again,
                                // before we add in the actual contents of the chunk:

                                let histogram = self
                                    .times
                                    .entry(*timeline.name())
                                    .or_insert_with(|| TimeHistogram::new(*timeline));

                                apply_estimate(
                                    Application::Remove,
                                    histogram,
                                    timeline_info.time_range,
                                    timeline_info.num_rows_for_all_entities_all_components,
                                );
                            }

                            self.add_temporal(
                                time_column.timeline(),
                                times,
                                // This value is incorrect since it doesn't account for the potential sparseness
                                // of individual components.
                                // I.e. this will over-count. For what we use this datastructure for, this is fine.
                                delta_chunk.num_components() as _,
                            );
                        }
                    }
                }

                ChunkStoreDiff::Deletion(del) => {
                    if del.chunk.is_static() {
                        // we don't care
                    } else {
                        // We want to explicitly look for root chunks here, even if that means walking recursively
                        // through the lineage tree.
                        // We will need them in order to re-fill the estimates as best as we can.
                        let root_chunk_ids = store.find_root_chunks(&del.chunk.id());
                        let root_chunk_infos = root_chunk_ids
                            .iter()
                            .filter_map(|cid| rrd_manifest_index.root_chunk_info(cid))
                            .collect_vec();

                        for time_column in del.chunk.timelines().values() {
                            let times = time_column.times_raw();
                            let timeline = time_column.timeline();

                            self.remove_temporal(
                                time_column.timeline(),
                                times,
                                // This value is incorrect since it doesn't account for the potential sparseness
                                // of individual components.
                                // I.e. this will over-count. For what we use this datastructure for, this is fine.
                                del.chunk.num_components() as _,
                            );

                            #[expect(clippy::match_same_arms)] // readability
                            let undo_factor: f64 = match store.direct_lineage(&del.chunk.id()) {
                                // If the removed chunk was part of split lineage of siblings, then only bring that
                                // much of the estimate back.
                                Some(ChunkDirectLineage::SplitFrom(_, sibling_ids)) => {
                                    1.0 / (sibling_ids.len() + 1) as f64
                                }

                                Some(ChunkDirectLineage::CompactedFrom(_)) => 1.0,

                                _ => 1.0,
                            };

                            for chunk_info in &root_chunk_infos {
                                if let Some(timeline_info) =
                                    chunk_info.temporals.get(timeline.name())
                                {
                                    let histogram = self
                                        .times
                                        .entry(*timeline.name())
                                        .or_insert_with(|| TimeHistogram::new(*timeline));

                                    let n = timeline_info.num_rows_for_all_entities_all_components
                                        as f64;
                                    let n = n * undo_factor;
                                    let n = n as u64;

                                    apply_estimate(
                                        Application::Add,
                                        histogram,
                                        timeline_info.time_range,
                                        n,
                                    );
                                }
                            }
                        }
                    }
                }

                ChunkStoreDiff::VirtualAddition(_) => {
                    // TODO(cmc): this should probably replace the `on_rrd_manifest` impl above.
                }
            }
        }
    }
}

#[derive(Clone, Copy, Debug)]
enum Application {
    Add,
    Remove,
}

impl Application {
    fn apply(self, histogram: &mut TimeHistogram, position: i64, inc: u32) {
        match self {
            Self::Add => {
                histogram.increment(position, inc);
            }
            Self::Remove => {
                histogram.decrement(position, inc);
            }
        }
    }
}

fn apply_estimate(
    application: Application,
    histogram: &mut TimeHistogram,
    time_range: re_log_types::AbsoluteTimeRange,
    num_rows_for_all_entities_all_components: u64,
) {
    if num_rows_for_all_entities_all_components == 0 {
        return;
    }

    // Assume even spread of chunk (for now):
    let num_pieces = u64::min(num_rows_for_all_entities_all_components, 10);

    if num_pieces == 1 || time_range.min == time_range.max {
        let position = time_range.center();
        application.apply(
            histogram,
            position.as_i64(),
            num_rows_for_all_entities_all_components as u32,
        );
    } else {
        let inc = (num_rows_for_all_entities_all_components / num_pieces) as u32;
        for i in 0..num_pieces {
            let position = lerp(
                time_range.min.as_f64()..=time_range.max.as_f64(),
                i as f64 / (num_pieces as f64 - 1.0),
            )
            .round() as i64;

            application.apply(
                histogram,
                position,
                inc + (i < num_rows_for_all_entities_all_components % num_pieces) as u32,
            );
        }
    }
}

impl re_byte_size::SizeBytes for TimeHistogram {
    fn heap_size_bytes(&self) -> u64 {
        // Calculating the memory use of the time histogram can be slow
        // (tens of ms for 1h-recording).
        // But it can also use a lot of memory
        // TODO(RR-3784): get rid of TimeHistogram completely

        re_tracing::profile_function!();

        let Self { timeline: _, hist } = self;

        let accurate_but_slow = true;

        if accurate_but_slow {
            hist.heap_size_bytes()
        } else {
            // VERY rouch estimate. Can easily be wrong by a factor of 4 in either direction.
            hist.total_count() * (std::mem::size_of::<u64>() as u64)
        }
    }
}

impl re_byte_size::SizeBytes for TimeHistogramPerTimeline {
    fn heap_size_bytes(&self) -> u64 {
        re_tracing::profile_function!();
        let Self { times, has_static } = self;
        times.heap_size_bytes() + has_static.heap_size_bytes()
    }
}

impl MemUsageTreeCapture for TimeHistogramPerTimeline {
    fn capture_mem_usage_tree(&self) -> MemUsageTree {
        re_tracing::profile_function!();

        let Self { times, has_static } = self;
        _ = has_static;

        let mut node = MemUsageNode::new();
        for (timeline_name, histogram) in times {
            node.add(
                timeline_name.as_str().to_owned(),
                histogram.total_size_bytes(),
            );
        }
        node.into_tree()
    }
}