re_entity_db 0.31.3

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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
use std::collections::{BTreeMap, BTreeSet};
use std::ops::RangeInclusive;

use ahash::{HashMap, HashSet};
use arrow::array::RecordBatch;
use itertools::chain;
use re_byte_size::SizeBytes as _;
use re_chunk::{Chunk, ChunkId, ComponentIdentifier, TimeInt, Timeline, TimelineName};
use re_chunk_store::{ChunkStore, QueriedChunkIdTracker};
use re_log::debug_assert;
use re_log_encoding::RrdManifest;
use re_log_types::{AbsoluteTimeRange, EntityPathHash, TimelinePoint};

use crate::{
    chunk_requests::{ChunkRequests, RequestInfo},
    rrd_manifest_index::{LoadState, RootChunkInfo},
    sorted_range_map::SortedRangeMap,
};

#[derive(Clone, Copy, Default)]
pub struct PrioritizationState {
    /// We're not allowed to have more things in-transit (on-wire)
    /// right now.
    pub transit_budget_filled: bool,

    /// We cannot fit the whole recording into memory.
    pub memory_budget_filled: bool,

    /// Some individual chunks exceed the total memory budget.
    pub some_chunks_too_big: bool,

    /// Are all required chunks fully loaded?
    ///
    /// If true, there are no missing chunks.
    pub all_required_are_loaded: bool,
}

impl PrioritizationState {
    /// The whole recording fits in memory,
    /// and the full download of it has started.
    pub fn all_chunks_loaded_or_in_transit(&self) -> bool {
        !self.transit_budget_filled && !self.memory_budget_filled && !self.some_chunks_too_big
    }
}

/// Errors that can occur during prefetching.
#[derive(thiserror::Error, Debug)]
pub enum PrefetchError {
    #[error("No manifest available")]
    NoManifest,

    #[error("Unknown timeline: {0:?}")]
    UnknownTimeline(Timeline),

    #[error("Codec: {0}")]
    Codec(#[from] re_log_encoding::CodecError),

    #[error("Arrow: {0}")]
    Arrow(#[from] arrow::error::ArrowError),
}

/// How to calculate which chunks to prefetch.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ChunkPrefetchOptions {
    /// Batch together requests until we reach this size.
    pub max_on_wire_bytes_per_batch: u64,

    /// Total budget for all physical chunks.
    pub total_uncompressed_byte_budget: u64,

    /// Maximum number of bytes in transit at once.
    pub max_bytes_on_wire_at_once: u64,
}

impl Default for ChunkPrefetchOptions {
    fn default() -> Self {
        Self {
            total_uncompressed_byte_budget: u64::MAX,

            // Batch small chunks together.
            max_on_wire_bytes_per_batch: 256 * 1024,

            // A high value -> better theoretical bandwidth
            // Low value -> better responsiveness (e.g. when moving time cursor).
            // In practice, this is a limit on how many bytes we can download _every frame_.
            max_bytes_on_wire_at_once: 4_000_000,
        }
    }
}

/// Special chunks for which we need the entire history, not just the latest-at value.
///
/// Right now, this is only used for transform-related chunks.
#[derive(Clone, Default)]
struct HighPrioChunks {
    /// Sorted by time range min.
    temporal_chunks: BTreeMap<TimelineName, Vec<HighPrioChunk>>,
}

impl HighPrioChunks {
    /// All static chunks, plus all temporal chunks on this timeline before the given time.
    /// With chunks closest to the time cursor ordered first.
    fn all_before(&self, timeline_point: TimelinePoint) -> impl Iterator<Item = ChunkId> + '_ {
        self.temporal_chunks
            .get(timeline_point.name())
            .into_iter()
            .flat_map(move |chunks| {
                let idx =
                    chunks.partition_point(|chunk| chunk.time_range.min <= timeline_point.time);

                // Start loading closest to the time cursor.
                chunks[..idx].iter().rev()
            })
            .map(|chunk| chunk.chunk_id)
    }
}

