Borrow the context instead of clone it for fewer reference count increments.

This commit is contained in:
Tom Alexander 2022-12-11 00:39:35 -05:00
parent 9dfca22b86
commit 0bcc3d9dc6
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 6 additions and 6 deletions

View File

@ -1,6 +1,6 @@
macro_rules! parser_with_context {
($target:ident) => {
move |context| move |i| $target(&context, i)
move |context| move |i| $target(context, i)
};
}
pub(crate) use parser_with_context;

View File

@ -107,7 +107,7 @@ where
pub fn document(input: &str) -> Res<&str, Vec<(Vec<TextElement>, &str)>> {
let initial_context: ContextTree<'_, '_> = ContextTree::new();
let paragraph_parser = parser_with_context!(paragraph);
let ret = many1(paragraph_parser(initial_context))(input);
let ret = many1(paragraph_parser(&initial_context))(input);
ret
}
@ -157,8 +157,8 @@ fn flat_text_element<'s, 'r>(
) -> Res<&'s str, TextElement<'s>> {
not(|i| context.check_fail_matcher(i))(i)?;
let bold_matcher = parser_with_context!(flat_bold)(context.clone());
let link_matcher = parser_with_context!(flat_link)(context.clone());
let bold_matcher = parser_with_context!(flat_bold)(&context);
let link_matcher = parser_with_context!(flat_link)(&context);
alt((
map(bold_matcher, TextElement::Bold),
@ -177,7 +177,7 @@ fn recognize_bold_end(input: &str) -> Res<&str, &str> {
}
fn flat_bold<'s, 'r>(context: Context<'r, 's>, i: &'s str) -> Res<&'s str, Bold<'s>> {
let bold_start = parser_with_context!(context_bold_start)(context.clone());
let bold_start = parser_with_context!(context_bold_start)(&context);
let nom_context =
context.with_additional_node(ContextElement::FailMatcherNode(FailMatcherNode {
fail_matcher: ChainBehavior::AndParent(Some(&recognize_bold_end)),
@ -199,7 +199,7 @@ fn flat_link<'s, 'r>(context: Context<'r, 's>, i: &'s str) -> Res<&'s str, Link<
fail_matcher: ChainBehavior::AndParent(Some(&recognize_link_end)),
}));
// let nom_context = context.with_additional_fail_matcher(&recognize_link_end);
let text_element_parser = parser_with_context!(flat_text_element)(nom_context);
let text_element_parser = parser_with_context!(flat_text_element)(&nom_context);
let (remaining, captured) = recognize(tuple((
link_start,
many_till(text_element_parser, link_end),