magellan 3.3.5

Deterministic codebase mapping tool for local development
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
//! Performance benchmarks for graph algorithms (Phase 40)
//!
//! These benchmarks measure algorithm performance on graphs of various sizes.
//! Run with: cargo test --test algo_benchmarks -- --ignored --nocapture --test-threads=1
//!
//! Benchmarks are marked #[ignore] by default to avoid slowing down normal test runs.
//! To run benchmarks:
//!     cargo test --test algo_benchmarks -- --ignored --nocapture --test-threads=1

use std::time::Instant;
use tempfile::TempDir;

#[test]
#[ignore]
fn benchmark_reachability_100_symbols() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.db");

    // Build source code with a chain of functions
    let n = 100;
    let mut source = String::from("fn main() {\n    fn1();\n}\n\n");
    for i in 1..=n {
        if i < n {
            source.push_str(&format!("fn fn{}() {{ fn{}(); }}\n\n", i, i + 1));
        } else {
            source.push_str(&format!("fn fn{}() {{}}\n", i));
        }
    }

    let mut graph = magellan::CodeGraph::open(&db_path).unwrap();
    let path_str = db_path.to_string_lossy().to_string();

    graph.index_file(&path_str, source.as_bytes()).unwrap();
    graph.index_calls(&path_str, source.as_bytes()).unwrap();

    // Get main's FQN for querying
    let symbols = graph.symbols_in_file(&path_str).unwrap();
    let main_symbol = symbols
        .iter()
        .find(|s| s.name.as_deref() == Some("main"))
        .expect("Should find main symbol");
    let main_fqn = main_symbol
        .fqn
        .as_ref()
        .or(main_symbol.canonical_fqn.as_ref())
        .expect("main should have FQN");

    let start = Instant::now();
    let result = graph.reachable_symbols(main_fqn, None).unwrap();
    let elapsed = start.elapsed();

    println!("Reachability (100 symbols): {:?}", elapsed);
    println!("  Found {} reachable symbols", result.len());

    assert!(
        elapsed.as_millis() < 100,
        "Reachability on 100 symbols should complete in <100ms"
    );
}

#[test]
#[ignore]
fn benchmark_reachability_1000_symbols() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.db");

    let n = 1000;
    let mut source = String::from("fn main() {\n    fn1();\n}\n\n");
    for i in 1..=n {
        if i < n {
            source.push_str(&format!("fn fn{}() {{ fn{}(); }}\n\n", i, i + 1));
        } else {
            source.push_str(&format!("fn fn{}() {{}}\n", i));
        }
    }

    let mut graph = magellan::CodeGraph::open(&db_path).unwrap();
    let path_str = db_path.to_string_lossy().to_string();

    graph.index_file(&path_str, source.as_bytes()).unwrap();
    graph.index_calls(&path_str, source.as_bytes()).unwrap();

    let start = Instant::now();
    let result = graph.reachable_symbols("main", None).unwrap();
    let elapsed = start.elapsed();

    println!("Reachability (1000 symbols): {:?}", elapsed);
    println!("  Found {} reachable symbols", result.len());

    assert!(
        elapsed.as_secs() < 1,
        "Reachability on 1000 symbols should complete in <1s"
    );
}

#[test]
#[ignore]
fn benchmark_scc_detection_100_symbols() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.db");

    let n = 100;
    let mut source = String::from("fn main() {\n    fn1();\n}\n\n");
    for i in 1..=n {
        if i < n {
            source.push_str(&format!("fn fn{}() {{ fn{}(); }}\n\n", i, i + 1));
        } else {
            source.push_str(&format!("fn fn{}() {{}}\n", i));
        }
    }

    let mut graph = magellan::CodeGraph::open(&db_path).unwrap();
    let path_str = db_path.to_string_lossy().to_string();

    graph.index_file(&path_str, source.as_bytes()).unwrap();
    graph.index_calls(&path_str, source.as_bytes()).unwrap();

    let start = Instant::now();
    let result = graph.detect_cycles().unwrap();
    let elapsed = start.elapsed();

    println!("SCC detection (100 symbols): {:?}", elapsed);
    println!("  Found {} cycles", result.cycles.len());

    assert!(
        elapsed.as_millis() < 100,
        "SCC detection on 100 symbols should complete in <100ms"
    );
}

