interstellar 0.2.0

A high-performance graph database with Gremlin-style traversals and GQL query language
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
//! Performance analysis benchmarks for Interstellar Graph Database.
//!
//! Run with: `cargo bench --bench perf_analysis`
//!
//! These benchmarks are designed to measure specific performance issues
//! documented in `perf-improvements/README.md` to:
//! 1. Establish baselines for optimization work
//! 2. Identify bottlenecks in the traversal engine
//! 3. Measure the impact of fixes
//!
//! Key issues being measured:
//! - Step count scaling (eager collection vs lazy evaluation)
//! - count() vs direct storage access
//! - Label comparison overhead (string vs ID)
//! - Navigation step overhead
//! - Concurrent read patterns

use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use interstellar::prelude::*;
use interstellar::storage::{Graph, GraphStorage};
use std::collections::HashMap;
use std::sync::Arc;
use std::thread;

// =============================================================================
// Helper Functions
// =============================================================================

/// Create a benchmark graph with specified number of vertices and edges.
///
/// Creates:
/// - `num_vertices` vertices with alternating "person" and "software" labels
/// - `num_edges` edges connecting vertices with "knows" or "uses" labels
fn create_benchmark_graph(num_vertices: usize, num_edges: usize) -> Graph {
    let graph = Graph::new();

    // Create vertices
    let mut vertex_ids = Vec::with_capacity(num_vertices);
    for i in 0..num_vertices {
        let (label, props) = if i % 2 == 0 {
            let mut props = HashMap::new();
            props.insert("name".to_string(), Value::String(format!("person_{}", i)));
            props.insert("age".to_string(), Value::Int((i % 100) as i64));
            ("person", props)
        } else {
            let mut props = HashMap::new();
            props.insert("name".to_string(), Value::String(format!("software_{}", i)));
            props.insert(
                "version".to_string(),
                Value::String(format!("1.{}", i % 10)),
            );
            ("software", props)
        };
        let id = graph.add_vertex(label, props);
        vertex_ids.push(id);
    }

    // Create edges - use deterministic pattern for reproducibility
    for i in 0..num_edges {
        let src_idx = i % num_vertices;
        let dst_idx = (i * 7 + 13) % num_vertices;

        // Skip self-loops
        if src_idx == dst_idx {
            continue;
        }

        let label = if i % 3 == 0 { "knows" } else { "uses" };
        let mut props = HashMap::new();
        props.insert("weight".to_string(), Value::Float((i % 100) as f64 / 10.0));

        // Ignore errors (duplicate edges are fine to skip)
        let _ = graph.add_edge(vertex_ids[src_idx], vertex_ids[dst_idx], label, props);
    }

    graph
}

/// Create a graph with many unique labels for label comparison benchmarks.
fn create_multi_label_graph(num_vertices: usize, num_labels: usize) -> Graph {
    let graph = Graph::new();

    for i in 0..num_vertices {
        let label = format!("label_{}", i % num_labels);
        let mut props = HashMap::new();
        props.insert("id".to_string(), Value::Int(i as i64));
        graph.add_vertex(&label, props);
    }

    graph
}

// =============================================================================
// Issue 1: Step Count Scaling (Eager Collection vs Lazy Evaluation)
// =============================================================================

/// Measures how traversal time scales with the number of steps.
///
/// If lazy evaluation is working, adding more steps should have minimal overhead
/// until terminal step execution. If eager collection is happening after each step,
/// time will scale linearly with step count.
///
/// Expected behavior (if lazy): O(N) regardless of step count
/// Actual behavior (with eager collect): O(N × steps)
fn bench_step_count_scaling(c: &mut Criterion) {
    let graph = create_benchmark_graph(1_000, 5_000);

    let mut group = c.benchmark_group("perf: step_count_scaling");
    group.throughput(Throughput::Elements(1_000));

    // 1 step
    group.bench_function("v().count()", |b| {
        b.iter(|| {
            let snapshot = graph.snapshot();
            let g = snapshot.gremlin();
            black_box(g.v().count())
        })
    });

    // 2 steps
    group.bench_function("v().out().count()", |b| {
        b.iter(|| {
            let snapshot = graph.snapshot();
            let g = snapshot.gremlin();
            black_box(g.v().out().count())
        })
    });

    // 3 steps
    group.bench_function("v().out().out().count()", |b| {
        b.iter(|| {
            let snapshot = graph.snapshot();
            let g = snapshot.gremlin();
            black_box(g.v().out().out().count())
        })
    });

    // 4 steps
    group.bench_function("v().out().out().out().count()", |b| {
        b.iter(|| {
            let snapshot = graph.snapshot();
            let g = snapshot.gremlin();
            black_box(g.v().out().out().out().count())
        })
    });

    // 5 steps (with dedup to limit explosion)
    group.bench_function("v().out().out().out().out().dedup().count()", |b| {
        b.iter(|| {
            let snapshot = graph.snapshot();
            let g = snapshot.gremlin();
            black_box(g.v().out().out().out().out().dedup().count())
        })
    });

    group.finish();
}