impl re_byte_size::SizeBytes for HighPrioChunks {
    fn heap_size_bytes(&self) -> u64 {
        let Self { temporal_chunks } = self;
        temporal_chunks.heap_size_bytes()
    }
}

#[derive(Clone)]
struct HighPrioChunk {
    chunk_id: ChunkId,
    time_range: AbsoluteTimeRange,
}

impl re_byte_size::SizeBytes for HighPrioChunk {
    fn heap_size_bytes(&self) -> u64 {
        let Self {
            chunk_id: _,
            time_range: _,
        } = self;
        0
    }

    fn is_pod() -> bool {
        true
    }
}

#[derive(Default)]
struct CurrentBatch {
    row_indices: Vec<usize>,
    uncompressed_bytes: u64,
    on_wire_bytes: u64,
}

impl CurrentBatch {
    fn reset(&mut self) {
        let Self {
            row_indices,
            uncompressed_bytes,
            on_wire_bytes,
        } = self;
        row_indices.clear();
        *uncompressed_bytes = 0;
        *on_wire_bytes = 0;
    }
}

/// Helper struct responsible for batching requests and creating
/// promises for missing chunks.
struct ChunkRequestBatcher<'a> {
    manifest: &'a RrdManifest,
    chunk_byte_size_uncompressed: &'a [u64],
    chunk_byte_size: &'a [u64],
    max_on_wire_bytes_per_batch: u64,

    remaining_bytes_in_on_wire_budget: u64,
    current_batch: CurrentBatch,

    // Output
    to_load: Vec<(RecordBatch, RequestInfo)>,
}

impl<'a> ChunkRequestBatcher<'a> {
    fn new(
        manifest: &'a RrdManifest,
        requests: &ChunkRequests,
        options: &ChunkPrefetchOptions,
    ) -> Self {
        Self {
            chunk_byte_size_uncompressed: manifest.col_chunk_byte_size_uncompressed(),
            chunk_byte_size: manifest.col_chunk_byte_size(),
            manifest,
            max_on_wire_bytes_per_batch: options.max_on_wire_bytes_per_batch,

            remaining_bytes_in_on_wire_budget: options
                .max_bytes_on_wire_at_once
                .saturating_sub(requests.num_on_wire_bytes_pending()),
            current_batch: Default::default(),

            to_load: Vec::new(),
        }
    }

    fn finish_batch(&mut self) -> Result<(), PrefetchError> {
        if self.current_batch.row_indices.is_empty() {
            return Ok(());
        }

        let row_indices: BTreeSet<usize> = self.current_batch.row_indices.iter().copied().collect();

        let col_chunk_ids: &[ChunkId] = self.manifest.col_chunk_ids();

        let mut root_chunk_ids = ahash::HashSet::default();
        for &row_idx in &row_indices {
            root_chunk_ids.insert(col_chunk_ids[row_idx]);
        }

        let rb = re_arrow_util::take_record_batch(
            self.manifest.data(),
            &std::mem::take(&mut self.current_batch.row_indices),
        )?;
        self.to_load.push((
            rb,
            RequestInfo {
                root_chunk_ids,
                row_indices,
                size_bytes_uncompressed: self.current_batch.uncompressed_bytes,
                size_bytes_on_wire: self.current_batch.on_wire_bytes,
            },
        ));
        self.current_batch.reset();
        Ok(())
    }

    /// Add a chunk to be fetched.
    fn try_fetch(&mut self, chunk_row_idx: usize) -> Result<bool, PrefetchError> {
        if self.remaining_bytes_in_on_wire_budget == 0 {
            return Ok(false);
        }

        let uncompressed_chunk_size = self.chunk_byte_size_uncompressed[chunk_row_idx];
        let on_wire_byte_size = self.chunk_byte_size[chunk_row_idx];

        self.current_batch.row_indices.push(chunk_row_idx);
        self.current_batch.uncompressed_bytes += uncompressed_chunk_size;
        self.current_batch.on_wire_bytes += on_wire_byte_size;

        if self.max_on_wire_bytes_per_batch <= self.current_batch.on_wire_bytes {
            self.finish_batch()?;
        }
        self.remaining_bytes_in_on_wire_budget = self
            .remaining_bytes_in_on_wire_budget
            .saturating_sub(on_wire_byte_size);
        Ok(true)
    }