#[test]
#[ignore]
fn benchmark_scc_detection_1000_symbols() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.db");

    let n = 1000;
    let mut source = String::from("fn main() {\n    fn1();\n}\n\n");
    for i in 1..=n {
        if i < n {
            source.push_str(&format!("fn fn{}() {{ fn{}(); }}\n\n", i, i + 1));
        } else {
            source.push_str(&format!("fn fn{}() {{}}\n", i));
        }
    }

    let mut graph = magellan::CodeGraph::open(&db_path).unwrap();
    let path_str = db_path.to_string_lossy().to_string();

    graph.index_file(&path_str, source.as_bytes()).unwrap();
    graph.index_calls(&path_str, source.as_bytes()).unwrap();

    let start = Instant::now();
    let result = graph.detect_cycles().unwrap();
    let elapsed = start.elapsed();

    println!("SCC detection (1000 symbols): {:?}", elapsed);
    println!("  Found {} cycles", result.cycles.len());

    assert!(
        elapsed.as_secs() < 1,
        "SCC detection on 1000 symbols should complete in <1s"
    );
}

#[test]
#[ignore]
fn benchmark_path_enumeration_with_bounds() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.db");

    let n = 100;
    let mut source = String::from("fn main() {\n    fn1();\n}\n\n");
    for i in 1..=n {
        if i < n {
            source.push_str(&format!("fn fn{}() {{ fn{}(); }}\n\n", i, i + 1));
        } else {
            source.push_str(&format!("fn fn{}() {{}}\n", i));
        }
    }

    let mut graph = magellan::CodeGraph::open(&db_path).unwrap();
    let path_str = db_path.to_string_lossy().to_string();

    graph.index_file(&path_str, source.as_bytes()).unwrap();
    graph.index_calls(&path_str, source.as_bytes()).unwrap();

    let start = Instant::now();
    let result = graph.enumerate_paths("main", None, 10, 100).unwrap();
    let elapsed = start.elapsed();

    println!(
        "Path enumeration with bounds (100 symbols, max_depth=10, max_paths=100): {:?}",
        elapsed
    );
    println!(
        "  Found {} paths, enumerated {}",
        result.paths.len(),
        result.total_enumerated
    );

    assert!(
        elapsed.as_secs() < 5,
        "Path enumeration with bounds should complete in <5s"
    );
}

#[test]
#[ignore]
fn benchmark_backward_slice_100_symbols() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.db");

    let n = 100;
    let mut source = String::from("fn main() {\n    fn1();\n}\n\n");
    for i in 1..=n {
        if i < n {
            source.push_str(&format!("fn fn{}() {{ fn{}(); }}\n\n", i, i + 1));
        } else {
            source.push_str(&format!("fn fn{}() {{}}\n", i));
        }
    }

    let mut graph = magellan::CodeGraph::open(&db_path).unwrap();
    let path_str = db_path.to_string_lossy().to_string();

    graph.index_file(&path_str, source.as_bytes()).unwrap();
    graph.index_calls(&path_str, source.as_bytes()).unwrap();

    let start = Instant::now();
    let result = graph.backward_slice("fn50").unwrap();
    let elapsed = start.elapsed();

    println!("Backward slice (100 symbols, from middle): {:?}", elapsed);
    println!("  Slice size: {}", result.slice.symbol_count);

    assert!(
        elapsed.as_millis() < 100,
        "Backward slice on 100 symbols should complete in <100ms"
    );
}

#[test]
#[ignore]
fn benchmark_forward_slice_100_symbols() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.db");

    let n = 100;
    let mut source = String::from("fn main() {\n    fn1();\n}\n\n");
    for i in 1..=n {
        if i < n {
            source.push_str(&format!("fn fn{}() {{ fn{}(); }}\n\n", i, i + 1));
        } else {
            source.push_str(&format!("fn fn{}() {{}}\n", i));
        }
    }

    let mut graph = magellan::CodeGraph::open(&db_path).unwrap();
    let path_str = db_path.to_string_lossy().to_string();

    graph.index_file(&path_str, source.as_bytes()).unwrap();
    graph.index_calls(&path_str, source.as_bytes()).unwrap();

    let start = Instant::now();
    let result = graph.forward_slice("fn50").unwrap();
    let elapsed = start.elapsed();

    println!("Forward slice (100 symbols, from middle): {:?}", elapsed);
    println!("  Slice size: {}", result.slice.symbol_count);

    assert!(
        elapsed.as_millis() < 100,
        "Forward slice on 100 symbols should complete in <100ms"
    );
}

