refactor: pass Unitig objects directly instead of raw byte slices

Refactored `try_for_each_unitig` and related pipelines across `obidebruinj` and `obikpartitionner` to accept `Unitig` instances directly. This eliminates manual `Unitig::from_nucleotides()` conversions, simplifies the data flow, and reduces unnecessary allocation overhead.
This commit is contained in:
Eric Coissac
2026-06-13 10:46:35 +02:00
parent 1f336fe496
commit fb8c6e427c
5 changed files with 14 additions and 16 deletions
+6 -8
View File
@@ -1,7 +1,7 @@
//use ahash::RandomState; //use ahash::RandomState;
use hashbrown::HashMap; use hashbrown::HashMap;
use obikseq::k; use obikseq::k;
use obikseq::{CanonicalKmer, Sequence}; use obikseq::{CanonicalKmer, Sequence, Unitig};
use rayon::iter::{IntoParallelRefIterator, ParallelIterator}; use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use std::cell::RefCell; use std::cell::RefCell;
use std::fmt; use std::fmt;
@@ -452,17 +452,17 @@ impl GraphDeBruijn {
pub fn try_for_each_unitig<E, F>(&self, f: F) -> Result<(), E> pub fn try_for_each_unitig<E, F>(&self, f: F) -> Result<(), E>
where where
E: Send, E: Send,
F: FnMut(&[u8]) -> Result<(), E> + Send, F: FnMut(&Unitig) -> Result<(), E> + Send,
{ {
thread_local! { thread_local! {
static BUF: RefCell<Vec<u8>> = RefCell::new(Vec::with_capacity(4096)); static BUF: RefCell<Vec<u8>> = RefCell::new(Vec::with_capacity(4096));
} }
let (tx, rx) = crossbeam_channel::bounded::<Vec<u8>>(rayon::current_num_threads() * 256); let (tx, rx) = crossbeam_channel::bounded::<Unitig>(rayon::current_num_threads() * 256);
std::thread::scope(|s| { std::thread::scope(|s| {
let writer = s.spawn(move || -> Result<(), E> { let writer = s.spawn(move || -> Result<(), E> {
let mut f = f; let mut f = f;
for nucs in rx { for unitig in rx {
f(&nucs)?; f(&unitig)?;
} }
Ok(()) Ok(())
}); });
@@ -471,9 +471,7 @@ impl GraphDeBruijn {
let mut buf = buf.borrow_mut(); let mut buf = buf.borrow_mut();
buf.clear(); buf.clear();
buf.extend(iter); buf.extend(iter);
let to_send = buf.clone(); tx.send(Unitig::from_nucleotides(&buf)).ok();
buf.clear();
tx.send(to_send).ok();
}); });
}); });
drop(tx); drop(tx);
+2 -2
View File
@@ -24,8 +24,8 @@ fn canonical_kmers(seq: &[u8]) -> Vec<CanonicalKmer> {
fn collect_unitigs(g: &GraphDeBruijn) -> Vec<Unitig> { fn collect_unitigs(g: &GraphDeBruijn) -> Vec<Unitig> {
let mut unitigs = Vec::new(); let mut unitigs = Vec::new();
g.try_for_each_unitig(|nucs| -> Result<(), std::convert::Infallible> { g.try_for_each_unitig(|unitig| -> Result<(), std::convert::Infallible> {
unitigs.push(Unitig::from_nucleotides(nucs)); unitigs.push(unitig.clone());
Ok(()) Ok(())
}) })
.unwrap(); .unwrap();
+2 -2
View File
@@ -107,8 +107,8 @@ impl KmerPartition {
fs::create_dir_all(&layer_dir)?; fs::create_dir_all(&layer_dir)?;
let mut uw = Layer::<()>::unitig_writer(&layer_dir).map_err(olm_to_sk)?; let mut uw = Layer::<()>::unitig_writer(&layer_dir).map_err(olm_to_sk)?;
g.try_for_each_unitig(|nucs| { g.try_for_each_unitig(|unitig| {
uw.write(&obikseq::unitig::Unitig::from_nucleotides(nucs)) uw.write(unitig)
})?; })?;
uw.close()?; uw.close()?;
+2 -2
View File
@@ -307,8 +307,8 @@ impl KmerPartition {
g.compute_degrees_and_mark_starts(); g.compute_degrees_and_mark_starts();
fs::create_dir_all(&new_layer_dir)?; fs::create_dir_all(&new_layer_dir)?;
let mut uw = Layer::<()>::unitig_writer(&new_layer_dir).map_err(olm_to_sk)?; let mut uw = Layer::<()>::unitig_writer(&new_layer_dir).map_err(olm_to_sk)?;
g.try_for_each_unitig(|nucs| { g.try_for_each_unitig(|unitig| {
uw.write(&obikseq::unitig::Unitig::from_nucleotides(nucs)) uw.write(unitig)
})?; })?;
uw.close()?; uw.close()?;
let n = g.len(); let n = g.len();
+2 -2
View File
@@ -168,8 +168,8 @@ impl KmerPartition {
fs::create_dir_all(&dst_layer_dir)?; fs::create_dir_all(&dst_layer_dir)?;
let mut uw = Layer::<()>::unitig_writer(&dst_layer_dir).map_err(olm_to_sk)?; let mut uw = Layer::<()>::unitig_writer(&dst_layer_dir).map_err(olm_to_sk)?;
g.try_for_each_unitig(|nucs| { g.try_for_each_unitig(|unitig| {
uw.write(&obikseq::unitig::Unitig::from_nucleotides(nucs)) uw.write(unitig)
})?; })?;
uw.close()?; uw.close()?;
drop(g); drop(g);