overgraph 0.7.0

An absurdly fast embedded graph database. Pure Rust, sub-microsecond reads.
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
// CP11: Async flush benchmarks. Phase 21 readiness gate.
//
// Measures write-burst throughput and mixed read/write performance under flush
// pressure, comparing sync-flush behavior (old) vs async auto-flush (Phase 21).
//
// Design: each scenario uses `iter_batched(PerIteration)` so every measurement
// starts from a clean DB, with no cross-iteration backpressure accumulation.
//
//   sync_baseline:    threshold=0, manual periodic flush() within the burst
//   async_auto_flush: threshold=ASYNC_THRESHOLD, auto-flush freezes + enqueues
//
// The sync baseline simulates the pre-Phase-21 behavior where maybe_auto_flush
// called flush() as a synchronous barrier, blocking the writer for the entire
// segment write. The async variant freezes the memtable and returns immediately.
//
// Run:       cargo bench --bench async_flush
// Specific:  cargo bench --bench async_flush -- "sustained_writes"

use criterion::{black_box, criterion_group, criterion_main, BatchSize, Criterion};
use overgraph::{
    DatabaseEngine, DbOptions, DenseMetric, DenseVectorConfig, HnswConfig, NodeInput, PropValue,
    UpsertNodeOptions, VectorSearchMode, VectorSearchRequest,
};
use std::collections::BTreeMap;

/// Nodes per write burst. With ~300 byte nodes and 1MB threshold, this
/// crosses the flush threshold ~3 times per burst.
const BURST_SIZE: u64 = 10_000;
/// Async auto-flush threshold in bytes.
const ASYNC_THRESHOLD: usize = 1024 * 1024;
/// Approximate writes between sync flush() calls, calibrated to match
/// the async threshold crossing frequency (~300 bytes/node → ~3300 per 1MB).
const SYNC_FLUSH_INTERVAL: u64 = 3300;

fn temp_db_with_opts(opts: DbOptions) -> (tempfile::TempDir, DatabaseEngine) {
    let dir = tempfile::tempdir().unwrap();
    let engine = DatabaseEngine::open(dir.path(), &opts).unwrap();
    (dir, engine)
}

fn sync_opts() -> DbOptions {
    DbOptions {
        create_if_missing: true,
        memtable_flush_threshold: 0, // disable auto-flush
        compact_after_n_flushes: 0,
        ..DbOptions::default()
    }
}

fn async_opts() -> DbOptions {
    DbOptions {
        create_if_missing: true,
        memtable_flush_threshold: ASYNC_THRESHOLD,
        compact_after_n_flushes: 0,
        ..DbOptions::default()
    }
}

/// Build UpsertNodeOptions with properties to make each node ~300 bytes
/// in the memtable, producing meaningful segment sizes on flush.
fn write_opts(i: u64) -> UpsertNodeOptions {
    let mut props = BTreeMap::new();
    props.insert(
        "name".to_string(),
        PropValue::String(format!("benchmark_node_{}", i)),
    );
    props.insert(
        "category".to_string(),
        PropValue::String("perf_test_data".to_string()),
    );
    props.insert("score".to_string(), PropValue::Float(i as f64 * 0.001));
    UpsertNodeOptions {
        props,
        ..Default::default()
    }
}

// ── Vector helpers ──────────────────────────────────────────────────────────

fn simple_dense_vector(dim: usize, index: usize) -> Vec<f32> {
    let mut v = vec![0.0f32; dim];
    let primary = index % dim;
    v[primary] = 1.0;
    v[(primary + 7) % dim] = 0.25;
    v[(index * 3 + 13) % dim] += 0.05;
    let norm = v.iter().map(|x| x * x).sum::<f32>().sqrt();
    if norm > 0.0 {
        for x in &mut v {
            *x /= norm;
        }
    }
    v
}

