Skip to content

Commit

Permalink
refactor: prefering Box<[T]> vs Vec<T> for increased performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Gnarus-G committed Jun 16, 2023
1 parent bc67915 commit 75e9348
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
4 changes: 3 additions & 1 deletion mrp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ pub mod parser;

use std::borrow::Cow;

pub type Array<T> = Box<[T]>;

use parser::{AbstractReplaceExpression, MatchAndReplaceExpression, MatchExpression};

/// Representing a stragety by which to match and replace on a `string` value
Expand All @@ -16,7 +18,7 @@ pub trait MatchAndReplaceStrategy<'input> {

pub struct MatchAndReplacer<'source> {
mex: MatchExpression<'source>,
exprs: Vec<AbstractReplaceExpression<'source>>,
exprs: Array<AbstractReplaceExpression<'source>>,
/// When true, this strategy will replace the matching range found, and strip everything else
/// off.
strip: bool,
Expand Down
13 changes: 9 additions & 4 deletions mrp/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::str::FromStr;
use crate::{
error::{ParseError, ParseErrorKind, Result},
lexer::{Lexer, Token, TokenKind},
Array,
};

#[derive(Debug, PartialEq, Clone)]
Expand Down Expand Up @@ -52,7 +53,7 @@ impl<'source> MatchExpression<'source> {

#[derive(Debug, PartialEq)]
pub struct ReplaceExpression<'source> {
pub expressions: Vec<AbstractReplaceExpression<'source>>,
pub expressions: Array<AbstractReplaceExpression<'source>>,
}

#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -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>> {
Expand All @@ -260,6 +263,8 @@ impl<'source> Parser<'source> {
#[cfg(test)]
mod tests {

use std::sync::Arc;

use super::*;

#[test]
Expand Down Expand Up @@ -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")
]
])
}
)
}
Expand Down

0 comments on commit 75e9348

Please sign in to comment.