// =============================================================================
// Issue 2: count() vs Direct Storage Access
// =============================================================================

/// Compares g.v().count() (traversal) vs storage.vertex_count() (direct).
///
/// The traversal version should be nearly as fast as direct access for simple
/// counts. If it's significantly slower, it indicates eager materialization.
///
/// Current issue: count() calls execute() which collects ALL traversers,
/// then just returns .len() - wasting O(N) memory for O(1) operation.
fn bench_count_vs_direct(c: &mut Criterion) {
    let mut group = c.benchmark_group("perf: count_vs_direct");

    for size in [1_000, 10_000, 100_000] {
        let graph = create_benchmark_graph(size, 0);

        group.throughput(Throughput::Elements(size as u64));

        // Traversal count (current - eager)
        group.bench_with_input(BenchmarkId::new("g.v().count()", size), &size, |b, _| {
            b.iter(|| {
                let snapshot = graph.snapshot();
                let g = snapshot.gremlin();
                black_box(g.v().count())
            })
        });

        // Direct storage count (baseline)
        group.bench_with_input(
            BenchmarkId::new("snapshot.vertex_count()", size),
            &size,
            |b, _| {
                b.iter(|| {
                    let snapshot = graph.snapshot();
                    black_box(snapshot.vertex_count())
                })
            },
        );

        // Iterator count (middle ground)
        group.bench_with_input(
            BenchmarkId::new("all_vertices().count()", size),
            &size,
            |b, _| {
                b.iter(|| {
                    let snapshot = graph.snapshot();
                    black_box(snapshot.all_vertices().count())
                })
            },
        );
    }

    group.finish();
}

// =============================================================================
// Issue 3: Label Comparison Overhead (String vs ID)
// =============================================================================

/// Measures has_label() performance with varying numbers of unique labels.
///
/// Current issue: HasLabelStep compares strings instead of interned label IDs.
/// With many unique labels, string comparison becomes expensive.
///
/// This benchmark tests:
/// - 1 unique label (best case for string comparison)
/// - 10 unique labels (moderate)
/// - 100 unique labels (worst case - long strings, many comparisons)
fn bench_has_label_scaling(c: &mut Criterion) {
    let mut group = c.benchmark_group("perf: has_label_scaling");
    let num_vertices = 10_000;

    for num_labels in [1, 10, 100] {
        let graph = create_multi_label_graph(num_vertices, num_labels);
        let target_label = "label_0"; // Always exists

        group.bench_with_input(
            BenchmarkId::new("has_label()", num_labels),
            &num_labels,
            |b, _| {
                b.iter(|| {
                    let snapshot = graph.snapshot();
                    let g = snapshot.gremlin();
                    black_box(g.v().has_label(target_label).count())
                })
            },
        );
    }

    // Compare against direct iteration with label filter
    let graph = create_multi_label_graph(num_vertices, 100);
    group.bench_function("all_vertices().filter(label==)", |b| {
        b.iter(|| {
            let snapshot = graph.snapshot();
            black_box(
                snapshot
                    .all_vertices()
                    .filter(|v| v.label == "label_0")
                    .count(),
            )
        })
    });

    group.finish();
}

// =============================================================================
// Issue 4: Navigation Step Overhead
// =============================================================================

