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
//! One module per contender. Most of the surface is uniform:
//!
//! ```text
//! build(&Workload) -> G
//! traverse_sum(&G) -> usize          // Σ node payloads + Σ edge payloads (wrapping)
//! out_degree_sum(&G) -> usize        // Σ outgoing degree (sanity coverage)
//! remove_edge_set(&mut G)
//! remove_node_set(&mut G)            // cascades incident edges
//! counts(&G) -> (usize, usize)
//! ```
//!
//! Random access differs by family, because safegraph's scoped indices cannot
//! escape a `scope()`:
//!
//! - Scoped contenders (`sg_vec_scoped` / `sg_flat` / `sg_btree`) expose
//!   `bench_random_access(&G, &[usize], &mut Bencher)`, which runs the untimed
//!   index prep AND the timed lookups inside one `scope()` so the checked
//!   `ctx.node()` accessor benefits from the `Context`'s always-true
//!   `contains_node_index` (no bounds-check branch).
//! - The others (`sg_vec_stabilized` / `sg_vec_checked` / `pg` / `pg_stable`)
//!   expose the precompute pair `access_indices(&G, &[usize]) -> Vec<NodeIx>`
//!   (untimed) and `access_sum(&G, &[NodeIx]) -> usize` (timed).
//!
//! The three VecGraph rows compare safe-access strategies: `sg_vec_scoped`
//! (scope, free `contains`), `sg_vec_stabilized` ([`Graph::stabilize`],
//! versioned `contains`), and `sg_vec_checked` (graph-level `node()`, a
//! bounds-check `contains`).
//!
//! The three scoped safegraph contenders are stamped out by the
//! [`scoped_adapter!`] macro with concrete types — not a generic `fn<G>` —
//! to sidestep the heavy `G::Endpoints: for<'scope> Map<…>` HRTB bounds.

use std::collections::BTreeMap;

use criterion::Bencher;
use safegraph::graph::capability::InsertNode;
use safegraph::graph::{Graph, GraphOperation, GraphProperty};
use safegraph::raw_graph::flat_adj_edge::{FlatAdjEdgeGraph, NodeRepr as FlatNodeRepr, TNone};
use safegraph::raw_graph::linked_adj_edge::{EdgeRepr, LinkedAdjEdgeGraph, NodeRepr as LinkedNodeRepr};

use crate::common::{edge_is_victim, node_is_victim, Workload};

/// Stamps out a contender module exercising the safe scoped (`scope`/
/// `scope_mut`) API for the concrete graph type `$G`.
macro_rules! scoped_adapter {
    ($name:ident, $G:ty) => {
        pub mod $name {
            use super::*;

            pub type G = $G;

            pub fn build(w: &Workload) -> G {
                let mut g = G::default();
                g.scope_mut(|mut ctx| {
                    let ixs: Vec<_> = (0..w.n)
                        .map(|i| ctx.insert_node(i).expect("insert_node"))
                        .collect();
                    for (j, &(f, t)) in w.edges.iter().enumerate() {
                        ctx.insert_edge(j, [ixs[f as usize], ixs[t as usize]])
                            .expect("insert_edge");
                    }
                });
                g
            }

            pub fn traverse_sum(g: &G) -> usize {
                g.scope(|ctx| {
                    let mut total = 0usize;
                    for nix in Graph::node_indices(ctx) {
                        total = total.wrapping_add(*ctx.node(nix));
                        for (_, e, _) in ctx.walks_from(nix).map(|w| w.get()) {
                            total = total.wrapping_add(*e);
                        }
                    }
                    total
                })
            }

            pub fn out_degree_sum(g: &G) -> usize {
                g.scope(|ctx| {
                    let mut total = 0usize;
                    for nix in Graph::node_indices(ctx) {
                        total += ctx.walks_from(nix).count();
                    }
                    total
                })
            }

            /// Random-access lookups through the scoped API. Scoped indices
            /// cannot escape a `scope()`, so the untimed index prep and the
            /// timed `b.iter` loop both run inside one scope. Inside it,
            /// `Context::contains_node_index` is unconditionally `true`, so the
            /// checked `ctx.node()` accessor's assert folds away — none of the
            /// bounds-check cost the graph-level `g.node(ix)` would pay.
            pub fn bench_random_access(g: &G, order: &[usize], b: &mut Bencher) {
                g.scope(|ctx| {
                    // Untimed setup: scoped indices in the requested order.
                    let all: Vec<_> = Graph::node_indices(ctx).collect();
                    let ixs: Vec<_> = order.iter().map(|&i| all[i]).collect();
                    b.iter(|| {
                        let mut total = 0usize;
                        for &ix in &ixs {
                            total = total.wrapping_add(*ctx.node(ix));
                        }
                        std::hint::black_box(total)
                    });
                });
            }

            pub fn remove_edge_set(g: &mut G) {
                g.scope_mut(|ctx| {
                    let victims: Vec<_> = Graph::edge_indices(&*ctx)
                        .filter(|&e| edge_is_victim(*ctx.edge(e)))
                        .collect();
                    ctx.remove_nodes_edges(None, victims);
                });
            }

            pub fn remove_node_set(g: &mut G) {
                g.scope_mut(|ctx| {
                    let victims: Vec<_> = Graph::node_indices(&*ctx)
                        .filter(|&n| node_is_victim(*ctx.node(n)))
                        .collect();
                    ctx.remove_nodes_edges(victims, None);
                });
            }

            pub fn counts(g: &G) -> (usize, usize) {
                g.scope(|ctx| (ctx.nodes().count(), ctx.edges().count()))
            }
        }
    };
}

