• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

vigna / webgraph-rs / 19278972970

11 Nov 2025 09:25PM UTC coverage: 62.316% (+14.3%) from 48.052%
19278972970

push

github

zommiommy
BatchCodec print stats and encoding time

40 of 53 new or added lines in 4 files covered. (75.47%)

814 existing lines in 46 files now uncovered.

5252 of 8428 relevant lines covered (62.32%)

29580067.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

64.91
/webgraph/src/transform/simplify.rs
1
/*
2
 * SPDX-FileCopyrightText: 2023 Inria
3
 *
4
 * SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
5
 */
6

7
use crate::graphs::{
8
    arc_list_graph, no_selfloops_graph::NoSelfLoopsGraph, union_graph::UnionGraph,
9
};
10
use crate::labels::Left;
11
use crate::traits::{LenderIntoIter, SequentialGraph, SortedIterator, SortedLender, SplitLabeling};
12
use crate::utils::sort_pairs::{KMergeIters, SortPairs};
13
use crate::utils::{CodecIter, DefaultBatchCodec, MemoryUsage};
14
use anyhow::{Context, Result};
15
use dsi_progress_logger::prelude::*;
16
use itertools::Itertools;
17
use lender::*;
18
use rayon::ThreadPool;
19
use tempfile::Builder;
20

21
use super::transpose;
22

23
/// Returns a simplified (i.e., undirected and loopless) version of the provided
24
/// sorted (both on nodes and successors) graph as a [sequential
25
/// graph](crate::traits::SequentialGraph).
26
///
27
/// This method exploits the fact that the input graph is already sorted,
28
/// sorting half the number of arcs of
29
/// [`simplify`](crate::transform::simplify::simplify).
30
pub fn simplify_sorted<G: SequentialGraph>(
1✔
31
    graph: G,
32
    memory_usage: MemoryUsage,
33
) -> Result<
34
    NoSelfLoopsGraph<
35
        UnionGraph<
36
            G,
37
            Left<arc_list_graph::ArcListGraph<KMergeIters<CodecIter<DefaultBatchCodec>, ()>>>,
38
        >,
39
    >,
40
>
41
where
42
    for<'a> G::Lender<'a>: SortedLender,
43
    for<'a, 'b> LenderIntoIter<'a, G::Lender<'b>>: SortedIterator,
44
{
45
    let transpose = transpose(&graph, memory_usage).context("Could not transpose the graph")?;
5✔
46
    Ok(NoSelfLoopsGraph(UnionGraph(graph, transpose)))
1✔
47
}
48

49
/// Returns a simplified (i.e., undirected and loopless) version of the provided
50
/// graph as a [sequential graph](crate::traits::SequentialGraph).
51
///
52
/// Note that if the graph is sorted (both on nodes and successors), it is
53
/// recommended to use [`simplify_sorted`](crate::transform::simplify::simplify_sorted).
54
///
55
/// For the meaning of the additional parameter, see
56
/// [`SortPairs`](crate::prelude::sort_pairs::SortPairs).
UNCOV
57
pub fn simplify(
×
58
    graph: &impl SequentialGraph,
59
    memory_usage: MemoryUsage,
60
) -> Result<
61
    Left<
62
        arc_list_graph::ArcListGraph<
63
            impl Iterator<Item = ((usize, usize), ())> + Clone + Send + Sync + 'static,
64
        >,
65
    >,
