Skip to content

Commit

Permalink
Rust - Use SmallVec for words instead of Vec.
Browse files Browse the repository at this point in the history
  • Loading branch information
renatoathaydes committed Aug 14, 2021
1 parent e6185a9 commit 4d8fe42
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/rust/phone_encoder/Cargo.toml
Expand Up @@ -10,6 +10,7 @@ license = ""
[dependencies]
#num-bigint = "0.4"
num-bigint = { git = "https://github.com/rust-num/num-bigint.git", branch = "master" }
smallvec = "1.6.1"

[dev-dependencies]
criterion = "0.3.5"
Expand Down
30 changes: 27 additions & 3 deletions src/rust/phone_encoder/benches/bench1.rs
@@ -1,9 +1,10 @@
use core::iter;
use std::io::{self, BufWriter};

use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main, Throughput};
use criterion::{BenchmarkId, black_box, Criterion, criterion_group, criterion_main, Throughput};
use smallvec::SmallVec;

use phone_encoder::print_solution;
use std::io::{self, BufWriter};

static NUM_SRC: [&str; 7] = ["123456", "78901234", "567890", "123456789", "012345", "43210", "98765432"];

Expand Down Expand Up @@ -38,5 +39,28 @@ fn bench_print_solution(c: &mut Criterion) {
group.finish();
}

criterion_group!(benches, bench_print_solution);
fn bench_vec_mut(c: &mut Criterion) {
let mut group = c.benchmark_group("vec_push_pop");
for words in [1, 10, 20, 30, 40, 50].iter()
.map(|size| generate_data(*size as usize, &WORD_SRC)) {
let size = words.len() as u64;
group.throughput(Throughput::Elements(size));
group.bench_with_input(BenchmarkId::from_parameter(size), &words, |b, words| {
b.iter(|| {
let mut vec = Vec::with_capacity(50);
for word in words {
vec.push(black_box(word));
}
for _ in 0..words.len() {
black_box(&vec);
vec.pop();
}
vec
});
});
}
group.finish();
}

criterion_group!(benches, bench_vec_mut);
criterion_main!(benches);
5 changes: 3 additions & 2 deletions src/rust/phone_encoder/src/lib.rs
Expand Up @@ -4,6 +4,7 @@ use std::io::{self, BufRead, Write};
use std::path::Path;

use num_bigint::BigUint;
use smallvec::SmallVec;

pub type Dictionary = HashMap<BigUint, Vec<String>>;

Expand All @@ -13,7 +14,7 @@ pub fn print_translations<'dict>(
num: &str,
digits: &[char],
start: usize,
words: &mut Vec<&'dict str>,
words: &mut SmallVec<[&'dict str; 32]>,
dict: &'dict Dictionary,
writer: &mut impl Write,
) -> io::Result<()> {
Expand Down Expand Up @@ -46,7 +47,7 @@ pub fn print_translations<'dict>(
}
}

pub fn print_solution(num: &str, words: &[&str], writer: &mut impl Write) {
pub fn print_solution(num: &str, words: &SmallVec<[&str; 32]>, writer: &mut impl Write) {
writeln!(writer, "{}: {}", num, words.join(" ")).expect("Cannot print solution");
}

Expand Down
3 changes: 2 additions & 1 deletion src/rust/phone_encoder/src/main.rs
@@ -1,5 +1,6 @@
use std::env::args;
use std::io::{self, BufWriter};
use smallvec::SmallVec;

mod lib;

Expand All @@ -22,7 +23,7 @@ fn main() -> io::Result<()> {
let digits: Vec<_> = num.chars()
.filter(|ch| ch.is_alphanumeric())
.collect();
let mut words = Vec::new();
let mut words: SmallVec<[&str; 32]> = SmallVec::with_capacity(50);
lib::print_translations(&num, &digits, 0, &mut words, &dict, &mut out)?;
}
Ok(())
Expand Down

0 comments on commit 4d8fe42

Please sign in to comment.