    /// Returns all batches that should be loaded
    #[must_use = "Load the returned batches"]
    pub fn finish(mut self) -> Result<Vec<(RecordBatch, RequestInfo)>, PrefetchError> {
        self.finish_batch()?;
        Ok(self.to_load)
    }
}

fn warn_entity_exceeds_memory(entity_path: &str) {
    // TODO(RR-3344): improve this error message
    if cfg!(target_arch = "wasm32") {
        re_log::debug_once!(
            "Cannot load all of entity '{entity_path}', because its size exceeds the memory budget. Try the native viewer instead, or split up your large assets (e.g. prefer VideoStream over VideoAsset)."
        );
    } else {
        re_log::warn_once!(
            "Cannot load all of entity '{entity_path}', because its size exceeds the memory budget. You should increase the `--memory-limit` or try to split up your large assets (e.g. prefer VideoStream over VideoAsset)."
        );
    }
}

struct RemainingByteBudget {
    remaining_bytes: u64,
}

impl RemainingByteBudget {
    /// Try to fit `bytes` into the remaining budget.
    ///
    /// Returns `true` if it fits (even partially), `false` if the budget is exhausted.
    fn try_fit_into_budget(&mut self, bytes: u64, required: bool) -> bool {
        self.remaining_bytes = self.remaining_bytes.saturating_sub(bytes);

        if self.remaining_bytes == 0 {
            if required {
                if cfg!(target_arch = "wasm32") {
                    re_log::warn_once!(
                        "This recording is very memory intense, and the Wasm32 build only has 4GiB of memory. Consider using the native viewer to use all of your RAM."
                    );
                } else {
                    re_log::warn_once!(
                        "The current recording may use more data than your current memory budget."
                    );
                }
                true // Risk it! We are conservative in our budgeting
            } else {
                false
            }
        } else {
            true
        }
    }
}

/// Chunk that we've prioritized in `chunks_in_priority`.
struct PrioritizedRootChunk {
    /// If this chunk came from `used_physical` or `missing_virtual` it's required
    /// and we log a warning if we can't fit it.
    required: bool,

    root_chunk_id: ChunkId,
}

impl PrioritizedRootChunk {
    fn required(root_chunk_id: ChunkId) -> Self {
        Self {
            required: true,
            root_chunk_id,
        }
    }

    fn optional(chunk_id: ChunkId) -> Self {
        Self {
            required: false,
            root_chunk_id: chunk_id,
        }
    }
}

#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct ComponentPathKey {
    entity_path: EntityPathHash,
    component: ComponentIdentifier,
}

#[cfg(test)]
impl ComponentPathKey {
    /// Creates a dummy key for use in tests where the specific entity/component doesn't matter.
    pub fn dummy() -> Self {
        Self {
            entity_path: EntityPathHash::NONE,
            component: ComponentIdentifier::new("test"),
        }
    }
}

impl re_byte_size::SizeBytes for ComponentPathKey {
    fn heap_size_bytes(&self) -> u64 {
        let Self {
            entity_path: _,
            component: _,
        } = self;

        0
    }

    fn is_pod() -> bool {
        true
    }
}

#[derive(Clone, Default)]
pub struct ProtectedChunks {
    /// All root chunks that we have an interest in having loaded,
    /// (or at least a part of them).
    ///
    /// These chunks are protected from _canceling_,
    /// i.e. we won't cancel the download of these chunks.
    pub roots: HashSet<ChunkId>,

    /// All physical chunks that are 'in' the memory limit.
    ///
    /// These chunks are protected from being gc'd.
    pub physical: HashSet<ChunkId>,
}

impl re_byte_size::SizeBytes for ProtectedChunks {
    fn heap_size_bytes(&self) -> u64 {
        let Self { roots, physical } = self;
        roots.heap_size_bytes() + physical.heap_size_bytes()
    }
}

#[derive(Default)]
#[cfg_attr(feature = "testing", derive(Clone))]
pub struct ChunkPrioritizer {
    protected_chunks: ProtectedChunks,