fn simple_sparse_vector(index: usize, nnz: usize) -> Vec<(u32, f32)> {
    let mut dims = Vec::with_capacity(nnz);
    for j in 0..nnz {
        let d = ((index * 7 + j * 31 + 13) % 4096) as u32;
        dims.push((d, 1.0 - j as f32 * 0.08));
    }
    dims.sort_unstable_by_key(|&(d, _)| d);
    dims.dedup_by_key(|(d, _)| *d);
    dims
}

fn pre_populate_plain(engine: &mut DatabaseEngine, count: usize) -> Vec<u64> {
    let inputs: Vec<NodeInput> = (0..count)
        .map(|i| NodeInput {
            type_id: 1,
            key: format!("seed_{}", i),
            props: BTreeMap::new(),
            weight: 1.0,
            dense_vector: None,
            sparse_vector: None,
        })
        .collect();
    let ids = engine.batch_upsert_nodes(&inputs).unwrap();
    engine.flush().unwrap();
    ids
}

fn pre_populate_dense(engine: &mut DatabaseEngine, count: usize, dim: usize) {
    let inputs: Vec<NodeInput> = (0..count)
        .map(|i| NodeInput {
            type_id: 1,
            key: format!("vec_{}", i),
            props: BTreeMap::new(),
            weight: 1.0,
            dense_vector: Some(simple_dense_vector(dim, i)),
            sparse_vector: None,
        })
        .collect();
    engine.batch_upsert_nodes(&inputs).unwrap();
    engine.flush().unwrap();
}

fn pre_populate_sparse(engine: &mut DatabaseEngine, count: usize, nnz: usize) {
    let inputs: Vec<NodeInput> = (0..count)
        .map(|i| NodeInput {
            type_id: 1,
            key: format!("svec_{}", i),
            props: BTreeMap::new(),
            weight: 1.0,
            dense_vector: None,
            sparse_vector: Some(simple_sparse_vector(i, nnz)),
        })
        .collect();
    engine.batch_upsert_nodes(&inputs).unwrap();
    engine.flush().unwrap();
}

// ── Scenario 1: Sustained writes crossing the soft threshold ────────────────
//
// Each iteration is a burst of BURST_SIZE node writes with properties.
// Sync baseline: manual flush() every SYNC_FLUSH_INTERVAL writes.
// Async: auto-flush triggers at ASYNC_THRESHOLD, returns immediately.

fn bench_sustained_writes_threshold(c: &mut Criterion) {
    let mut group = c.benchmark_group("async_flush_sustained_writes");
    group.sample_size(30);

    group.bench_function("sync_baseline", |b| {
        b.iter_batched(
            || temp_db_with_opts(sync_opts()),
            |(_dir, engine)| {
                for i in 0..BURST_SIZE {
                    engine
                        .upsert_node(1, &format!("n{}", i), write_opts(i))
                        .unwrap();
                    if (i + 1) % SYNC_FLUSH_INTERVAL == 0 {
                        engine.flush().unwrap();
                    }
                }
            },
            BatchSize::PerIteration,
        );
    });

    group.bench_function("async_auto_flush", |b| {
        b.iter_batched(
            || temp_db_with_opts(async_opts()),
            |(_dir, engine)| {
                for i in 0..BURST_SIZE {
                    engine
                        .upsert_node(1, &format!("n{}", i), write_opts(i))
                        .unwrap();
                }
            },
            BatchSize::PerIteration,
        );
    });

    group.finish();
}

// ── Scenario 2: Sustained writes while immutable epochs are already queued ──
//
// Pre-populates 2000 nodes (triggering several freezes in async mode), then
// measures another burst of writes under that existing queue pressure.