// Vec-backed, safe scoped API (scope()/scope_mut()). NodeIx = u32.
scoped_adapter!(sg_vec_scoped, safegraph::VecGraph<usize, usize>);

// Flat nested-collection adjacency, no reverse index (IS = TNone).
// EdgeIx = EdgeIx<u32, u32>.
scoped_adapter!(
    sg_flat,
    FlatAdjEdgeGraph<Vec<(usize, FlatNodeRepr<Vec<(usize, u32)>, TNone>)>>
);

// BTreeMap-backed, payload-keyed stable indices. NodeIx = usize (the key).
scoped_adapter!(
    sg_btree,
    LinkedAdjEdgeGraph<
        BTreeMap<usize, LinkedNodeRepr<Option<usize>>>,
        BTreeMap<usize, EdgeRepr<usize, Option<usize>>>,
    >
);

/// Vec-backed safegraph wrapped by [`Graph::stabilize`] — tombstone-versioned
/// stable indices give a fully safe, scope-free API (no `unsafe`, no
/// `scope()`). `contains_*_index` does a version/liveness check.
pub mod sg_vec_stabilized {
    use super::*;
    use safegraph::graph::stabilized::{EdgeIx as StabEdgeIx, NodeIx as StabNodeIx, Stabilized};

    pub type G = Stabilized<
        LinkedAdjEdgeGraph<
            Vec<(StabNodeIx<usize>, LinkedNodeRepr<u32>)>,
            Vec<(StabEdgeIx<usize>, EdgeRepr<u32, u32>)>,
        >,
        usize,
        usize,
    >;
    pub type NodeIx = <G as GraphProperty>::NodeIx;

    pub fn build(w: &Workload) -> G {
        let mut g: G = safegraph::VecGraph::<usize, usize>::default().stabilize();
        let ixs: Vec<NodeIx> = (0..w.n)
            .map(|i| g.insert_node(i).expect("insert_node"))
            .collect();
        for (j, &(f, t)) in w.edges.iter().enumerate() {
            g.insert_edge(j, [ixs[f as usize], ixs[t as usize]])
                .expect("insert_edge");
        }
        g
    }

    pub fn traverse_sum(g: &G) -> usize {
        let mut total = 0usize;
        for nix in Graph::node_indices(g) {
            total = total.wrapping_add(*g.node(nix));
            for (_, e, _) in g.walks_from(nix).map(|w| w.get()) {
                total = total.wrapping_add(*e);
            }
        }
        total
    }

    pub fn out_degree_sum(g: &G) -> usize {
        Graph::node_indices(g).map(|nix| g.walks_from(nix).count()).sum()
    }

    pub fn access_indices(g: &G, order: &[usize]) -> Vec<NodeIx> {
        let all: Vec<NodeIx> = Graph::node_indices(g).collect();
        order.iter().map(|&i| all[i]).collect()
    }

    pub fn access_sum(g: &G, ixs: &[NodeIx]) -> usize {
        let mut total = 0usize;
        for &ix in ixs {
            total = total.wrapping_add(*g.node(ix));
        }
        total
    }

    pub fn remove_edge_set(g: &mut G) {
        let victims: Vec<_> = Graph::edge_indices(g)
            .filter(|&e| edge_is_victim(*g.edge(e)))
            .collect();
        g.remove_nodes_edges(None, victims);
    }

    pub fn remove_node_set(g: &mut G) {
        let victims: Vec<_> = Graph::node_indices(g)
            .filter(|&nix| node_is_victim(*g.node(nix)))
            .collect();
        g.remove_nodes_edges(victims, None);
    }

