Skip to content

Commit

Permalink
fix(cmn): 🧵 fix cargo bench
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienrousseau committed May 11, 2024
1 parent 9cf7544 commit 8102cfb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
10 changes: 5 additions & 5 deletions benches/criterion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,28 @@ pub use cmn::Common;
pub use cmn::Constants;
pub use cmn::Words;

#[allow(unused_results)]
fn bench_cmn(c: &mut Criterion) {
c.bench_function("common", |b| {
b.iter(|| {
let common = black_box(Common::default());
black_box(common.constants());
black_box(common.words());
});
})
});
}

#[allow(unused_results)]
fn bench_words(c: &mut Criterion) {
c.bench_function("Words::new", |b| {
b.iter(|| {
let words = black_box(Words::new());
let _ = black_box(words.words_list());
let _words = black_box(Words::new());
})
});

c.bench_function("Words::default", |b| {
b.iter(|| {
let words = black_box(Words::default());
let _ = black_box(words.words_list());
let _words = black_box(Words::default());
})
});
}
Expand Down
15 changes: 9 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,15 @@ impl Common {
let words_data = self
.fields
.get("words")
.expect("Words data not found in JSON")
.as_array()
.expect("Words data is not an array")
.iter()
.map(|word_value| word_value.as_str().unwrap().to_string())
.collect::<Vec<String>>(); // Add type annotation here
.map(|words_array| {
words_array
.as_array()
.expect("Words data is not an array")
.iter()
.map(|word_value| word_value.as_str().unwrap().to_string())
.collect::<Vec<String>>()
})
.unwrap_or_default(); // Add this line

Words {
words: HashSet::from_iter(words_data),
Expand Down
14 changes: 10 additions & 4 deletions src/words.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,17 @@ impl Default for Words {
/// This method is automatically called when creating a new instance of `Words`
/// using `Words::default()`.
fn default() -> Self {
let mut words = Words::new();
for word in WORD_LIST.iter() {
words.add_word(word);
let mut sorted_words: Vec<String> = WORD_LIST
.iter()
.map(|&word| word.to_owned())
.collect();

sorted_words.sort_unstable();
sorted_words.dedup();

Words {
words: sorted_words.into_iter().collect(),
}
words
}
}

Expand Down

0 comments on commit 8102cfb

Please sign in to comment.