From 72b4cf8e715567ba0ec8006f79a57ef7cccaabd9 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Mon, 16 Oct 2023 15:50:08 -0400 Subject: [PATCH 1/2] Add the first use of the rust benchmark tests. --- src/lib.rs | 3 +++ src/parser/keyword.rs | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index bb3a52cb..c4d2e832 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,8 +2,11 @@ #![feature(trait_alias)] #![feature(path_file_prefix)] #![feature(is_sorted)] +#![feature(test)] // TODO: #![warn(missing_docs)] +extern crate test; + #[cfg(feature = "compare")] pub mod compare; diff --git a/src/parser/keyword.rs b/src/parser/keyword.rs index c78f8997..2ff75663 100644 --- a/src/parser/keyword.rs +++ b/src/parser/keyword.rs @@ -250,3 +250,25 @@ fn export_keyword<'s>(input: OrgSource<'s>) -> Res, OrgSource<'s>> take_while1(|c: char| c.is_alphanumeric() || "-_".contains(c)), )))(input) } + +#[cfg(test)] +mod tests { + use test::Bencher; + + use super::*; + use crate::context::Context; + use crate::context::ContextElement; + use crate::context::GlobalSettings; + use crate::context::List; + use crate::parser::OrgSource; + + #[bench] + fn bench_affiliated_keyword(b: &mut Bencher) { + let input = OrgSource::new("#+CAPTION[*foo*]: bar *baz*"); + let global_settings = GlobalSettings::default(); + let initial_context = ContextElement::document_context(); + let initial_context = Context::new(&global_settings, List::new(&initial_context)); + + b.iter(|| assert!(affiliated_keyword(&initial_context, input).is_ok())); + } +} From f10efec21d6f05196457ea76d9de64a835aad437 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Mon, 16 Oct 2023 15:57:18 -0400 Subject: [PATCH 2/2] No performance change switching affiliated_key to using element macro. --- src/parser/keyword.rs | 12 +++++++----- src/parser/macros.rs | 5 +++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/parser/keyword.rs b/src/parser/keyword.rs index 2ff75663..a7ac6aed 100644 --- a/src/parser/keyword.rs +++ b/src/parser/keyword.rs @@ -27,6 +27,7 @@ use crate::context::RefContext; use crate::error::CustomError; use crate::error::MyError; use crate::error::Res; +use crate::parser::macros::element; use crate::parser::util::start_of_line; use crate::types::AffiliatedKeywords; use crate::types::Keyword; @@ -151,11 +152,12 @@ fn affiliated_key<'b, 'g, 'r, 's>( context: RefContext<'b, 'g, 'r, 's>, input: OrgSource<'s>, ) -> Res, OrgSource<'s>> { - alt(( - parser_with_context!(dual_affiliated_key)(context), - parser_with_context!(plain_affiliated_key)(context), - export_keyword, - ))(input) + element!(dual_affiliated_key, context, input); + element!(plain_affiliated_key, context, input); + element!(export_keyword, input); + Err(nom::Err::Error(CustomError::MyError(MyError( + "No affiliated key.", + )))) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] diff --git a/src/parser/macros.rs b/src/parser/macros.rs index ea81c4e1..f33cf044 100644 --- a/src/parser/macros.rs +++ b/src/parser/macros.rs @@ -35,6 +35,11 @@ macro_rules! element { return Ok((remaining, ele)); } }; + ($parser:expr, $input: expr) => { + if let Ok((remaining, ele)) = $parser($input) { + return Ok((remaining, ele)); + } + }; } pub(crate) use element;