    pub fn counts(g: &G) -> (usize, usize) {
        (Graph::node_indices(g).count(), Graph::edge_indices(g).count())
    }
}

/// Plain Vec-backed safegraph that pays a real `contains_*_index` bounds check
/// in advance of *every* index-taking graph operation, with NO `scope()` — the
/// consistent "fully checked" VecGraph contender. Plain `VecGraph` is not
/// `StableNode`, so enumeration and edge walks have no safe form and use
/// `*_unstable`, but each index-taking op is still gated by a real check:
/// accesses via the checked `node`/`edge` accessors; edge insertion via an
/// explicit endpoint `contains_node_index` assert before
/// `insert_edge_unchecked`; and batch removal via `remove_nodes_edges`, which
/// asserts every index before the unchecked sweep. Unlike the scoped contender
/// (whose `contains` is unconditionally `true`, so the checks fold away), these
/// are genuine bounds checks.
pub mod sg_vec_checked {
    use super::*;

    pub type G = safegraph::VecGraph<usize, usize>;
    pub type NodeIx = <G as GraphProperty>::NodeIx;
    type EdgeIx = <G as GraphProperty>::EdgeIx;

    pub fn build(w: &Workload) -> G {
        let mut g = G::default();
        let ixs: Vec<NodeIx> = (0..w.n)
            .map(|i| unsafe { InsertNode::insert_node_unchecked(&mut g, i) }.expect("insert_node"))
            .collect();
        for (j, &(f, t)) in w.edges.iter().enumerate() {
            let endpoints = [ixs[f as usize], ixs[t as usize]];
            // Emulate the checked insert: assert both endpoints are valid (the
            // same per-edge bounds check `insert_edge_unchecked` runs) before the
            // unchecked insert. `VecGraph` is not `StableEdge`, so the safe
            // `insert_edge` is unavailable — this models its index check.
            assert!(endpoints
                .iter()
                .all(|&n| GraphOperation::contains_node_index(&g, n)));
            // SAFETY: endpoints validated immediately above.
            unsafe { Graph::insert_edge_unchecked(&mut g, j, endpoints) }.expect("insert_edge");
        }
        g
    }

    pub fn traverse_sum(g: &G) -> usize {
        let mut total = 0usize;
        // SAFETY: not mutated during iteration. Enumeration / walks have no
        // safe form for a non-`StableNode` graph; node access is the safe
        // checked `g.node`.
        for nix in GraphOperation::node_indices(g) {
            total = total.wrapping_add(*g.node(nix));
            for (_, e, _) in unsafe { GraphOperation::walks_from_unchecked(g, nix) }.map(|w| w.get()) {
                total = total.wrapping_add(*e);
            }
        }
        total
    }

    pub fn out_degree_sum(g: &G) -> usize {
        let mut total = 0usize;
        // SAFETY: not mutated during iteration.
        for nix in GraphOperation::node_indices(g) {
            total += unsafe { GraphOperation::walks_from_unchecked(g, nix) }.count();
        }
        total
    }

    pub fn access_indices(g: &G, order: &[usize]) -> Vec<NodeIx> {
        // SAFETY: setup only; graph unchanged before the timed access.
        let all: Vec<NodeIx> = GraphOperation::node_indices(g).collect();
        order.iter().map(|&i| all[i]).collect()
    }

    pub fn access_sum(g: &G, ixs: &[NodeIx]) -> usize {
        let mut total = 0usize;
        for &ix in ixs {
            // Safe checked accessor: asserts `contains_node_index` (bounds check).
            total = total.wrapping_add(*g.node(ix));
        }
        total
    }

    pub fn remove_edge_set(g: &mut G) {
        // SAFETY: not mutated while collecting victims (enumeration only).
        let victims: Vec<EdgeIx> = GraphOperation::edge_indices(&*g)
            .filter(|&e| edge_is_victim(*g.edge(e)))
            .collect();
        g.remove_nodes_edges(None, victims);
    }

    pub fn remove_node_set(g: &mut G) {
        // SAFETY: not mutated while collecting victims (enumeration only).
        let victims: Vec<NodeIx> = GraphOperation::node_indices(&*g)
            .filter(|&n| node_is_victim(*g.node(n)))
            .collect();
        g.remove_nodes_edges(victims, None);
    }

    pub fn counts(g: &G) -> (usize, usize) {
        (GraphOperation::len_node(g), GraphOperation::len_edge(g))
    }
}

