mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 22:12:21 -04:00
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:
parent
4c177280af
commit
f19126a2f1
2 changed files with 30 additions and 30 deletions
|
@ -82,6 +82,33 @@ struct OtherTag<'a> {
|
||||||
options: HashMap<&'a str, &'a str>,
|
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")]
|
#[cfg(feature = "bench")]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn anki_tag_benchmark() {
|
pub fn anki_tag_benchmark() {
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
// Copyright: Ankitects Pty Ltd and contributors
|
// Copyright: Ankitects Pty Ltd and contributors
|
||||||
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||||
|
|
||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
use nom::{
|
use nom::{
|
||||||
branch::alt,
|
branch::alt,
|
||||||
bytes::complete::{is_not, tag},
|
bytes::complete::{is_not, tag},
|
||||||
|
@ -31,34 +29,7 @@ impl<'a> CardNodes<'a> {
|
||||||
impl<'a> Tag<'a> {
|
impl<'a> Tag<'a> {
|
||||||
fn new(name: &'a str, options: Vec<(&'a str, &'a str)>, content: &'a str) -> Self {
|
fn new(name: &'a str, options: Vec<(&'a str, &'a str)>, content: &'a str) -> Self {
|
||||||
match name {
|
match name {
|
||||||
"tts" => {
|
"tts" => Self::Tts(TtsTag::new(content, options)),
|
||||||
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,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
_ => Self::Other(OtherTag {
|
_ => Self::Other(OtherTag {
|
||||||
name,
|
name,
|
||||||
content,
|
content,
|
||||||
|
@ -162,6 +133,8 @@ fn text_node(s: &str) -> IResult<Node> {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
macro_rules! assert_parsed_nodes {
|
macro_rules! assert_parsed_nodes {
|
||||||
|
|
Loading…
Reference in a new issue