    /// Result of the latest call to [`Self::prioritize_and_prefetch`].
    latest_result: Option<PrioritizationState>,

    /// Chunks that are in the progress of being downloaded.
    chunk_requests: ChunkRequests,

    /// Intervals of all root chunks in the rrd manifest per timeline.
    root_chunk_intervals: BTreeMap<Timeline, SortedRangeMap<TimeInt, ChunkId>>,

    /// All static root chunks in the rrd manifest.
    static_chunk_ids: HashSet<ChunkId>,

    /// Chunks that should be downloaded before any else.
    high_priority_chunks: HighPrioChunks,

    pub component_paths_from_root_id: HashMap<ChunkId, Vec<ComponentPathKey>>,

    /// Component paths that were reported either as being used or missing.
    pub components_of_interest: HashSet<ComponentPathKey>,
}

impl re_byte_size::SizeBytes for ChunkPrioritizer {
    fn heap_size_bytes(&self) -> u64 {
        let Self {
            protected_chunks,
            latest_result: _,
            chunk_requests: _, // not yet implemented
            root_chunk_intervals: virtual_chunk_intervals,
            static_chunk_ids,
            high_priority_chunks,
            component_paths_from_root_id,
            components_of_interest,
        } = self;

        protected_chunks.heap_size_bytes()
            + virtual_chunk_intervals.heap_size_bytes()
            + static_chunk_ids.heap_size_bytes()
            + high_priority_chunks.heap_size_bytes()
            + component_paths_from_root_id.heap_size_bytes()
            + components_of_interest.heap_size_bytes()
    }
}

impl ChunkPrioritizer {
    pub fn on_rrd_manifest(&mut self, delta: &RrdManifest) {
        self.update_static_chunks(delta);
        self.update_chunk_intervals(delta);
        self.update_high_priority_chunks(delta);

        for (entity, per_component) in delta.static_map() {
            for (component, chunk) in per_component {
                self.component_paths_from_root_id
                    .entry(*chunk)
                    .or_default()
                    .push(ComponentPathKey {
                        entity_path: entity.hash(),
                        component: *component,
                    });
            }
        }

        for (entity, per_timeline) in delta.temporal_map() {
            for per_component in per_timeline.values() {
                for (component, chunks) in per_component {
                    for chunk in chunks.keys() {
                        self.component_paths_from_root_id
                            .entry(*chunk)
                            .or_default()
                            .push(ComponentPathKey {
                                entity_path: entity.hash(),
                                component: *component,
                            });
                    }
                }
            }
        }
    }

    /// Result of the latest call to [`Self::prioritize_and_prefetch`].
    pub fn latest_result(&self) -> Option<PrioritizationState> {
        self.latest_result
    }

    /// Find all chunk IDs that contain components with the given prefix.
    fn find_chunks_with_component_prefix(manifest: &RrdManifest, prefix: &str) -> HighPrioChunks {
        let mut temporal_chunks: BTreeMap<TimelineName, Vec<HighPrioChunk>> = Default::default();

        // We intentionally ignore static chunks, because we already prioritize ALL static chunks.

        for timelines in manifest.temporal_map().values() {
            for (timeline, components) in timelines {
                for (component, chunks) in components {
                    if component.as_str().starts_with(prefix) {
                        for (chunk_id, entry) in chunks {
                            temporal_chunks.entry(*timeline.name()).or_default().push(
                                HighPrioChunk {
                                    chunk_id: *chunk_id,
                                    time_range: entry.time_range,
                                },
                            );
                        }
                    }
                }
            }
        }

        for chunks in temporal_chunks.values_mut() {
            chunks.sort_by_key(|chunk| chunk.time_range.min);
        }

        HighPrioChunks { temporal_chunks }
    }