/// Measures the overhead of navigation steps (out, in, both).
///
/// Each navigation step:
/// - Allocates a label Vec (if labels specified)
/// - Boxes the iterator
/// - Collects results (due to eager execution)
///
/// This benchmark isolates navigation overhead from step count scaling.
fn bench_navigation_step_overhead(c: &mut Criterion) {
    let graph = create_benchmark_graph(1_000, 5_000);

    let mut group = c.benchmark_group("perf: navigation_overhead");

    // Single vertex, out() - measures pure navigation cost
    group.bench_function("v(0).out().count()", |b| {
        b.iter(|| {
            let snapshot = graph.snapshot();
            let g = snapshot.gremlin();
            black_box(g.v_ids([VertexId(0)]).out().count())
        })
    });

    // Single vertex, out() with label filter
    group.bench_function("v(0).out(\"knows\").count()", |b| {
        b.iter(|| {
            let snapshot = graph.snapshot();
            let g = snapshot.gremlin();
            black_box(g.v_ids([VertexId(0)]).out_labels(&["knows"]).count())
        })
    });

    // Single vertex, out_e().in_v() - explicit edge traversal
    group.bench_function("v(0).out_e().in_v().count()", |b| {
        b.iter(|| {
            let snapshot = graph.snapshot();
            let g = snapshot.gremlin();
            black_box(g.v_ids([VertexId(0)]).out_e().in_v().count())
        })
    });

    // Direct storage access (baseline)
    group.bench_function("storage.out_edges(0).count()", |b| {
        b.iter(|| {
            let snapshot = graph.snapshot();
            black_box(snapshot.out_edges(VertexId(0)).count())
        })
    });

    group.finish();
}

// =============================================================================
// Issue 5: Iterator Boxing Overhead
// =============================================================================

/// Measures the overhead of boxed iterators in the storage layer.
///
/// Every storage method returns Box<dyn Iterator>, which:
/// - Allocates on the heap
/// - Prevents inlining
/// - Adds vtable dispatch overhead
///
/// This compares boxed (current) vs what concrete iterators might achieve.
fn bench_iterator_boxing(c: &mut Criterion) {
    let graph = create_benchmark_graph(10_000, 50_000);

    let mut group = c.benchmark_group("perf: iterator_boxing");
    group.throughput(Throughput::Elements(10_000));

    // Current: all_vertices() returns Box<dyn Iterator>
    group.bench_function("all_vertices() [boxed]", |b| {
        b.iter(|| {
            let snapshot = graph.snapshot();
            black_box(snapshot.all_vertices().count())
        })
    });

    // Comparison: collect to vec then iterate (removes boxing overhead, adds allocation)
    // This helps isolate whether boxing or the iterator itself is the bottleneck
    group.bench_function("all_vertices().collect::<Vec<_>>().len()", |b| {
        b.iter(|| {
            let snapshot = graph.snapshot();
            let vertices: Vec<_> = snapshot.all_vertices().collect();
            black_box(vertices.len())
        })
    });

    group.finish();
}

// =============================================================================
// Issue 7: Concurrent Read Patterns
// =============================================================================

/// Measures performance under concurrent read access.
///
/// Current issue: Multiple lock acquisitions within single methods may cause
/// contention under concurrent access.
///
/// This benchmark compares:
/// - Single-threaded baseline
/// - Multi-threaded with potential contention
fn bench_concurrent_reads(c: &mut Criterion) {
    let graph = Arc::new(create_benchmark_graph(10_000, 50_000));

    let mut group = c.benchmark_group("perf: concurrent_reads");

    // Single-threaded baseline
    group.bench_function("single_thread: 1000 reads", |b| {
        b.iter(|| {
            let snapshot = graph.snapshot();
            for i in 0..1000 {
                black_box(snapshot.get_vertex(VertexId(i % 10_000)));
            }
        })
    });

    // Multi-threaded (4 threads)
    group.bench_function("4_threads: 250 reads each", |b| {
        b.iter(|| {
            let handles: Vec<_> = (0..4)
                .map(|thread_id| {
                    let graph_clone = Arc::clone(&graph);
                    thread::spawn(move || {
                        let snapshot = graph_clone.snapshot();
                        for i in 0..250 {
                            let vertex_id = (thread_id * 250 + i) % 10_000;
                            black_box(snapshot.get_vertex(VertexId(vertex_id as u64)));
                        }
                    })
                })
                .collect();

            for handle in handles {
                handle.join().unwrap();
            }
        })
    });

    // Multi-threaded traversals
    group.bench_function("4_threads: v().count() each", |b| {
        b.iter(|| {
            let handles: Vec<_> = (0..4)
                .map(|_| {
                    let graph_clone = Arc::clone(&graph);
                    thread::spawn(move || {
                        let snapshot = graph_clone.snapshot();
                        let g = snapshot.gremlin();
                        black_box(g.v().count())
                    })
                })
                .collect();

            for handle in handles {
                handle.join().unwrap();
            }
        })
    });

    group.finish();
}

