From 75e9348e3d8d25f8a8e92dff3f7d744c0278631d Mon Sep 17 00:00:00 2001 From: gnarus-g <37311893+Gnarus-G@users.noreply.github.com> Date: Thu, 15 Jun 2023 21:16:04 -0400 Subject: [PATCH] refactor: prefering Box<[T]> vs Vec for increased performance --- mrp/src/lib.rs | 4 +++- mrp/src/parser.rs | 13 +++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/mrp/src/lib.rs b/mrp/src/lib.rs index ec0513c..8fb1ff7 100644 --- a/mrp/src/lib.rs +++ b/mrp/src/lib.rs @@ -6,6 +6,8 @@ pub mod parser; use std::borrow::Cow; +pub type Array = Box<[T]>; + use parser::{AbstractReplaceExpression, MatchAndReplaceExpression, MatchExpression}; /// Representing a stragety by which to match and replace on a `string` value @@ -16,7 +18,7 @@ pub trait MatchAndReplaceStrategy<'input> { pub struct MatchAndReplacer<'source> { mex: MatchExpression<'source>, - exprs: Vec>, + exprs: Array>, /// When true, this strategy will replace the matching range found, and strip everything else /// off. strip: bool, diff --git a/mrp/src/parser.rs b/mrp/src/parser.rs index 31e3499..53af4a7 100644 --- a/mrp/src/parser.rs +++ b/mrp/src/parser.rs @@ -3,6 +3,7 @@ use std::str::FromStr; use crate::{ error::{ParseError, ParseErrorKind, Result}, lexer::{Lexer, Token, TokenKind}, + Array, }; #[derive(Debug, PartialEq, Clone)] @@ -52,7 +53,7 @@ impl<'source> MatchExpression<'source> { #[derive(Debug, PartialEq)] pub struct ReplaceExpression<'source> { - pub expressions: Vec>, + pub expressions: Array>, } #[derive(Debug, PartialEq)] @@ -235,7 +236,9 @@ impl<'source> Parser<'source> { token = self.token(); } - Ok(ReplaceExpression { expressions }) + Ok(ReplaceExpression { + expressions: expressions.into(), + }) } pub fn parse(&mut self) -> Result<'source, MatchAndReplaceExpression<'source>> { @@ -260,6 +263,8 @@ impl<'source> Parser<'source> { #[cfg(test)] mod tests { + use std::sync::Arc; + use super::*; #[test] @@ -378,10 +383,10 @@ mod tests { assert_eq!( p.parse_replacement_exp(vec!["num"]).unwrap(), ReplaceExpression { - expressions: vec![ + expressions: Box::new([ AbstractReplaceExpression::Literal("lul"), AbstractReplaceExpression::Identifier("num") - ] + ]) } ) }