safegraph 0.1.0

A type-safe, scope-aware graph library that leverages Rust's type system to prevent common graph-related bugs at compile time
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
//! Criterion benchmark comparing basic graph-operation costs across the
//! safegraph backends and petgraph.
//!
//! Six groups — `creation`, `traversal`, `random_access`, `remove_edges`,
//! `remove_nodes` (criterion's default wall-clock measurement), plus `memory`
//! (a custom [`Measurement`] reading a tracking global allocator to report the
//! live heap bytes each built graph occupies). Every contender comes from
//! `adapters.rs`, run on the shared workloads from `common.rs`. A
//! `Once`-guarded [`sanity`] gate proves all contenders do equivalent work
//! before any measurement (a mismatch panics with the contender name).

// Benchmarks are a dev-only target and are not bound by the crate's MSRV, so
// they may use newer std APIs than `rust-version` allows.
#![allow(clippy::incompatible_msrv)]

mod adapters;
mod common;

use std::alloc::{GlobalAlloc, Layout, System};
use std::hint::black_box;
use std::sync::atomic::{AtomicI64, Ordering};
use std::sync::Once;

use criterion::measurement::{Measurement, ValueFormatter};
use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion, Throughput};

use common::{
    edge_is_victim, generate_workload, node_is_victim, shuffled_ordinals, Workload, SEED, SIZES,
};

// ---------------------------------------------------------------------------
// Tracking global allocator + a criterion `Measurement` over live heap bytes.
//
// `LIVE` is net live bytes (allocated minus freed). The `memory` group brackets
// each graph build with `live_bytes()` to read the graph's heap footprint. The
// per-allocation atomic adds a small, uniform overhead to the wall-clock groups
// too — fine for backend-vs-backend comparison.
// ---------------------------------------------------------------------------

static LIVE: AtomicI64 = AtomicI64::new(0);

struct TrackingAllocator;

unsafe impl GlobalAlloc for TrackingAllocator {
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        let p = System.alloc(layout);
        if !p.is_null() {
            LIVE.fetch_add(layout.size() as i64, Ordering::Relaxed);
        }
        p
    }
    unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
        System.dealloc(ptr, layout);
        LIVE.fetch_sub(layout.size() as i64, Ordering::Relaxed);
    }
    unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
        let p = System.realloc(ptr, layout, new_size);
        if !p.is_null() {
            LIVE.fetch_add(new_size as i64 - layout.size() as i64, Ordering::Relaxed);
        }
        p
    }
}

#[global_allocator]
static GLOBAL: TrackingAllocator = TrackingAllocator;

fn live_bytes() -> i64 {
    LIVE.load(Ordering::Relaxed)
}

/// Criterion measurement whose value is bytes (net live heap allocation).
struct AllocBytes;

static BYTE_FORMATTER: ByteFormatter = ByteFormatter;

impl Measurement for AllocBytes {
    type Intermediate = i64;
    type Value = u64;
    // start/end are unused here (the `memory` group uses `iter_custom`, which
    // returns the value directly), but the trait requires them.
    fn start(&self) -> i64 {
        live_bytes()
    }
    fn end(&self, i: i64) -> u64 {
        (live_bytes() - i).max(0) as u64
    }
    fn add(&self, a: &u64, b: &u64) -> u64 {
        a + b
    }
    fn zero(&self) -> u64 {
        0
    }
    fn to_f64(&self, v: &u64) -> f64 {
        *v as f64
    }
    fn formatter(&self) -> &dyn ValueFormatter {
        &BYTE_FORMATTER
    }
}

struct ByteFormatter;

impl ByteFormatter {
    fn scale(typical: f64, values: &mut [f64]) -> &'static str {
        let (div, unit) = if typical >= 1024.0 * 1024.0 {
            (1024.0 * 1024.0, "MiB")
        } else if typical >= 1024.0 {
            (1024.0, "KiB")
        } else {
            (1.0, "B")
        };
        for v in values.iter_mut() {
            *v /= div;
        }
        unit
    }
}

