forge
Explore
Log in

Don't you dare go Hollow

project_note

Clone
★ 0

0 watching0 forksRust3.6 MiBc49f5e7Tahinli1 month ago2 commits

handle_walk_memory.rs141 lines, 5.3 KiBHistoryRawDownload
1//! Regression test for loro#1092: a container-by-container handle walk must
2//! retain bounded memory. The decoded-value cache in `InnerStore` is capped
3//! (2048 by default), so retained memory after a walk is
4//! O(cache bound), not O(containers ever read).
5//!
6//! Run: cargo test -p loro-internal --test handle_walk_memory -- --nocapture
7
8use dev_utils::get_mem_usage;
9use loro_internal::cursor::PosType;
10use loro_internal::handler::{Handler, ValueOrHandler};
11use loro_internal::LoroDoc;
12
13fn live_bytes() -> usize {
14 get_mem_usage().0
15}
16
17const MIB: usize = 1 << 20;
18
19const TEXT: &str = "The quick brown fox jumps over the lazy dog. \
20The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.";
21
22fn build_snapshot(turns: usize, items_per_turn: usize) -> Vec<u8> {
23 let doc = LoroDoc::new_auto_commit();
24 let history = doc.get_list("history");
25 for t in 0..turns {
26 let turn = history
27 .push_container(loro_internal::handler::MapHandler::new_detached())
28 .unwrap();
29 turn.insert("id", format!("turn-{t}").as_str()).unwrap();
30 turn.insert("role", if t % 2 == 0 { "user" } else { "assistant" })
31 .unwrap();
32 let items = turn
33 .insert_container("items", loro_internal::handler::ListHandler::new_detached())
34 .unwrap();
35 let count = if t % 2 == 0 { 1 } else { items_per_turn };
36 for i in 0..count {
37 let item = items
38 .push_container(loro_internal::handler::MapHandler::new_detached())
39 .unwrap();
40 if i % 4 == 3 {
41 item.insert("type", "text").unwrap();
42 let text = item
43 .insert_container("text", loro_internal::handler::TextHandler::new_detached())
44 .unwrap();
45 text.insert(0, &format!("{TEXT} turn {t} item {i}"), PosType::Unicode)
46 .unwrap();
47 } else {
48 item.insert("type", "tool_call").unwrap();
49 item.insert("toolCallId", format!("tc-{t}-{i}").as_str())
50 .unwrap();
51 let raw = item
52 .insert_container(
53 "rawInput",
54 loro_internal::handler::MapHandler::new_detached(),
55 )
56 .unwrap();
57 raw.insert("command", "pnpm test").unwrap();
58 raw.insert("cwd", "/repo").unwrap();
59 let text = item
60 .insert_container("title", loro_internal::handler::TextHandler::new_detached())
61 .unwrap();
62 text.insert(0, TEXT, PosType::Unicode).unwrap();
63 }
64 }
65 }
66 doc.export(loro_internal::encoding::ExportMode::snapshot())
67 .unwrap()
68}
69
70/// The loro-mirror-style traversal: keys()/get() per map, get(i) per list,
71/// to_string() per text.
72fn walk(h: &Handler, handles: &mut usize) {
73 *handles += 1;
74 match h {
75 Handler::Map(m) => {
76 let keys: Vec<_> = m.keys().collect();
77 for k in keys {
78 if let Some(ValueOrHandler::Handler(child)) = m.get_(&k) {
79 walk(&child, handles);
80 }
81 }
82 }
83 Handler::List(l) => {
84 for i in 0..l.len() {
85 if let Some(ValueOrHandler::Handler(child)) = l.get_(i) {
86 walk(&child, handles);
87 }
88 }
89 }
90 Handler::Text(t) => {
91 let _ = t.to_string();
92 }
93 _ => {}
94 }
95}
96
97#[test]
98fn handle_walk_retains_bounded_memory() {
99 let snapshot = build_snapshot(95, 100); // ≈ 63k containers
100 let doc = LoroDoc::new_auto_commit();
101 doc.import(&snapshot).unwrap();
102 let history = Handler::List(doc.get_list("history"));
103
104 // Every walk must visit all 13,260 containers (48 even turns × 5
105 // containers + 47 odd turns × 277 containers + the root list): a walk that
106 // silently visits fewer containers would pass the memory thresholds below
107 // more easily.
108 let expected_containers = 48 * 5 + 47 * 277 + 1;
109
110 let after_import = live_bytes();
111 let mut handles = 0;
112 walk(&history, &mut handles);
113 assert_eq!(handles, expected_containers, "first walk is incomplete");
114 let after_walk1 = live_bytes();
115 handles = 0;
116 walk(&history, &mut handles);
117 assert_eq!(handles, expected_containers, "second walk is incomplete");
118 let after_walk2 = live_bytes();
119
120 let walk1_retained = after_walk1.saturating_sub(after_import);
121 let walk2_growth = after_walk2.saturating_sub(after_walk1);
122 println!(
123 "handles={handles} retained after walk#1: {:.1} MiB, walk#2 growth: {:.1} MiB",
124 walk1_retained as f64 / MIB as f64,
125 walk2_growth as f64 / MIB as f64,
126 );
127
128 // Unbounded, this walk pinned ~1 KB per container (~60 MB at this size).
129 // The bounded cache (2048 entries) must retain far less than that.
130 assert!(
131 walk1_retained < 16 * MIB,
132 "walk retained {:.1} MiB; the decoded-value cache is not bounded",
133 walk1_retained as f64 / MIB as f64
134 );
135 // Re-walking hits the same bounded cache: no further growth.
136 assert!(
137 walk2_growth < 4 * MIB,
138 "second walk grew memory by {:.1} MiB; cached values must be reused or re-decoded, not accumulated",
139 walk2_growth as f64 / MIB as f64
140 );
141}