From a0376d361b83360c6f9eb971419726995f4c9c19 Mon Sep 17 00:00:00 2001 From: Jonas Maier Date: Sat, 6 Jun 2026 21:11:02 +0200 Subject: regex: perf improvement by using ops that do not free and alloc again --- src/regex/bc.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/regex/bc.rs b/src/regex/bc.rs index 5de1513..ea1d086 100644 --- a/src/regex/bc.rs +++ b/src/regex/bc.rs @@ -114,8 +114,8 @@ struct Thread { struct VM<'p, F: Flavor> { instr: &'p [Instr], - passive_threads: VecDeque>, - active_threads: VecDeque>, + passive_threads: Vec>, + active_threads: Vec>, hot: BitSet, warm: BitSet, } @@ -124,8 +124,8 @@ impl<'p, F: Flavor> VM<'p, F> { fn new(instr: &'p [Instr], starting_thread: Thread) -> Self { Self { instr, - passive_threads: vec![starting_thread].into(), - active_threads: VecDeque::new(), + passive_threads: vec![starting_thread], + active_threads: Vec::new(), hot: BitSet::new(instr.len()), warm: BitSet::new(instr.len()), } @@ -142,7 +142,7 @@ impl<'p, F: Flavor> VM<'p, F> { let bit = t.pc as usize; if !self.warm.get(bit) { self.warm.set(bit, true); - self.active_threads.push_front(t); + self.active_threads.push(t); } }}; } @@ -157,13 +157,13 @@ impl<'p, F: Flavor> VM<'p, F> { }}; } - while let Some(mut thread) = self.active_threads.pop_front() { + while let Some(mut thread) = self.active_threads.pop() { loop { match self.instr[thread.pc as usize] { Instr::Class(_) | Instr::Consume(_) => { if !self.hot.get(thread.pc as usize) { self.hot.set(thread.pc as usize, true); - self.passive_threads.push_back(thread); + self.passive_threads.push(thread); } } Instr::Jump(j) => { @@ -188,6 +188,8 @@ impl<'p, F: Flavor> VM<'p, F> { break; } } + + self.passive_threads.reverse(); } fn step_consume(&mut self, byte: u8) { -- cgit v1.2.3