impl ValueFormatter for ByteFormatter {
    fn scale_values(&self, typical: f64, values: &mut [f64]) -> &'static str {
        Self::scale(typical, values)
    }
    fn scale_throughputs(
        &self,
        typical: f64,
        _throughput: &Throughput,
        values: &mut [f64],
    ) -> &'static str {
        // Bytes have no meaningful throughput notion here; report bytes.
        Self::scale(typical, values)
    }
    fn scale_for_machines(&self, _values: &mut [f64]) -> &'static str {
        "bytes"
    }
}

// ---------------------------------------------------------------------------
// Sanity gate: workload equivalence across all contenders.
// ---------------------------------------------------------------------------

fn sanity() {
    static ONCE: Once = Once::new();
    ONCE.call_once(|| {
        // Validate the two smaller sizes (skip 10k for startup time).
        for &(n, m) in &SIZES[..2] {
            run_sanity(&generate_workload(n, m, SEED));
        }
        // Self-loop + adjacent-edge micro-case: the self-loop must be counted
        // exactly once (outgoing-only) on every contender.
        run_sanity(&Workload {
            n: 2,
            edges: vec![(0, 0), (0, 1)],
        });
    });
}

fn run_sanity(w: &Workload) {
    let n = w.n;
    let m = w.edges.len();

    // Order/multiplicity-independent payload checksum.
    let node_sum = (0..n).fold(0usize, |a, i| a.wrapping_add(i));
    let edge_sum = (0..m).fold(0usize, |a, j| a.wrapping_add(j));
    let checksum = node_sum.wrapping_add(edge_sum);

    let edge_victims = (0..m).filter(|&j| edge_is_victim(j)).count();
    let node_victims = (0..n).filter(|&v| node_is_victim(v)).count();
    let edges_after_node_removal = w
        .edges
        .iter()
        .filter(|&&(f, t)| !node_is_victim(f as usize) && !node_is_victim(t as usize))
        .count();

    macro_rules! check {
        ($a:path, $name:literal) => {{
            use $a as ad;
            let g = ad::build(w);
            assert_eq!(ad::counts(&g), (n, m), concat!($name, " counts"));
            assert_eq!(ad::traverse_sum(&g), checksum, concat!($name, " checksum"));
            assert_eq!(ad::out_degree_sum(&g), m, concat!($name, " out-degree coverage"));

            let mut ge = g.clone();
            ad::remove_edge_set(&mut ge);
            assert_eq!(
                ad::counts(&ge),
                (n, m - edge_victims),
                concat!($name, " edge-remove counts")
            );

            let mut gn = g.clone();
            ad::remove_node_set(&mut gn);
            assert_eq!(
                ad::counts(&gn),
                (n - node_victims, edges_after_node_removal),
                concat!($name, " node-remove counts")
            );
        }};
    }

    check!(adapters::sg_vec_scoped, "sg_vec_scoped");
    check!(adapters::sg_vec_stabilized, "sg_vec_stabilized");
    check!(adapters::sg_vec_checked, "sg_vec_checked");
    check!(adapters::sg_flat, "sg_flat");
    check!(adapters::sg_btree, "sg_btree");
    check!(adapters::pg, "pg");
    check!(adapters::pg_stable, "pg_stable");

    // pg_stable loop rows must match its retain rows.
    {
        let g = adapters::pg_stable::build(w);
        let mut ge = g.clone();
        adapters::pg_stable::remove_edge_loop(&mut ge);
        assert_eq!(
            adapters::pg_stable::counts(&ge),
            (n, m - edge_victims),
            "pg_stable edge-loop counts"
        );
        let mut gn = g.clone();
        adapters::pg_stable::remove_node_loop(&mut gn);
        assert_eq!(
            adapters::pg_stable::counts(&gn),
            (n - node_victims, edges_after_node_removal),
            "pg_stable node-loop counts"
        );
    }
}

// ---------------------------------------------------------------------------
// Bench groups.
// ---------------------------------------------------------------------------

fn bench_creation(c: &mut Criterion) {
    sanity();
    let mut group = c.benchmark_group("creation");
    for &(n, m) in SIZES {
        let w = generate_workload(n, m, SEED);
        group.throughput(Throughput::Elements((n + m) as u64));
        macro_rules! row {
            ($a:path, $name:literal) => {{
                use $a as ad;
                group.bench_with_input(BenchmarkId::new($name, n), &w, |b, w| {
                    b.iter(|| black_box(ad::build(w)))
                });
            }};
        }
        row!(adapters::sg_vec_scoped, "sg_vec_scoped");
        row!(adapters::sg_vec_stabilized, "sg_vec_stabilized");
        row!(adapters::sg_vec_checked, "sg_vec_checked");
        row!(adapters::sg_flat, "sg_flat");
        row!(adapters::sg_btree, "sg_btree");
        row!(adapters::pg, "pg");
        row!(adapters::pg_stable, "pg_stable");
    }
    group.finish();
}

