Skip to content

Commit

Permalink
add different string crates under features (default to std::string::S…
Browse files Browse the repository at this point in the history
…tring)
  • Loading branch information
brentp committed May 5, 2023
1 parent 5f0390c commit 2f1835a
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 8 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ edition = "2021"

[dependencies]
rand = "0.8.5"
smartstring = "1.0.1"
smartstring = {version = "1.0.1", optional = true }
smol_str = {version = "0.2.0", optional = true }
compact_str = {version = "0.7.0", optional = true }
kstring = {version = "2.0.0", optional = true }

[dev-dependencies]
criterion = { version = "0.4", features = ["html_reports"] }
Expand Down
12 changes: 7 additions & 5 deletions benches/random_intervals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion};
use rand::Rng;
use resort::intersection::{Intersection, IntersectionIterator};
use resort::position::{Positioned, PositionedIterator};
use smartstring::alias::String;
use resort::string::String;
use std::collections::HashMap;
use std::io;

Expand Down Expand Up @@ -31,6 +31,7 @@ struct Intervals {
curr_max: f64,
rng: rand::rngs::ThreadRng,
interval_len: u64,
saved_chrom: String,
}

impl Intervals {
Expand All @@ -42,6 +43,7 @@ impl Intervals {
curr_max: 1.0,
rng: rand::thread_rng(),
interval_len: interval_len,
saved_chrom: String::from("chr1"),
}
}
}
Expand All @@ -60,7 +62,7 @@ impl PositionedIterator for Intervals {
self.curr_max *= r.powf(self.i as f64);
let start = ((1.0 - self.curr_max) * (MAX_POSITION as f64)) as u64;
Some(Ok(Interval {
chrom: String::from("chr1"),
chrom: self.saved_chrom.clone(),
start: start,
stop: start + self.interval_len,
}))
Expand All @@ -70,15 +72,15 @@ impl PositionedIterator for Intervals {
}
}

const MAX_POSITION: u64 = 100_000;
const MAX_POSITION: u64 = 10_000;

pub fn intersection_benchmark(c: &mut Criterion) {
let chrom_order = HashMap::from([(String::from("chr1"), 0), (String::from("chr2"), 1)]);

c.bench_function("simple intersection", |b| {
b.iter(|| {
let a_ivs = Intervals::new(String::from("a"), 1000, 1000);
let b_ivs = Intervals::new(String::from("b"), 10_000, 100);
let a_ivs = Intervals::new(String::from("a"), 100, 1000);
let b_ivs = Intervals::new(String::from("b"), 100_000, 100);
let iter = IntersectionIterator::new(a_ivs, vec![b_ivs], &chrom_order)
.expect("error getting iterator");

Expand Down
2 changes: 1 addition & 1 deletion src/intersection.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use smartstring::alias::String;
use crate::string::String;
use std::cmp::Ordering;
use std::collections::{vec_deque::VecDeque, BinaryHeap, HashMap};
use std::io;
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod intersection;
pub mod position;
pub mod string;
2 changes: 1 addition & 1 deletion src/position.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use smartstring::alias::String;
use crate::string::String;
use std::io;
pub enum Value {
Int(i64),
Expand Down
19 changes: 19 additions & 0 deletions src/string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#[cfg(feature = "smol_str")]
pub use smol_str::SmolStr as String;

#[cfg(feature = "smartstring")]
pub use smartstring::alias::String;

#[cfg(feature = "compact_str")]
pub use compact_str::CompactString as String;

#[cfg(feature = "kstring")]
pub use kstring::KString as String;

#[cfg(all(
not(feature = "smartstring"),
not(feature = "smol_str"),
not(feature = "compact_str"),
not(feature = "kstring"),
))]
pub use std::string::String;

0 comments on commit 2f1835a

Please sign in to comment.