fn bench_writes_with_queued_epochs(c: &mut Criterion) {
    let mut group = c.benchmark_group("async_flush_queued_epochs");
    group.sample_size(30);

    group.bench_function("sync_baseline", |b| {
        b.iter_batched(
            || {
                let (_dir, engine) = temp_db_with_opts(sync_opts());
                for j in 0..2000u64 {
                    engine
                        .upsert_node(1, &format!("pre_{}", j), write_opts(j))
                        .unwrap();
                    if (j + 1) % SYNC_FLUSH_INTERVAL == 0 {
                        engine.flush().unwrap();
                    }
                }
                (_dir, engine)
            },
            |(_dir, engine)| {
                for i in 0..BURST_SIZE {
                    let k = 2000 + i;
                    engine
                        .upsert_node(1, &format!("n{}", k), write_opts(k))
                        .unwrap();
                    if (i + 1) % SYNC_FLUSH_INTERVAL == 0 {
                        engine.flush().unwrap();
                    }
                }
            },
            BatchSize::PerIteration,
        );
    });

    group.bench_function("async_auto_flush", |b| {
        b.iter_batched(
            || {
                let (_dir, engine) = temp_db_with_opts(async_opts());
                for j in 0..2000u64 {
                    engine
                        .upsert_node(1, &format!("pre_{}", j), write_opts(j))
                        .unwrap();
                }
                (_dir, engine)
            },
            |(_dir, engine)| {
                for i in 0..BURST_SIZE {
                    let k = 2000 + i;
                    engine
                        .upsert_node(1, &format!("n{}", k), write_opts(k))
                        .unwrap();
                }
            },
            BatchSize::PerIteration,
        );
    });

    group.finish();
}

// ── Scenario 3: Mixed writes + raw lookups under pending flush debt ─────────
//
// Pre-populates 1000 read-target nodes, then interleaves writes and reads.
// Measures combined write+read throughput under flush pressure.

fn bench_mixed_writes_reads(c: &mut Criterion) {
    let mut group = c.benchmark_group("async_flush_mixed_reads");
    group.sample_size(30);

    group.bench_function("sync_baseline", |b| {
        b.iter_batched(
            || {
                let (_dir, mut engine) = temp_db_with_opts(sync_opts());
                let ids = pre_populate_plain(&mut engine, 1000);
                (_dir, engine, ids)
            },
            |(_dir, engine, ids)| {
                for i in 0..BURST_SIZE {
                    engine
                        .upsert_node(1, &format!("w{}", i), write_opts(i))
                        .unwrap();
                    if (i + 1) % SYNC_FLUSH_INTERVAL == 0 {
                        engine.flush().unwrap();
                    }
                    black_box(engine.get_node(ids[(i as usize) % ids.len()]).unwrap());
                }
            },
            BatchSize::PerIteration,
        );
    });

    group.bench_function("async_auto_flush", |b| {
        b.iter_batched(
            || {
                let (_dir, mut engine) = temp_db_with_opts(async_opts());
                let ids = pre_populate_plain(&mut engine, 1000);
                (_dir, engine, ids)
            },
            |(_dir, engine, ids)| {
                for i in 0..BURST_SIZE {
                    engine
                        .upsert_node(1, &format!("w{}", i), write_opts(i))
                        .unwrap();
                    black_box(engine.get_node(ids[(i as usize) % ids.len()]).unwrap());
                }
            },
            BatchSize::PerIteration,
        );
    });

    group.finish();
}

// ── Scenario 4: Mixed writes + dense vector search ──────────────────────────
//
// Pre-populates 1000 nodes with dim-32 dense vectors, then interleaves
// writes and vector searches under flush pressure.

