performance comparison: creating HashMap up front

the previous solution:

anki_tag_parse          time:   [1.8401 us 1.8437 us 1.8476 us]

this solution:

anki_tag_parse          time:   [2.2420 us 2.2447 us 2.2477 us]
                        change: [+21.477% +21.770% +22.066%] (p = 0.00 < 0.05)
                        Performance has regressed.
This commit is contained in:
Damien Elmes 2021-12-17 12:58:01 +10:00
parent 4c177280af
commit f19126a2f1
2 changed files with 30 additions and 30 deletions

View file

@ -82,6 +82,33 @@ struct OtherTag<'a> {
options: HashMap<&'a str, &'a str>,
}
impl TtsTag<'_> {
pub(crate) fn new<'a>(content: &'a str, options: Vec<(&'a str, &'a str)>) -> TtsTag<'a> {
let mut options: HashMap<&str, &str> = options.into_iter().collect();
let lang = options.remove("lang").unwrap_or_default();
let voices = options
.remove("voices")
.unwrap_or_default()
.split(',')
.collect();
let speed = options
.remove("speed")
.unwrap_or_default()
.parse()
.unwrap_or(1.0);
let blank = options.remove("cloze_blank");
TtsTag {
content,
lang,
voices,
speed,
blank,
options,
}
}
}
#[cfg(feature = "bench")]
#[inline]
pub fn anki_tag_benchmark() {

View file

@ -1,8 +1,6 @@
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
use std::collections::HashMap;
use nom::{
branch::alt,
bytes::complete::{is_not, tag},
@ -31,34 +29,7 @@ impl<'a> CardNodes<'a> {
impl<'a> Tag<'a> {
fn new(name: &'a str, options: Vec<(&'a str, &'a str)>, content: &'a str) -> Self {
match name {
"tts" => {
let mut lang = "";
let mut voices = vec![];
let mut speed = 1.0;
let mut blank = None;
let mut other_options = HashMap::new();
for option in options {
match option.0 {
"lang" => lang = option.1,
"voices" => voices = option.1.split(',').collect(),
"speed" => speed = option.1.parse().unwrap_or(1.0),
"cloze_blank" => blank = Some(option.1),
_ => {
other_options.insert(option.0, option.1);
}
}
}
Self::Tts(TtsTag {
content,
lang,
voices,
speed,
blank,
options: other_options,
})
}
"tts" => Self::Tts(TtsTag::new(content, options)),
_ => Self::Other(OtherTag {
name,
content,
@ -162,6 +133,8 @@ fn text_node(s: &str) -> IResult<Node> {
#[cfg(test)]
mod test {
use std::collections::HashMap;
use super::*;
macro_rules! assert_parsed_nodes {