/// petgraph `DiGraph` — indices invalidate on the last-removed slot.
pub mod pg {
    use petgraph::graph::{DiGraph, NodeIndex};

    use crate::common::{edge_is_victim, node_is_victim, Workload};

    pub type G = DiGraph<usize, usize>;
    pub type NodeIx = NodeIndex;

    pub fn build(w: &Workload) -> G {
        let mut g = DiGraph::new();
        let ixs: Vec<NodeIndex> = (0..w.n).map(|i| g.add_node(i)).collect();
        for (j, &(f, t)) in w.edges.iter().enumerate() {
            g.add_edge(ixs[f as usize], ixs[t as usize], j);
        }
        g
    }

    pub fn traverse_sum(g: &G) -> usize {
        let mut total = 0usize;
        for n in g.node_indices() {
            total = total.wrapping_add(*g.node_weight(n).unwrap());
            for er in g.edges(n) {
                total = total.wrapping_add(*er.weight());
            }
        }
        total
    }

    pub fn out_degree_sum(g: &G) -> usize {
        g.node_indices().map(|n| g.edges(n).count()).sum()
    }

    pub fn access_indices(g: &G, order: &[usize]) -> Vec<NodeIx> {
        let _ = g;
        order.iter().map(|&i| NodeIndex::new(i)).collect()
    }

    pub fn access_sum(g: &G, ixs: &[NodeIx]) -> usize {
        let mut total = 0usize;
        for &ix in ixs {
            total = total.wrapping_add(*g.node_weight(ix).unwrap());
        }
        total
    }

    pub fn remove_edge_set(g: &mut G) {
        g.retain_edges(|gg, e| !edge_is_victim(gg[e]));
    }

    pub fn remove_node_set(g: &mut G) {
        g.retain_nodes(|gg, n| !node_is_victim(gg[n]));
    }

    pub fn counts(g: &G) -> (usize, usize) {
        (g.node_count(), g.edge_count())
    }
}

/// petgraph `StableDiGraph` — stable indices with tombstones. Additionally
/// exposes one-at-a-time removal rows (valid only because its indices survive
/// individual removals).
pub mod pg_stable {
    use petgraph::stable_graph::{EdgeIndex, NodeIndex, StableDiGraph};

    use crate::common::{edge_is_victim, node_is_victim, Workload};

    pub type G = StableDiGraph<usize, usize>;
    pub type NodeIx = NodeIndex;

    pub fn build(w: &Workload) -> G {
        let mut g = StableDiGraph::new();
        let ixs: Vec<NodeIndex> = (0..w.n).map(|i| g.add_node(i)).collect();
        for (j, &(f, t)) in w.edges.iter().enumerate() {
            g.add_edge(ixs[f as usize], ixs[t as usize], j);
        }
        g
    }

    pub fn traverse_sum(g: &G) -> usize {
        let mut total = 0usize;
        for n in g.node_indices() {
            total = total.wrapping_add(*g.node_weight(n).unwrap());
            for er in g.edges(n) {
                total = total.wrapping_add(*er.weight());
            }
        }
        total
    }

    pub fn out_degree_sum(g: &G) -> usize {
        g.node_indices().map(|n| g.edges(n).count()).sum()
    }

    pub fn access_indices(g: &G, order: &[usize]) -> Vec<NodeIx> {
        let all: Vec<NodeIndex> = g.node_indices().collect();
        order.iter().map(|&i| all[i]).collect()
    }

    pub fn access_sum(g: &G, ixs: &[NodeIx]) -> usize {
        let mut total = 0usize;
        for &ix in ixs {
            total = total.wrapping_add(*g.node_weight(ix).unwrap());
        }
        total
    }

    pub fn remove_edge_set(g: &mut G) {
        g.retain_edges(|gg, e| !edge_is_victim(gg[e]));
    }

    pub fn remove_node_set(g: &mut G) {
        g.retain_nodes(|gg, n| !node_is_victim(gg[n]));
    }

    pub fn remove_edge_loop(g: &mut G) {
        let victims: Vec<EdgeIndex> =
            g.edge_indices().filter(|&e| edge_is_victim(g[e])).collect();
        for e in victims {
            g.remove_edge(e);
        }
    }

    pub fn remove_node_loop(g: &mut G) {
        let victims: Vec<NodeIndex> =
            g.node_indices().filter(|&n| node_is_victim(g[n])).collect();
        for n in victims {
            g.remove_node(n);
        }
    }

    pub fn counts(g: &G) -> (usize, usize) {
        (g.node_count(), g.edge_count())
    }
}