66
> {
UNCOV
67
    let dir = Builder::new().prefix("simplify_").tempdir()?;
×
UNCOV
68
    let mut sorted = SortPairs::new(memory_usage, dir.path())?;
×
69

70
    let mut pl = ProgressLogger::default();
×
UNCOV
71
    pl.item_name("node")
×
72
        .expected_updates(Some(graph.num_nodes()));
×
73
    pl.start("Creating batches...");
×
74
    // create batches of sorted edges
75
    let mut iter = graph.iter();
×
UNCOV
76
    while let Some((src, succ)) = iter.next() {
×
77
        for dst in succ {
×
78
            if src != dst {
×
79
                sorted.push(src, dst)?;
×
80
                sorted.push(dst, src)?;
×
81
            }
82
        }
UNCOV
83
        pl.light_update();
×
84
    }
85
    // merge the batches
UNCOV
86
    let map: fn(((usize, usize), ())) -> (usize, usize) = |(pair, _)| pair;
×
UNCOV
87
    let filter: fn(&(usize, usize)) -> bool = |(src, dst)| src != dst;
×
88
    let iter = Itertools::dedup(sorted.iter()?.map(map).filter(filter));
×
89
    let sorted = arc_list_graph::ArcListGraph::new(graph.num_nodes(), iter);
×
90
    pl.done();
×
91

92
    Ok(sorted)
×
93
}
94

95
/// Returns a simplified (i.e., undirected and loopless) version of the provided
96
/// graph as a [sequential graph](crate::traits::SequentialGraph).
97
///
98
/// This method uses splitting to sort in parallel different parts of the graph.
99
///
100
/// For the meaning of the additional parameter, see
101
/// [`SortPairs`](crate::prelude::sort_pairs::SortPairs).
102
pub fn simplify_split<S>(
4✔
103
    graph: &S,
104
    memory_usage: MemoryUsage,
105
    threads: &ThreadPool,
106
) -> Result<
107
    Left<
108
        arc_list_graph::ArcListGraph<
109
            itertools::Dedup<KMergeIters<CodecIter<DefaultBatchCodec>, ()>>,
110
        >,
111
    >,
112
>
113
where
114
    S: SequentialGraph + SplitLabeling,
115
{
116
    let num_threads = threads.current_num_threads();
12✔
117
    let (tx, rx) = std::sync::mpsc::channel();
12✔
118

119
    let mut dirs = vec![];
8✔
120

121
    threads.in_place_scope(|scope| {
12✔
122
        let mut thread_id = 0;
8✔
123
        #[allow(clippy::explicit_counter_loop)] // enumerate requires some extra bounds here
124
        for iter in graph.split_iter(num_threads) {
28✔
125
            let tx = tx.clone();
48✔
126
            let dir = Builder::new()
48✔
127
                .prefix(&format!("simplify_split_{thread_id}_"))
32✔
128
                .tempdir()
16✔
129
                .expect("Could not create a temporary directory");
32✔
130
            let dir_path = dir.path().to_path_buf();
48✔
131
            dirs.push(dir);
48✔
132
            scope.spawn(move |_| {
48✔
133
                log::debug!("Spawned thread {thread_id}");
16✔
134
                let mut sorted = SortPairs::new(memory_usage, dir_path).unwrap();
80✔
135
                for_!( (src, succ) in iter {
2,604,472✔
136
                    for dst in succ {
27,031,444✔
137
                        if src != dst {
25,379,448✔
138
                            sorted.push(src, dst).unwrap();
75,089,040✔
139
                            sorted.push(dst, src).unwrap();
50,059,360✔
140
                        }
141
                    }
142
                });
143
                let result = sorted.iter().context("Could not read arcs").unwrap();
80✔
144
                tx.send(result).expect("Could not send the sorted pairs");
80✔
145
                log::debug!("Thread {thread_id} finished");
16✔
146
            });
147
            thread_id += 1;
16✔
148
        }
149
    });
150
    drop(tx);
8✔
151

152
    // get a graph on the sorted data
153
    log::debug!("Waiting for threads to finish");
4✔
154
    let edges: KMergeIters<CodecIter<DefaultBatchCodec>> = rx.iter().sum();
20✔
155
    let edges = edges.dedup();
12✔
156
    log::debug!("All threads finished");
4✔
157
    let sorted = arc_list_graph::ArcListGraph::new_labeled(graph.num_nodes(), edges);
20✔
158

159
    drop(dirs);
8✔
160
    Ok(Left(sorted))
4✔
161
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc