Skip to content

Commit

Permalink
Merge pull request #13 from andyquinterom/optimizacion
Browse files Browse the repository at this point in the history
feat: Improves performance with pre-allocation
  • Loading branch information
pierina-ixpantia authored Jun 18, 2024
2 parents aea4dba + c295ae8 commit d6731fe
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 90 deletions.
6 changes: 5 additions & 1 deletion benches/directed_graph.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use orbweaver::directed::{acyclic::DirectedAcyclicGraph, DirectedGraphBuilder};
use std::io::{prelude::*};

const MEDIUM_TXT_PATH: &str = "assets/medium.txt";

Expand All @@ -24,6 +23,7 @@ fn get_medium_graph() -> DirectedAcyclicGraph {
pub fn criterion_benchmark(c: &mut Criterion) {
let graph_dag = get_medium_graph();
let graph_dg = graph_dag.clone().into_inner();
let graph_all_nodes = graph_dg.nodes();

println!("Done building the graph!");

Expand Down Expand Up @@ -98,6 +98,10 @@ pub fn criterion_benchmark(c: &mut Criterion) {
})
});

c.bench_function("dg_least_common_parents", |b| {
b.iter(|| graph_dg.least_common_parents(black_box(&graph_all_nodes)))
});

c.bench_function("dag_find_path", |b| {
b.iter(|| {
graph_dag.find_path(
Expand Down
30 changes: 13 additions & 17 deletions src/directed/acyclic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl DirectedAcyclicGraph {
return Ok(vec![]); // No path from start to goal in a DAG if start comes after goal in topo order
}

let mut path = Vec::new();
let path = unsafe { self.dg.u32x1_vec_0() };
let mut current = to;
path.push(current);

Expand All @@ -79,7 +79,7 @@ impl DirectedAcyclicGraph {
current = node_id;
if current == from {
path.reverse();
return Ok(self.resolve_mul(path));
return Ok(self.resolve_mul(path.drain(..)));
}
}
}
Expand All @@ -93,20 +93,22 @@ impl DirectedAcyclicGraph {
to: impl AsRef<str>,
) -> GraphInteractionResult<Vec<Vec<&str>>> {
// Helper function to perform DFS
#[inline]
fn dfs(
graph: &DirectedAcyclicGraph,
current: u32,
goal_id: u32,
current_path: &mut Vec<u32>,
all_paths: &mut Vec<Vec<u32>>,
all_paths: &mut Vec<u32>,
children_buffer: &mut Vec<u32>,
) {
// Add current node to path
current_path.push(current);

// Check if the current node is the goal
if current == goal_id {
all_paths.push(current_path.clone());
all_paths.extend_from_slice(current_path);
all_paths.push(0);
} else {
let children_start_index_local = children_buffer.len();
// Continue to next nodes that can be visited from the current node
Expand Down Expand Up @@ -135,23 +137,17 @@ impl DirectedAcyclicGraph {
let from = self.get_internal(from)?;
let to = self.get_internal(to)?;

let mut all_paths = Vec::new();
let mut current_path = Vec::new();
let mut children = Vec::new();
let current_path = unsafe { self.dg.u32x1_vec_0() };
let children = unsafe { self.dg.u32x1_vec_1() };
let all_paths = unsafe { self.dg.u32x1_vec_2() };

// Start DFS from the start node
dfs(
self,
from,
to,
&mut current_path,
&mut all_paths,
&mut children,
);
dfs(self, from, to, current_path, all_paths, children);

Ok(all_paths
.into_iter()
.map(|path| self.resolve_mul(path))
.split(|&n| n == 0)
.filter(|path| !path.is_empty())
.map(|path| self.resolve_mul(path.iter().copied()))
.collect())
}

Expand Down
Loading

0 comments on commit d6731fe

Please sign in to comment.