diff --git a/benches/benches.rs b/benches/benches.rs index 36b9614..fe37c5a 100644 --- a/benches/benches.rs +++ b/benches/benches.rs @@ -11,14 +11,14 @@ fn bench_parser(c: &mut Criterion) { group.throughput(Throughput::Bytes(input_pp.len() as u64)); group.bench_function("rust-sexp", |b| b.iter(|| { - let mut parser = parser::State::from_sexp_factory(rust_parser::SexpFactory::new()); - let result = parser.process_all(parser::SegmentIndex::EntireFile, input_pp).unwrap(); + let mut parser = parser::parser_from_sexp_factory(rust_parser::SexpFactory::new()); + let result = parser.process(parser::SegmentIndex::EntireFile, input_pp).unwrap(); black_box(result) })); group.bench_function("rust-tape", |b| b.iter(|| { - let mut parser = parser::State::from_visitor(rust_parser::TapeVisitor::new()); - let result = parser.process_all(parser::SegmentIndex::EntireFile,input_pp).unwrap(); + let mut parser = parser::parser_from_visitor(rust_parser::TapeVisitor::new()); + let result = parser.process(parser::SegmentIndex::EntireFile,input_pp).unwrap(); black_box(result) })); group.finish(); @@ -32,14 +32,14 @@ fn bench_parser(c: &mut Criterion) { group.throughput(Throughput::Bytes(input_mach.len() as u64)); group.bench_function("rust-sexp", |b| b.iter(|| { - let mut parser = parser::State::from_sexp_factory(rust_parser::SexpFactory::new()); - let result = parser.process_all(parser::SegmentIndex::EntireFile,input_mach).unwrap(); + let mut parser = parser::parser_from_sexp_factory(rust_parser::SexpFactory::new()); + let result = parser.process(parser::SegmentIndex::EntireFile,input_mach).unwrap(); black_box(result) })); group.bench_function("rust-tape", |b| b.iter(|| { - let mut parser = parser::State::from_visitor(rust_parser::TapeVisitor::new()); - let result = parser.process_all(parser::SegmentIndex::EntireFile,input_mach).unwrap(); + let mut parser = parser::parser_from_visitor(rust_parser::TapeVisitor::new()); + let result = parser.process(parser::SegmentIndex::EntireFile,input_mach).unwrap(); black_box(result) })); group.finish(); diff --git a/src/clmul.rs b/src/clmul.rs index 2db040e..c113a1b 100644 --- a/src/clmul.rs +++ b/src/clmul.rs @@ -1,8 +1,3 @@ -#[cfg(target_arch = "x86")] -use core::arch::x86::*; -#[cfg(target_arch = "x86_64")] -use core::arch::x86_64::*; - pub trait Clmul { fn clmul(&self, input: u64) -> u64; } @@ -32,6 +27,12 @@ impl Clmul for Generic { #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] mod x86 { + use super::Clmul; + #[cfg(target_arch = "x86")] + use core::arch::x86::*; + #[cfg(target_arch = "x86_64")] + use core::arch::x86_64::*; + #[derive(Copy, Clone, Debug)] pub struct Sse2Pclmulqdq { _feature_detected_witness: () } @@ -62,7 +63,7 @@ mod x86 { } #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] -pub use x86; +pub use x86::*; impl Clmul for Box { fn clmul(&self, input: u64) -> u64 { diff --git a/src/parser.rs b/src/parser.rs index d16cf23..32a5aed 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -184,8 +184,6 @@ impl State(&mut self, segment_index: SegmentIndex, buf_reader: &mut BufReadT) -> Result { - use structural::Classifier; - let mut input_index = 0; let mut indices_len = 0; let mut indices_buffer = [0; INDICES_BUFFER_MAX_LEN]; @@ -274,8 +272,6 @@ impl State Result { - use structural::Classifier; - let mut input_index = 0; let mut indices_len = 0; let mut indices_buffer = [0; INDICES_BUFFER_MAX_LEN]; diff --git a/src/start_stop_transitions.rs b/src/start_stop_transitions.rs index 6dbf544..9a134ef 100644 --- a/src/start_stop_transitions.rs +++ b/src/start_stop_transitions.rs @@ -1,8 +1,3 @@ -#[cfg(target_arch = "x86")] -use core::arch::x86::*; -#[cfg(target_arch = "x86_64")] -use core::arch::x86_64::*; - use crate::clmul; use crate::xor_masked_adjacent; @@ -34,6 +29,13 @@ impl { fn start_stop_transitions(&self, start: u64, stop: u64, prev_state: bool) -> (u64, bool) { diff --git a/src/structural.rs b/src/structural.rs index 1b76e3a..064b6b2 100644 --- a/src/structural.rs +++ b/src/structural.rs @@ -1,11 +1,4 @@ -#[cfg(target_arch = "x86")] -use core::arch::x86::*; -#[cfg(target_arch = "x86_64")] -use core::arch::x86_64::*; - -use crate::{clmul, xor_masked_adjacent, vector_classifier, utils, find_quote_transitions, ranges}; -use vector_classifier::ClassifierBuilder; -use clmul::Clmul; +use crate::vector_classifier; pub enum CallbackResult { Continue, @@ -22,7 +15,7 @@ pub trait Classifier { (&mut self, input_buf: &[u8], f: F); } -#[derive(Debug, Eq, PartialEq)] +#[derive(Copy, Clone, Debug, Eq, PartialEq)] pub struct Generic { escape: bool, quote_state: bool, @@ -87,7 +80,18 @@ pub fn not_atom_like_lookup_tables() -> vector_classifier::LookupTables { #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] mod x86 { - #[derive(Debug)] + #[cfg(target_arch = "x86")] + use core::arch::x86::*; + #[cfg(target_arch = "x86_64")] + use core::arch::x86_64::*; + + use crate::{clmul, vector_classifier, xor_masked_adjacent, utils, find_quote_transitions, ranges}; + use vector_classifier::ClassifierBuilder; + use clmul::Clmul; + + use super::{Classifier, CallbackResult, Generic, not_atom_like_lookup_tables}; + + #[derive(Clone, Debug)] pub struct Avx2 { /* constants */ clmul: clmul::Sse2Pclmulqdq, @@ -144,6 +148,11 @@ mod x86 { self.generic.atom_like = self.atom_like; } + pub fn get_generic_state(&mut self) -> Generic { + self.copy_state_to_generic(); + self.generic + } + #[target_feature(enable = "avx2,bmi2,sse2,ssse3,pclmulqdq")] #[inline] unsafe fn classify_one_avx2(&self, input: __m256i) -> ClassifyOneAvx2 @@ -246,7 +255,7 @@ mod x86 { } #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] -pub use x86; +pub use x86::*; #[cfg(test)] mod tests { @@ -296,8 +305,7 @@ mod tests { match Avx2::new() { Some(mut avx2) => { avx2.run_test(input, output); - avx2.copy_state_to_generic(); - assert_eq!(generic, avx2.generic); + assert_eq!(generic, avx2.get_generic_state()); }, None => (), } diff --git a/src/vector_classifier.rs b/src/vector_classifier.rs index 1e2e36e..1f06dd7 100644 --- a/src/vector_classifier.rs +++ b/src/vector_classifier.rs @@ -1,8 +1,4 @@ use std::collections::HashMap; -#[cfg(target_arch = "x86")] -use core::arch::x86::*; -#[cfg(target_arch = "x86_64")] -use core::arch::x86_64::*; #[derive(Clone, Debug)] pub struct LookupTables { @@ -133,6 +129,13 @@ impl ClassifierBuilder for GenericBuilder { #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] mod x86 { + #[cfg(target_arch = "x86")] + use core::arch::x86::*; + #[cfg(target_arch = "x86_64")] + use core::arch::x86_64::*; + + use super::{Classifier, ClassifierBuilder, GenericClassifier, LookupTables}; + #[derive(Clone, Debug)] pub struct Ssse3Classifier { generic: GenericClassifier, @@ -258,7 +261,7 @@ mod x86 { } #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] -pub use x86; +pub use x86::*; pub struct RuntimeDetectBuilder {} diff --git a/src/xor_masked_adjacent.rs b/src/xor_masked_adjacent.rs index a37b4ee..318225d 100644 --- a/src/xor_masked_adjacent.rs +++ b/src/xor_masked_adjacent.rs @@ -1,8 +1,3 @@ -#[cfg(target_arch = "x86")] -use core::arch::x86::*; -#[cfg(target_arch = "x86_64")] -use core::arch::x86_64::*; - pub trait XorMaskedAdjacent { fn xor_masked_adjacent(&self, bitstring: u64, mask: u64, lo_fill: bool) -> u64; } @@ -27,6 +22,13 @@ impl XorMaskedAdjacent for Generic { #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] mod x86 { + #[cfg(target_arch = "x86")] + use core::arch::x86::*; + #[cfg(target_arch = "x86_64")] + use core::arch::x86_64::*; + + use super::XorMaskedAdjacent; + #[derive(Copy, Clone, Debug)] pub struct Bmi2 { _feature_detected_witness: () @@ -61,7 +63,7 @@ mod x86 { } #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] -pub use x86; +pub use x86::*; impl XorMaskedAdjacent for Box { #[inline(always)]