fn bench_traversal(c: &mut Criterion) {
    sanity();
    let mut group = c.benchmark_group("traversal");
    for &(n, m) in SIZES {
        let w = generate_workload(n, m, SEED);
        group.throughput(Throughput::Elements((n + m) as u64));
        macro_rules! row {
            ($a:path, $name:literal) => {{
                use $a as ad;
                let g = ad::build(&w);
                group.bench_with_input(BenchmarkId::new($name, n), &g, |b, g| {
                    b.iter(|| black_box(ad::traverse_sum(g)))
                });
            }};
        }
        row!(adapters::sg_vec_scoped, "sg_vec_scoped");
        row!(adapters::sg_vec_stabilized, "sg_vec_stabilized");
        row!(adapters::sg_vec_checked, "sg_vec_checked");
        row!(adapters::sg_flat, "sg_flat");
        row!(adapters::sg_btree, "sg_btree");
        row!(adapters::pg, "pg");
        row!(adapters::pg_stable, "pg_stable");
    }
    group.finish();
}

fn bench_random_access(c: &mut Criterion) {
    sanity();
    let mut group = c.benchmark_group("random_access");
    for &(n, _m) in SIZES {
        let w = generate_workload(n, _m, SEED);
        let order = shuffled_ordinals(n, SEED);
        group.throughput(Throughput::Elements(n as u64));
        // Scoped contenders run the whole access inside one `scope()` (prep
        // untimed, lookups timed) so the checked accessor pays no bounds check.
        macro_rules! row_scoped {
            ($a:path, $name:literal) => {{
                use $a as ad;
                let g = ad::build(&w);
                group.bench_with_input(BenchmarkId::new($name, n), &g, |b, g| {
                    ad::bench_random_access(g, &order, b)
                });
            }};
        }
        // Precompute-then-access contenders: indices are built untimed, the
        // sum loop is timed.
        macro_rules! row {
            ($a:path, $name:literal) => {{
                use $a as ad;
                let g = ad::build(&w);
                let ixs = ad::access_indices(&g, &order);
                group.bench_with_input(BenchmarkId::new($name, n), &(g, ixs), |b, (g, ixs)| {
                    b.iter(|| black_box(ad::access_sum(g, ixs)))
                });
            }};
        }
        row_scoped!(adapters::sg_vec_scoped, "sg_vec_scoped");
        row!(adapters::sg_vec_stabilized, "sg_vec_stabilized");
        row!(adapters::sg_vec_checked, "sg_vec_checked");
        row_scoped!(adapters::sg_flat, "sg_flat");
        row_scoped!(adapters::sg_btree, "sg_btree");
        row!(adapters::pg, "pg");
        row!(adapters::pg_stable, "pg_stable");
    }
    group.finish();
}

fn bench_remove_edges(c: &mut Criterion) {
    sanity();
    let mut group = c.benchmark_group("remove_edges");
    group.sample_size(30);
    for &(n, m) in SIZES {
        let w = generate_workload(n, m, SEED);
        group.throughput(Throughput::Elements(m as u64));
        let bs = if n <= 1_000 {
            BatchSize::SmallInput
        } else {
            BatchSize::LargeInput
        };
        macro_rules! row {
            ($a:path, $name:literal) => {{
                use $a as ad;
                let g0 = ad::build(&w);
                group.bench_with_input(BenchmarkId::new($name, n), &g0, |b, g0| {
                    b.iter_batched(
                        || g0.clone(),
                        |mut g| {
                            ad::remove_edge_set(&mut g);
                            g
                        },
                        bs,
                    )
                });
            }};
        }
        row!(adapters::sg_vec_scoped, "sg_vec_scoped");
        row!(adapters::sg_vec_stabilized, "sg_vec_stabilized");
        row!(adapters::sg_vec_checked, "sg_vec_checked");
        row!(adapters::sg_flat, "sg_flat");
        row!(adapters::sg_btree, "sg_btree");
        row!(adapters::pg, "pg");
        row!(adapters::pg_stable, "pg_stable");

        // One-at-a-time removal — valid only on the stable backend.
        let g0 = adapters::pg_stable::build(&w);
        group.bench_with_input(BenchmarkId::new("pg_stable_loop", n), &g0, |b, g0| {
            b.iter_batched(
                || g0.clone(),
                |mut g| {
                    adapters::pg_stable::remove_edge_loop(&mut g);
                    g
                },
                bs,
            )
        });
    }
    group.finish();
}

