| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | use dev_utils::get_mem_usage; |
| 9 | use loro_internal::cursor::PosType; |
| 10 | use loro_internal::handler::{Handler, ValueOrHandler}; |
| 11 | use loro_internal::LoroDoc; |
| 12 | |
| 13 | fn live_bytes() -> usize { |
| 14 | get_mem_usage().0 |
| 15 | } |
| 16 | |
| 17 | const MIB: usize = 1 << 20; |
| 18 | |
| 19 | const TEXT: &str = "The quick brown fox jumps over the lazy dog. \ |
| 20 | The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog."; |
| 21 | |
| 22 | fn 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 | |
| 71 | |
| 72 | fn 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] |
| 98 | fn handle_walk_retains_bounded_memory() { |
| 99 | let snapshot = build_snapshot(95, 100); |
| 100 | let doc = LoroDoc::new_auto_commit(); |
| 101 | doc.import(&snapshot).unwrap(); |
| 102 | let history = Handler::List(doc.get_list("history")); |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 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 | |
| 129 | |
| 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 | |
| 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 | } |