perf(dict): Compare table to trie

This commit is contained in:
Ed Page 2024-12-24 21:41:48 -06:00
parent fad1637b6c
commit 1b0830df9c
3 changed files with 138092 additions and 0 deletions

View file

@ -1,6 +1,7 @@
#![allow(clippy::wildcard_imports)]
mod map_codegen;
mod table_codegen;
mod trie_codegen;
mod miss {
@ -17,6 +18,11 @@ mod miss {
fn trie(word: unicase::UniCase<&str>) -> Option<&'static &[&str]> {
trie_codegen::WORD_TRIE.find(&word)
}
#[divan::bench(args = [unicase::UniCase::new(MISS)])]
fn table(word: unicase::UniCase<&str>) -> Option<&'static &[&str]> {
table_codegen::WORD.find(&word)
}
}
mod hit {
@ -33,6 +39,11 @@ mod hit {
fn trie(word: unicase::UniCase<&str>) -> Option<&'static &[&str]> {
trie_codegen::WORD_TRIE.find(&word)
}
#[divan::bench(args = [unicase::UniCase::new(HIT)])]
fn table(word: unicase::UniCase<&str>) -> Option<&'static &[&str]> {
table_codegen::WORD.find(&word)
}
}
fn main() {

File diff suppressed because it is too large Load diff

View file

@ -20,6 +20,15 @@ fn codegen() {
snapbox::file!["../benches/benches/map_codegen.rs"].raw()
);
let mut table_content = vec![];
generate_table(&mut table_content, "WORD", DICT);
let table_content = String::from_utf8(table_content).unwrap();
let table_content = codegenrs::rustfmt(&table_content, None).unwrap();
snapbox::assert_data_eq!(
&table_content,
snapbox::file!["../benches/benches/table_codegen.rs"].raw()
);
snapbox::assert_data_eq!(
&trie_content,
snapbox::file!["../src/word_codegen.rs"].raw()
@ -96,3 +105,38 @@ fn generate_map<W: std::io::Write>(file: &mut W, prefix: &str, dict: &[u8]) {
)
.unwrap();
}
fn generate_table<W: std::io::Write>(file: &mut W, prefix: &str, dict: &[u8]) {
writeln!(
file,
"// This file is @generated by {}",
file!().replace('\\', "/")
)
.unwrap();
writeln!(file, "#![allow(clippy::unreadable_literal)]",).unwrap();
writeln!(file, "#![allow(unreachable_pub)]",).unwrap();
writeln!(file).unwrap();
let records: Vec<_> = csv::ReaderBuilder::new()
.has_headers(false)
.flexible(true)
.from_reader(dict)
.records()
.map(|r| r.unwrap())
.collect();
dictgen::generate_table(
file,
prefix,
"&'static [&'static str]",
records.iter().map(|record| {
let mut record_fields = record.iter();
let key = record_fields.next().unwrap();
let value = format!(
"&[{}]",
itertools::join(record_fields.map(|field| format!(r#""{field}""#)), ", ")
);
(key, value)
}),
)
.unwrap();
}