From 6adc8f442156566df7e25159513246b015160fea Mon Sep 17 00:00:00 2001 From: Omkar Wagholikar Date: Thu, 15 Oct 2026 07:18:41 +0530 Subject: [PATCH] fast_regex: store literal needles in an Arc re_or_saved_re() (src/sed/processor.rs:192) copies the regex for every input line. With an Arc<[u8]>, that copy shares the existing pattern instead of allocating a new one. --- src/sed/fast_regex.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/sed/fast_regex.rs b/src/sed/fast_regex.rs index 9fa87be3..afa0bb09 100644 --- a/src/sed/fast_regex.rs +++ b/src/sed/fast_regex.rs @@ -18,7 +18,7 @@ use memchr::memmem; use regex::bytes::{ CaptureMatches as ByteCaptureMatches, Captures as ByteCaptures, Regex as ByteRegex, }; -use std::sync::LazyLock; +use std::sync::{Arc, LazyLock}; use uucore::error::{UResult, USimpleError}; use crate::sed::command::CharacterMode; @@ -89,7 +89,7 @@ enum AnchoredMatch { #[derive(Clone, Debug)] /// A fast Regex-like matcher for literal strings using memchr:memmem pub struct LiteralMatcher { - needle: Vec, // Bytes without any anchors + needle: Arc<[u8]>, // Bytes without any anchors; cheap to clone match_type: AnchoredMatch, // Type of anchoring specified } @@ -100,22 +100,22 @@ impl LiteralMatcher { if needle_bytes[0] == b'^' && needle_bytes[needle_bytes.len() - 1] == b'$' { LiteralMatcher { match_type: AnchoredMatch::Both, - needle: needle_bytes[1..needle_bytes.len() - 1].to_vec(), + needle: needle_bytes[1..needle_bytes.len() - 1].into(), } } else if needle_bytes[0] == b'^' { LiteralMatcher { match_type: AnchoredMatch::Begin, - needle: needle_bytes[1..needle_bytes.len()].to_vec(), + needle: needle_bytes[1..needle_bytes.len()].into(), } } else if needle_bytes[needle_bytes.len() - 1] == b'$' { LiteralMatcher { match_type: AnchoredMatch::End, - needle: needle_bytes[0..needle_bytes.len() - 1].to_vec(), + needle: needle_bytes[0..needle_bytes.len() - 1].into(), } } else { LiteralMatcher { match_type: AnchoredMatch::Free, - needle: needle_bytes.to_vec(), + needle: needle_bytes.into(), } } } @@ -127,21 +127,21 @@ impl LiteralMatcher { match self.match_type { AnchoredMatch::Both => { - if hlen == nlen && haystack == self.needle.as_slice() { + if hlen == nlen && haystack == &*self.needle { Some(0) } else { None } } AnchoredMatch::Begin => { - if hlen >= nlen && &haystack[..nlen] == self.needle.as_slice() { + if hlen >= nlen && haystack[..nlen] == *self.needle { Some(0) } else { None } } AnchoredMatch::End => { - if hlen >= nlen && &haystack[hlen - nlen..] == self.needle.as_slice() { + if hlen >= nlen && haystack[hlen - nlen..] == *self.needle { Some(hlen - nlen) } else { None