avoid allocations for results

This commit is contained in:
foosinn 2021-11-30 03:21:27 +01:00
parent a0497b4452
commit 3c3d20b3d9

View file

@ -11,7 +11,7 @@ struct App {
impl App { impl App {
/// check if word consists only of letters in query /// check if word consists only of letters in query
fn check_word(&self, word: &str) -> Option<String> { fn check_word<'a>(&self, word: &'a str) -> Option<&'a str> {
let mut query = self.query.clone(); let mut query = self.query.clone();
word.chars() word.chars()
.try_for_each(|c| { .try_for_each(|c| {
@ -19,17 +19,19 @@ impl App {
query.remove(position); query.remove(position);
Some(()) Some(())
}) })
.and(Some(word.to_string())) .and(Some(word))
} }
} }
fn main() { fn main() {
let app: App = argh::from_env(); let app: App = argh::from_env();
let start = std::time::Instant::now();
let mut results = WORDS let mut results = WORDS
.lines() .lines()
.filter_map(|line| app.check_word(line)) .filter_map(|line| app.check_word(line))
.collect::<Vec<String>>(); .collect::<Vec<&str>>();
eprintln!("search took: {:?}", start.elapsed());
results.sort_by(|a, b| { results.sort_by(|a, b| {
a.len() a.len()