fn bench_mixed_writes_dense_vector(c: &mut Criterion) {
    let mut group = c.benchmark_group("async_flush_mixed_dense_vector");
    group.sample_size(10);

    let dim = 32usize;
    let dense_config = DenseVectorConfig {
        dimension: dim as u32,
        metric: DenseMetric::Cosine,
        hnsw: HnswConfig::default(),
    };
    let request = VectorSearchRequest {
        mode: VectorSearchMode::Dense,
        dense_query: Some(simple_dense_vector(dim, 999)),
        sparse_query: None,
        k: 10,
        type_filter: None,
        ef_search: None,
        scope: None,
        dense_weight: None,
        sparse_weight: None,
        fusion_mode: None,
    };

    group.bench_function("sync_baseline", |b| {
        b.iter_batched(
            || {
                let mut opts = sync_opts();
                opts.dense_vector = Some(dense_config.clone());
                let (_dir, mut engine) = temp_db_with_opts(opts);
                pre_populate_dense(&mut engine, 1000, dim);
                (_dir, engine)
            },
            |(_dir, engine)| {
                for i in 0..BURST_SIZE {
                    engine
                        .upsert_node(1, &format!("w{}", i), write_opts(i))
                        .unwrap();
                    if (i + 1) % SYNC_FLUSH_INTERVAL == 0 {
                        engine.flush().unwrap();
                    }
                    black_box(engine.vector_search(&request).unwrap());
                }
            },
            BatchSize::PerIteration,
        );
    });

    group.bench_function("async_auto_flush", |b| {
        b.iter_batched(
            || {
                let mut opts = async_opts();
                opts.dense_vector = Some(dense_config.clone());
                let (_dir, mut engine) = temp_db_with_opts(opts);
                pre_populate_dense(&mut engine, 1000, dim);
                (_dir, engine)
            },
            |(_dir, engine)| {
                for i in 0..BURST_SIZE {
                    engine
                        .upsert_node(1, &format!("w{}", i), write_opts(i))
                        .unwrap();
                    black_box(engine.vector_search(&request).unwrap());
                }
            },
            BatchSize::PerIteration,
        );
    });

    group.finish();
}

// ── Scenario 5: Mixed writes + sparse vector search ─────────────────────────
//
// Pre-populates 1000 nodes with 8-nnz sparse vectors over 4096 dimensions,
// then interleaves writes and sparse searches under flush pressure.

fn bench_mixed_writes_sparse_vector(c: &mut Criterion) {
    let mut group = c.benchmark_group("async_flush_mixed_sparse_vector");
    group.sample_size(10);

    let request = VectorSearchRequest {
        mode: VectorSearchMode::Sparse,
        dense_query: None,
        sparse_query: Some(simple_sparse_vector(999, 8)),
        k: 10,
        type_filter: None,
        ef_search: None,
        scope: None,
        dense_weight: None,
        sparse_weight: None,
        fusion_mode: None,
    };

    group.bench_function("sync_baseline", |b| {
        b.iter_batched(
            || {
                let (_dir, mut engine) = temp_db_with_opts(sync_opts());
                pre_populate_sparse(&mut engine, 1000, 8);
                (_dir, engine)
            },
            |(_dir, engine)| {
                for i in 0..BURST_SIZE {
                    engine
                        .upsert_node(1, &format!("w{}", i), write_opts(i))
                        .unwrap();
                    if (i + 1) % SYNC_FLUSH_INTERVAL == 0 {
                        engine.flush().unwrap();
                    }
                    black_box(engine.vector_search(&request).unwrap());
                }
            },
            BatchSize::PerIteration,
        );
    });

    group.bench_function("async_auto_flush", |b| {
        b.iter_batched(
            || {
                let (_dir, mut engine) = temp_db_with_opts(async_opts());
                pre_populate_sparse(&mut engine, 1000, 8);
                (_dir, engine)
            },
            |(_dir, engine)| {
                for i in 0..BURST_SIZE {
                    engine
                        .upsert_node(1, &format!("w{}", i), write_opts(i))
                        .unwrap();
                    black_box(engine.vector_search(&request).unwrap());
                }
            },
            BatchSize::PerIteration,
        );
    });

    group.finish();
}

criterion_group!(
    benches,
    bench_sustained_writes_threshold,
    bench_writes_with_queued_epochs,
    bench_mixed_writes_reads,
    bench_mixed_writes_dense_vector,
    bench_mixed_writes_sparse_vector,
);
criterion_main!(benches);