    fn update_high_priority_chunks(&mut self, manifest: &RrdManifest) {
        // Find chunks containing transform-related components.
        // We need to download _all_ of them because any one of them could
        // contain a crucial part of the transform hierarchy.
        // Latest-at fails, because a single entity can define the transform of multiple
        // parts of a hierarchy, and not all of the transform are required to be
        // available at each time point.
        // More here: https://linear.app/rerun/issue/RR-3441/required-transform-frames-arent-always-loaded
        let new_chunks = Self::find_chunks_with_component_prefix(
            manifest,
            "Transform3D:", // Hard-coding this here is VERY hacky, but I want to ship MVP
        );
        for (timeline, mut chunks) in new_chunks.temporal_chunks {
            let existing = self
                .high_priority_chunks
                .temporal_chunks
                .entry(timeline)
                .or_default();
            existing.append(&mut chunks);
            existing.sort_by_key(|chunk| chunk.time_range.min);
        }
    }

    fn update_static_chunks(&mut self, manifest: &RrdManifest) {
        for entity_chunks in manifest.static_map().values() {
            for &chunk_id in entity_chunks.values() {
                self.static_chunk_ids.insert(chunk_id);
            }
        }
    }

    fn update_chunk_intervals(&mut self, manifest: &RrdManifest) {
        let mut per_timeline_chunks: BTreeMap<Timeline, Vec<(RangeInclusive<TimeInt>, ChunkId)>> =
            BTreeMap::default();

        for timelines in manifest.temporal_map().values() {
            for (timeline, components) in timelines {
                let timeline_chunks = per_timeline_chunks.entry(*timeline).or_default();
                for chunks in components.values() {
                    for (chunk_id, entry) in chunks {
                        timeline_chunks.push((entry.time_range.into(), *chunk_id));
                    }
                }
            }
        }

        for (timeline, chunks) in per_timeline_chunks {
            self.root_chunk_intervals
                .entry(timeline)
                .or_default()
                .extend(chunks);
        }
    }

    pub fn chunk_requests(&self) -> &ChunkRequests {
        &self.chunk_requests
    }

    pub fn chunk_requests_mut(&mut self) -> &mut ChunkRequests {
        &mut self.chunk_requests
    }

    pub fn protected_chunks(&self) -> &ProtectedChunks {
        &self.protected_chunks
    }