fn bench_remove_nodes(c: &mut Criterion) {
    sanity();
    let mut group = c.benchmark_group("remove_nodes");
    group.sample_size(30);
    for &(n, m) in SIZES {
        let w = generate_workload(n, m, SEED);
        group.throughput(Throughput::Elements(n as u64));
        let bs = if n <= 1_000 {
            BatchSize::SmallInput
        } else {
            BatchSize::LargeInput
        };
        macro_rules! row {
            ($a:path, $name:literal) => {{
                use $a as ad;
                let g0 = ad::build(&w);
                group.bench_with_input(BenchmarkId::new($name, n), &g0, |b, g0| {
                    b.iter_batched(
                        || g0.clone(),
                        |mut g| {
                            ad::remove_node_set(&mut g);
                            g
                        },
                        bs,
                    )
                });
            }};
        }
        row!(adapters::sg_vec_scoped, "sg_vec_scoped");
        row!(adapters::sg_vec_stabilized, "sg_vec_stabilized");
        row!(adapters::sg_vec_checked, "sg_vec_checked");
        row!(adapters::sg_flat, "sg_flat");
        row!(adapters::sg_btree, "sg_btree");
        row!(adapters::pg, "pg");
        row!(adapters::pg_stable, "pg_stable");

        // One-at-a-time node removal is O(victims · deg); cap to the two
        // smaller sizes so it does not dominate wall time at 10k.
        if n <= 1_000 {
            let g0 = adapters::pg_stable::build(&w);
            group.bench_with_input(BenchmarkId::new("pg_stable_loop", n), &g0, |b, g0| {
                b.iter_batched(
                    || g0.clone(),
                    |mut g| {
                        adapters::pg_stable::remove_node_loop(&mut g);
                        g
                    },
                    bs,
                )
            });
        }
    }
    group.finish();
}

fn bench_memory(c: &mut Criterion<AllocBytes>) {
    sanity();
    let mut group = c.benchmark_group("memory");
    for &(n, m) in SIZES {
        let w = generate_workload(n, m, SEED);
        group.throughput(Throughput::Elements((n + m) as u64));
        macro_rules! row {
            ($a:path, $name:literal) => {{
                use $a as ad;
                group.bench_with_input(BenchmarkId::new($name, n), &w, |b, w| {
                    // Live-heap delta across one build = the graph's footprint.
                    // Each graph is dropped immediately so peak stays bounded.
                    b.iter_custom(|iters| {
                        let mut total = 0u64;
                        for _ in 0..iters {
                            let before = live_bytes();
                            let g = ad::build(w);
                            let after = live_bytes();
                            total += (after - before).max(0) as u64;
                            drop(black_box(g));
                        }
                        total
                    })
                });
            }};
        }
        row!(adapters::sg_vec_scoped, "sg_vec_scoped");
        row!(adapters::sg_vec_stabilized, "sg_vec_stabilized");
        row!(adapters::sg_vec_checked, "sg_vec_checked");
        row!(adapters::sg_flat, "sg_flat");
        row!(adapters::sg_btree, "sg_btree");
        row!(adapters::pg, "pg");
        row!(adapters::pg_stable, "pg_stable");
    }
    group.finish();
}

criterion_group!(
    benches,
    bench_creation,
    bench_traversal,
    bench_random_access,
    bench_remove_edges,
    bench_remove_nodes
);
criterion_group! {
    name = memory;
    // Memory is deterministic (zero variance); disable plot generation, whose
    // KDE divides by the standard deviation and would NaN-panic.
    config = Criterion::default().with_measurement(AllocBytes).without_plots();
    targets = bench_memory
}
criterion_main!(benches, memory);