#[test]
#[ignore]
fn benchmark_dead_code_detection_1000_symbols() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.db");

    let n = 1000;
    let mut source = String::from("fn main() {\n    fn1();\n}\n\n");
    for i in 1..=n {
        if i < n {
            source.push_str(&format!("fn fn{}() {{ fn{}(); }}\n\n", i, i + 1));
        } else {
            source.push_str(&format!("fn fn{}() {{}}\n", i));
        }
    }

    let mut graph = magellan::CodeGraph::open(&db_path).unwrap();
    let path_str = db_path.to_string_lossy().to_string();

    graph.index_file(&path_str, source.as_bytes()).unwrap();
    graph.index_calls(&path_str, source.as_bytes()).unwrap();

    let start = Instant::now();
    let result = graph.dead_symbols("main").unwrap();
    let elapsed = start.elapsed();

    println!("Dead code detection (1000 symbols): {:?}", elapsed);
    println!("  Found {} dead symbols", result.len());

    assert!(
        elapsed.as_secs() < 1,
        "Dead code detection on 1000 symbols should complete in <1s"
    );
}

#[test]
#[ignore]
fn benchmark_condensation_1000_symbols() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.db");

    let n = 1000;
    let mut source = String::from("fn main() {\n    fn1();\n}\n\n");
    for i in 1..=n {
        if i < n {
            source.push_str(&format!("fn fn{}() {{ fn{}(); }}\n\n", i, i + 1));
        } else {
            source.push_str(&format!("fn fn{}() {{}}\n", i));
        }
    }

    let mut graph = magellan::CodeGraph::open(&db_path).unwrap();
    let path_str = db_path.to_string_lossy().to_string();

    graph.index_file(&path_str, source.as_bytes()).unwrap();
    graph.index_calls(&path_str, source.as_bytes()).unwrap();

    let start = Instant::now();
    let result = graph.condense_call_graph().unwrap();
    let elapsed = start.elapsed();

    println!("Condensation (1000 symbols): {:?}", elapsed);
    println!("  Created {} supernodes", result.graph.supernodes.len());

    assert!(
        elapsed.as_secs() < 1,
        "Condensation on 1000 symbols should complete in <1s"
    );
}

#[test]
#[ignore]
fn benchmark_branching_graph_reachability() {
    // Create a graph with 3-way branching, depth 5 = ~363 symbols
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.db");

    let branching_factor: u32 = 3;
    let depth: u32 = 5;

    let mut source = String::from("fn main() {\n");
    for i in 0..branching_factor {
        source.push_str(&format!("    branch_0_{}();\n", i));
    }
    source.push_str("}\n\n");

    let mut count = 0;
    for level in 0..depth {
        for _parent in 0..branching_factor.pow(level) {
            for child in 0..branching_factor {
                let name = format!("branch_{}_{}", level, count);
                if level < depth - 1 {
                    source.push_str(&format!(
                        "fn {}() {{ branch_{}_{}(); }}\n",
                        name,
                        level + 1,
                        child * branching_factor + child
                    ));
                } else {
                    source.push_str(&format!("fn {}() {{}}\n", name));
                }
                count += 1;
            }
        }
        source.push('\n');
    }

    let mut graph = magellan::CodeGraph::open(&db_path).unwrap();
    let path_str = db_path.to_string_lossy().to_string();

    graph.index_file(&path_str, source.as_bytes()).unwrap();
    graph.index_calls(&path_str, source.as_bytes()).unwrap();

    let start = Instant::now();
    let result = graph.reachable_symbols("main", None).unwrap();
    let elapsed = start.elapsed();

    println!(
        "Reachability on branching graph (3 branches, depth 5): {:?}",
        elapsed
    );
    println!("  Found {} reachable symbols", result.len());

    assert!(
        elapsed.as_millis() < 100,
        "Reachability on branching graph should complete in <100ms"
    );
}