    /// An iterator over root chunks in priority order.
    ///
    /// May return duplicates!
    ///
    /// See [`Self::prioritize_and_prefetch`] for more details.
    #[expect(clippy::too_many_arguments)] // TODO(emilk): refactor to simplify
    fn root_chunks_in_priority<'a>(
        components_of_interest: &'a HashSet<ComponentPathKey>,
        component_paths_from_root_id: &'a HashMap<ChunkId, Vec<ComponentPathKey>>,
        static_chunk_ids: &'a HashSet<ChunkId>,
        high_priority_chunks: &'a HighPrioChunks,
        store: &'a ChunkStore,
        used_and_missing: &QueriedChunkIdTracker,
        time_cursor: Option<TimelinePoint>,
        root_chunks: &'a HashMap<ChunkId, RootChunkInfo>,
        root_chunks_on_timeline: Option<&'a SortedRangeMap<TimeInt, ChunkId>>,
    ) -> impl Iterator<Item = PrioritizedRootChunk> + use<'a> {
        re_tracing::profile_function!();

        let mut missing_roots = Vec::new();
        for missing_virtual_chunk_id in &used_and_missing.missing_virtual {
            store.collect_root_ids(missing_virtual_chunk_id, &mut missing_roots);
        }
        missing_roots.sort();
        missing_roots.dedup();

        let chunks_ids_after_time_cursor = move || {
            time_cursor
                .zip(root_chunks_on_timeline)
                .map(|(time_cursor, root_chunks_on_timeline)| {
                    root_chunks_on_timeline
                        .query(time_cursor.time..=TimeInt::MAX)
                        .map(|(_, chunk_id)| *chunk_id)
                })
                .into_iter()
                .flatten()
        };
        let chunks_ids_before_time_cursor = move || {
            time_cursor
                .zip(root_chunks_on_timeline)
                .map(|(time_cursor, root_chunks_on_timeline)| {
                    root_chunks_on_timeline
                        .query(TimeInt::MIN..=time_cursor.time.saturating_sub(1))
                        .map(|(_, chunk_id)| *chunk_id)
                })
                .into_iter()
                .flatten()
        };

        // Note: we do NOT take `components_of_interest` for high-priority transform chunks,
        // because that seems to cause bugs for unknown reasons.
        let high_prio_chunks_before_time_cursor = time_cursor
            .map(|time_cursor| high_priority_chunks.all_before(time_cursor))
            .into_iter()
            .flatten();

        // Chunks that are required for the current view.
        let required_chunks = chain!(
            missing_roots,
            static_chunk_ids.iter().copied(),
            high_prio_chunks_before_time_cursor,
        );

        // Chunks that aren't currently required. Pure prefetching:
        let optional_chunks = {
            // Chunks for components we are interested in.
            let is_interesting_chunk = |chunk_id: &ChunkId| {
                component_paths_from_root_id[chunk_id]
                    .iter()
                    .any(|path| components_of_interest.contains(path))
            };
            let is_uninteresting_chunk = |chunk_id: &ChunkId| {
                !component_paths_from_root_id[chunk_id]
                    .iter()
                    .any(|path| components_of_interest.contains(path))
            };

            // Extra chunks we try to prefetch, that may _soon_ be needed:
            let optional_interesting_chunks = chain!(
                std::iter::once_with(chunks_ids_after_time_cursor).flatten(),
                std::iter::once_with(chunks_ids_before_time_cursor).flatten(),
            )
            .filter(is_interesting_chunk);

            // Extra chunks at the current time (or after), that the user is not _currently_
            // looking at, but they may switch views.
            let optional_uninteresting_chunks = std::iter::once_with(chunks_ids_after_time_cursor)
                .flatten()
                .filter(is_uninteresting_chunk);

            // Finally: backfill with ALL unloaded chunks.
            // If we have the memory budget for it, we always want to load the full recording:
            let all_chunks = root_chunks.keys().copied();

            chain!(
                optional_interesting_chunks,
                optional_uninteresting_chunks,
                all_chunks,
            )
        };

        chain!(
            required_chunks.map(PrioritizedRootChunk::required),
            optional_chunks.map(PrioritizedRootChunk::optional),
        )
    }

    /// Prioritize which chunk (loaded & unloaded) we want to fit in the
    /// current memory budget. And prefetch some amount of those chunks.
    ///
    /// This prioritizes chunks in the order of:
    /// - Physical chunks that were used since last time this was ran.
    /// - Virtual chunks that would've been hit by queries since last time
    ///   this was ran.
    /// - Static chunks.
    /// - Chunks after the time cursor in rising temporal order.
    /// - Chunks before the time cursor in rising temporal order.
    ///
    /// We go through these chunks until we hit [`ChunkPrefetchOptions::total_uncompressed_byte_budget`]
    /// and prefetch missing chunks until we hit [`ChunkPrefetchOptions::max_bytes_on_wire_at_once`].
    /// Returns all batches that should be loaded.
    #[must_use = "Load the returned batches"]
    pub fn prioritize_and_prefetch(
        &mut self,
        store: &ChunkStore,
        used_and_missing: &QueriedChunkIdTracker,
        options: &ChunkPrefetchOptions,
        time_cursor: Option<TimelinePoint>,
        manifest: &RrdManifest,
        root_chunks: &HashMap<ChunkId, RootChunkInfo>,
    ) -> Result<Vec<(RecordBatch, RequestInfo)>, PrefetchError> {
        re_tracing::profile_function!();

        let mut chunk_batcher = ChunkRequestBatcher::new(manifest, &self.chunk_requests, options);

        if let Some(latest_result) = &mut self.latest_result
            && chunk_batcher.remaining_bytes_in_on_wire_budget == 0
        {
            // Early-out: too many bytes already in-transit.

            if !used_and_missing.missing_virtual.is_empty() {
                latest_result.all_required_are_loaded = false;
            }

            self.protect_used_and_missing(store, used_and_missing);
            return Ok(vec![]);
        }

        self.update_components_of_interest(store, used_and_missing);

        // We will re-calculate these:
        self.protected_chunks.roots.clear();
        self.protected_chunks.physical.clear(); // <- Things we put in here will also be subtracted from remaining_byte_budget

        self.protect_used_and_missing(store, used_and_missing);

        let mut remaining_byte_budget = RemainingByteBudget {
            remaining_bytes: options.total_uncompressed_byte_budget,
        };

        // Start by going through the actually used physical chunks:
        for &physical_chunk_id in &used_and_missing.used_physical {
            debug_assert!(
                self.protected_chunks.physical.contains(&physical_chunk_id),
                "We added it earlier"
            );

            if let Some(chunk) = store.physical_chunk(&physical_chunk_id) {
                let required = true;
                remaining_byte_budget
                    .try_fit_into_budget(Chunk::total_size_bytes(chunk.as_ref()), required);
            } else {
                re_log::debug_warn_once!("Couldn't get physical chunk from chunk store");
            }
        }

        let root_chunks_on_timeline = time_cursor
            .and_then(|time_cursor| self.root_chunk_intervals.get(&time_cursor.timeline()));

        let root_chunk_ids_in_priority_order = Self::root_chunks_in_priority(
            &self.components_of_interest,
            &self.component_paths_from_root_id,
            &self.static_chunk_ids,
            &self.high_priority_chunks,
            store,
            used_and_missing,
            time_cursor,
            root_chunks,
            root_chunks_on_timeline,
        );

        let state = Self::fill_byte_budget(
            &mut self.protected_chunks,
            store,
            options,
            manifest,
            root_chunks,
            &mut chunk_batcher,
            &mut remaining_byte_budget,
            root_chunk_ids_in_priority_order,
        )?;
        self.latest_result = Some(state);

        chunk_batcher.finish()
    }

    #[expect(clippy::too_many_arguments)]
    fn fill_byte_budget(
        protected_chunks: &mut ProtectedChunks,
        store: &ChunkStore,
        options: &ChunkPrefetchOptions,
        manifest: &RrdManifest,
        root_chunks: &HashMap<ChunkId, RootChunkInfo>,
        chunk_batcher: &mut ChunkRequestBatcher<'_>,
        remaining_byte_budget: &mut RemainingByteBudget,
        mut root_chunk_ids_in_priority_order: impl Iterator<Item = PrioritizedRootChunk>,
    ) -> Result<PrioritizationState, PrefetchError> {
        re_tracing::profile_function!();

        let entity_paths = manifest.col_chunk_entity_path_raw();

        let mut visited_root_chunks: HashSet<ChunkId> = Default::default();

        let mut physical_chunks_scratch = Vec::new(); // scratch space to save on reallocations

        let mut state = PrioritizationState {
            transit_budget_filled: false,
            memory_budget_filled: false,
            some_chunks_too_big: false,
            all_required_are_loaded: true,
        };

        for next in root_chunk_ids_in_priority_order.by_ref() {
            let PrioritizedRootChunk {
                required,
                root_chunk_id,
            } = next;

            if !visited_root_chunks.insert(root_chunk_id) {
                continue; // We've already handled this chunk earlier in the priority order.
            }

            let Some(root_chunk) = root_chunks.get(&root_chunk_id) else {
                re_log::debug_warn_once!("Missing root chunk");
                continue;
            };

            store.collect_physical_descendents_of(&root_chunk_id, &mut physical_chunks_scratch);

            match root_chunk.state {
                LoadState::Unloaded | LoadState::InTransit => {
                    if required {
                        state.all_required_are_loaded = false;
                    }

                    let row_idx = root_chunk.row_id;

                    // We count only the chunks we are interested in as being part of the memory budget.
                    // The others can/will be evicted as needed.
                    let uncompressed_chunk_size =
                        chunk_batcher.chunk_byte_size_uncompressed[row_idx];

                    if options.total_uncompressed_byte_budget < uncompressed_chunk_size {
                        warn_entity_exceeds_memory(entity_paths.value(row_idx));
                        state.some_chunks_too_big = true;
                        continue;
                    }

                    if !remaining_byte_budget.try_fit_into_budget(uncompressed_chunk_size, required)
                    {
                        state.memory_budget_filled = true;
                        break;
                    }

                    if root_chunk.state == LoadState::Unloaded
                        && !chunk_batcher.try_fetch(row_idx)?
                    {
                        // If we don't have anything more to fetch we stop looking.
                        //
                        // This isn't entirely correct gc wise. But if we evict chunks
                        // we didn't get to because of this break, we won't be fighting
                        // back and forth with gc since there's some unloaded
                        // chunks inbetween we have to download first. After
                        // which we won't stop prioritizing which chunks should
                        // be in memory here.
                        state.transit_budget_filled = true;
                        break;
                    }

                    protected_chunks.roots.insert(root_chunk_id);
                    protected_chunks
                        .physical
                        .extend(physical_chunks_scratch.drain(..));
                }

                LoadState::FullyLoaded => {
                    protected_chunks.roots.insert(root_chunk_id);

                    for chunk_id in physical_chunks_scratch.drain(..) {
                        if protected_chunks.physical.contains(&chunk_id) {
                            continue; // Already counted as part of our byte budget
                        }

                        let Some(chunk) = store.physical_chunk(&chunk_id) else {
                            re_log::debug_warn_once!(
                                "Couldn't get physical chunk from chunk store"
                            );
                            continue;
                        };

                        let bytes = Chunk::total_size_bytes(chunk.as_ref());
                        if !remaining_byte_budget.try_fit_into_budget(bytes, required) {
                            state.memory_budget_filled = true;
                            break;
                        }

                        protected_chunks.physical.insert(chunk_id);
                    }
                }
            }
        }

        if root_chunk_ids_in_priority_order
            .next()
            .is_some_and(|next| next.required)
        {
            state.all_required_are_loaded = false;
        }

        Ok(state)
    }

    fn update_components_of_interest(
        &mut self,
        store: &ChunkStore,
        used_and_missing: &QueriedChunkIdTracker,
    ) {
        re_tracing::profile_function!();

        // Basically: what components of which entities are currently being viewed by the user?
        self.components_of_interest.clear();

        let QueriedChunkIdTracker {
            used_physical,
            missing_virtual,
        } = used_and_missing;

        for physical_chunk_id in used_physical {
            if let Some(chunk) = store.physical_chunk(physical_chunk_id) {
                for component in chunk.components_identifiers() {
                    self.components_of_interest.insert(ComponentPathKey {
                        entity_path: chunk.entity_path().hash(),
                        component,
                    });
                }
            }
        }
        for missing_virtual_chunk_id in missing_virtual {
            for root_id in store.find_root_chunks(missing_virtual_chunk_id) {
                if let Some(components) = self.component_paths_from_root_id.get(&root_id) {
                    self.components_of_interest
                        .extend(components.iter().copied());
                }
            }
        }
    }

    /// Prevent these chunks from being canceled or GC:ed.
    fn protect_used_and_missing(
        &mut self,
        store: &ChunkStore,
        used_and_missing: &QueriedChunkIdTracker,
    ) {
        let QueriedChunkIdTracker {
            used_physical,
            missing_virtual,
        } = used_and_missing;

        for physical_chunk_id in used_physical {
            // We don't need to add the root(s) of this to the `protected_root_chunks`.
            // It is fine to cancel the download of the root(s),
            // as long as we don't GC this particular physical chunk.
            self.protected_chunks.physical.insert(*physical_chunk_id);
        }

        for chunk_id in missing_virtual {
            // Do not cancel any downloads of any roots of this missing chunk:
            for root_id in store.find_root_chunks(chunk_id) {
                self.protected_chunks.roots.insert(root_id);
            }
        }
    }

    /// Cancel all fetches of things that are not currently needed.
    #[must_use = "Returns root chunks whose download got cancelled. Mark them as unloaded!"]
    pub fn cancel_outdated_requests(&mut self, egui_now_time: f64) -> Vec<ChunkId> {
        self.chunk_requests
            .cancel_outdated_requests(egui_now_time, &self.protected_chunks.roots)
    }
}