// =============================================================================
// Additional: Memory Pressure Benchmarks
// =============================================================================

/// Measures how traversal performance degrades under memory pressure.
///
/// The eager collection after each step creates O(N × steps) intermediate
/// allocations. This benchmark shows the impact on larger graphs.
fn bench_memory_pressure(c: &mut Criterion) {
    let mut group = c.benchmark_group("perf: memory_pressure");

    for size in [1_000, 10_000, 50_000] {
        let graph = create_benchmark_graph(size, size * 5);

        group.throughput(Throughput::Elements(size as u64));

        // Simple count (baseline)
        group.bench_with_input(BenchmarkId::new("v().count()", size), &size, |b, _| {
            b.iter(|| {
                let snapshot = graph.snapshot();
                let g = snapshot.gremlin();
                black_box(g.v().count())
            })
        });

        // Two-hop with dedup (moderate pressure)
        group.bench_with_input(
            BenchmarkId::new("v().out().out().dedup().count()", size),
            &size,
            |b, _| {
                b.iter(|| {
                    let snapshot = graph.snapshot();
                    let g = snapshot.gremlin();
                    black_box(g.v().out().out().dedup().count())
                })
            },
        );
    }

    group.finish();
}

// =============================================================================
// Summary: Key Metrics Benchmark
// =============================================================================

/// A quick summary benchmark that shows key performance metrics.
///
/// Run this to get a quick overview of current performance characteristics.
fn bench_key_metrics(c: &mut Criterion) {
    let graph = create_benchmark_graph(10_000, 50_000);

    let mut group = c.benchmark_group("perf: key_metrics");

    // Metric 1: Simple count
    group.bench_function("v().count() [10K vertices]", |b| {
        b.iter(|| {
            let snapshot = graph.snapshot();
            let g = snapshot.gremlin();
            black_box(g.v().count())
        })
    });

    // Metric 2: Filtered count
    group.bench_function("v().has_label().count() [10K vertices]", |b| {
        b.iter(|| {
            let snapshot = graph.snapshot();
            let g = snapshot.gremlin();
            black_box(g.v().has_label("person").count())
        })
    });

    // Metric 3: Two-hop traversal
    group.bench_function(
        "v().out().out().dedup().count() [10K vertices, 50K edges]",
        |b| {
            b.iter(|| {
                let snapshot = graph.snapshot();
                let g = snapshot.gremlin();
                black_box(g.v().out().out().dedup().count())
            })
        },
    );

    // Metric 4: Direct storage (baseline)
    group.bench_function("vertex_count() [direct]", |b| {
        b.iter(|| {
            let snapshot = graph.snapshot();
            black_box(snapshot.vertex_count())
        })
    });

    group.finish();
}

// =============================================================================
// Criterion Groups
// =============================================================================

criterion_group!(step_scaling, bench_step_count_scaling,);

criterion_group!(count_optimization, bench_count_vs_direct,);

criterion_group!(label_comparison, bench_has_label_scaling,);

criterion_group!(
    navigation,
    bench_navigation_step_overhead,
    bench_iterator_boxing,
);

criterion_group!(concurrency, bench_concurrent_reads,);

criterion_group!(memory, bench_memory_pressure,);

criterion_group!(key_metrics, bench_key_metrics,);

criterion_main!(
    step_scaling,
    count_optimization,
    label_comparison,
    navigation,
    concurrency,
    memory,
    key_metrics
);