From fd0be1bf5960c4bfcb978cce692c30f8679e1ede Mon Sep 17 00:00:00 2001 From: Pavel Ivanov Date: Wed, 2 Feb 2022 22:06:45 +0300 Subject: [PATCH 1/2] Revert "new: pool module" 1c4bf7c5dd1d957de7cfcc7cc596a43cdccd0e57 --- src/lib.rs | 1 - src/pool.rs | 174 ------------------------------------------------ src/scanning.rs | 103 ++++++++-------------------- 3 files changed, 28 insertions(+), 250 deletions(-) delete mode 100644 src/pool.rs diff --git a/src/lib.rs b/src/lib.rs index b5171d4e..2c2e6cc0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,7 +18,6 @@ mod eseq; mod filtering; mod formatting; mod model; -mod pool; mod scanning; // conditional public modules diff --git a/src/pool.rs b/src/pool.rs deleted file mode 100644 index aa47538e..00000000 --- a/src/pool.rs +++ /dev/null @@ -1,174 +0,0 @@ -// third-party imports -use crossbeam_queue::SegQueue; - -// --- - -pub trait Pool: Checkout + Checkin {} - -impl + Checkin> Pool for U {} - -// --- - -pub trait Checkout { - fn checkout(&self) -> T; -} - -// --- - -pub trait Checkin { - fn checkin(&self, item: T); -} - -// --- - -pub trait Factory { - fn new(&self) -> T; -} - -impl Factory for F -where - F: Fn() -> T, -{ - #[inline(always)] - fn new(&self) -> T { - self() - } -} - -// --- - -pub trait Recycler { - fn recycle(&self, item: T) -> T; -} - -impl Recycler for F -where - F: Fn(T) -> T, -{ - #[inline(always)] - fn recycle(&self, item: T) -> T { - self(item) - } -} - -// --- - -pub struct DefaultFactory; - -impl Factory for DefaultFactory { - #[inline(always)] - fn new(&self) -> T { - T::default() - } -} - -// --- - -pub struct RecycleAsIs; - -impl Recycler for RecycleAsIs { - #[inline(always)] - fn recycle(&self, item: T) -> T { - item - } -} - -// --- - -/// Constructs new items of type T using Factory F and recycles them using Recycler R on request. -pub struct SQPool -where - F: Factory, - R: Recycler, -{ - factory: F, - recycler: R, - recycled: SegQueue, -} - -impl SQPool -where - T: Default, -{ - /// Returns a new Pool with default factory. - pub fn new() -> SQPool { - SQPool { - factory: DefaultFactory, - recycler: RecycleAsIs, - recycled: SegQueue::new(), - } - } -} - -impl SQPool -where - F: Factory, -{ - /// Returns a new Pool with the given factory. - pub fn new_with_factory(factory: F) -> SQPool { - SQPool { - factory, - recycler: RecycleAsIs, - recycled: SegQueue::new(), - } - } -} - -impl SQPool -where - F: Factory, - R: Recycler, -{ - /// Converts the Pool to a new Pool with the given factory. - pub fn with_factory>(self, factory: F2) -> SQPool { - SQPool { - factory, - recycler: self.recycler, - recycled: self.recycled, - } - } - - /// Converts the Pool to a new Pool with the given recycle function. - pub fn with_recycler>(self, recycler: R2) -> SQPool { - SQPool { - factory: self.factory, - recycler, - recycled: self.recycled, - } - } - /// Returns a new or recycled T. - #[inline(always)] - pub fn checkout(&self) -> T { - match self.recycled.pop() { - Some(item) => item, - None => self.factory.new(), - } - } - /// Recycles the given T. - #[inline(always)] - pub fn checkin(&self, item: T) { - self.recycled.push(self.recycler.recycle(item)) - } -} - -impl Checkout for SQPool -where - F: Factory, - R: Recycler, -{ - #[inline(always)] - fn checkout(&self) -> T { - self.checkout() - } -} - -impl Checkin for SQPool -where - F: Factory, - R: Recycler, -{ - #[inline(always)] - fn checkin(&self, item: T) { - self.checkin(item) - } -} diff --git a/src/scanning.rs b/src/scanning.rs index 50206c70..6a3c8246 100644 --- a/src/scanning.rs +++ b/src/scanning.rs @@ -4,9 +4,11 @@ use std::convert::From; use std::io::Read; use std::sync::Arc; +// third-party imports +use crossbeam_queue::SegQueue; + // local imports use crate::error::*; -use crate::pool::{Factory, Recycler, SQPool}; // --- @@ -43,26 +45,22 @@ pub struct SegmentBuf { impl SegmentBuf { /// Returns a reference to the contained data. - #[inline(always)] pub fn data(&self) -> &[u8] { &self.data[..self.size] } /// Converts the SegmentBuf to a Vec. - #[inline(always)] pub fn to_vec(mut self) -> Vec { self.data.resize(self.size, 0); self.data } - #[inline] fn new(capacity: usize) -> Self { let mut data = Vec::with_capacity(capacity); data.resize(capacity, 0); Self { data, size: 0 } } - #[inline(always)] fn zero() -> Self { Self { data: Vec::new(), @@ -70,19 +68,16 @@ impl SegmentBuf { } } - #[inline(always)] fn reset(&mut self) { self.data.resize(self.data.capacity(), 0); self.size = 0; } - #[inline(always)] fn resetted(mut self) -> Self { self.reset(); self } - #[inline(always)] fn replace(&mut self, mut other: Self) -> Self { std::mem::swap(self, &mut other); other @@ -90,7 +85,6 @@ impl SegmentBuf { } impl PartialEq for SegmentBuf { - #[inline(always)] fn eq(&self, other: &Self) -> bool { self.size == other.size && self.data().eq(other.data()) } @@ -107,7 +101,6 @@ impl std::fmt::Debug for SegmentBuf { } impl> From for SegmentBuf { - #[inline(always)] fn from(data: T) -> Self { let size = data.as_ref().len(); Self { @@ -130,7 +123,6 @@ pub enum Segment { impl Segment { /// Returns a new Segment containing the given SegmentBuf. - #[inline(always)] fn new(buf: SegmentBuf, placement: Option) -> Self { if let Some(placement) = placement { Self::Incomplete(buf, placement) @@ -154,50 +146,32 @@ pub enum PartialPlacement { /// Constructs new SegmentBuf's with the configures size and recycles unneeded SegmentBuf's. pub struct SegmentBufFactory { - pool: SQPool, + buf_size: usize, + recycled: SegQueue, } impl SegmentBufFactory { /// Returns a new SegmentBufFactory with the given parameters. - pub fn new(buf_size: usize) -> SegmentBufFactory { - return SegmentBufFactory { - pool: SQPool::new_with_factory(SBFFactory { buf_size }).with_recycler(SBFRecycler), + pub fn new(buf_size: usize) -> Self { + return Self { + buf_size, + recycled: SegQueue::new(), }; } /// Returns a new or recycled SegmentBuf. - #[inline(always)] pub fn new_segment(&self) -> SegmentBuf { - self.pool.checkout() + match self.recycled.pop() { + Some(buf) => buf.resetted(), + None => SegmentBuf::new(self.buf_size), + } } /// Recycles the given SegmentBuf. - #[inline(always)] pub fn recycle(&self, buf: SegmentBuf) { - self.pool.checkin(buf) - } -} - -// -- - -struct SBFFactory { - buf_size: usize, -} - -impl Factory for SBFFactory { - #[inline(always)] - fn new(&self) -> SegmentBuf { - SegmentBuf::new(self.buf_size) - } -} -// -- - -struct SBFRecycler; - -impl Recycler for SBFRecycler { - #[inline(always)] - fn recycle(&self, buf: SegmentBuf) -> SegmentBuf { - buf.resetted() + if buf.data.capacity() == self.buf_size { + self.recycled.push(buf); + } } } @@ -205,54 +179,33 @@ impl Recycler for SBFRecycler { /// Constructs new raw Vec buffers with the configured size. pub struct BufFactory { - pool: SQPool, RawBufFactory, RawBufRecycler>, + buf_size: usize, + recycled: SegQueue>, } impl BufFactory { /// Returns a new BufFactory with the given parameters. pub fn new(buf_size: usize) -> Self { return Self { - pool: SQPool::new() - .with_factory(RawBufFactory { buf_size }) - .with_recycler(RawBufRecycler), + buf_size, + recycled: SegQueue::new(), }; } /// Returns a new or recycled buffer. - #[inline(always)] pub fn new_buf(&self) -> Vec { - self.pool.checkout() + match self.recycled.pop() { + Some(mut buf) => { + buf.resize(0, 0); + buf + } + None => Vec::with_capacity(self.buf_size), + } } /// Recycles the given buffer. - #[inline(always)] pub fn recycle(&self, buf: Vec) { - self.pool.checkin(buf); - } -} - -// --- - -struct RawBufFactory { - buf_size: usize, -} - -impl Factory> for RawBufFactory { - #[inline(always)] - fn new(&self) -> Vec { - Vec::with_capacity(self.buf_size) - } -} - -// --- - -struct RawBufRecycler; - -impl Recycler> for RawBufRecycler { - #[inline(always)] - fn recycle(&self, mut buf: Vec) -> Vec { - buf.resize(0, 0); - buf + self.recycled.push(buf); } } From c051f7d84e43e0e1ce6ec3dae47baa404d92b8be Mon Sep 17 00:00:00 2001 From: Pavel Ivanov Date: Thu, 3 Feb 2022 00:00:30 +0300 Subject: [PATCH 2/2] new: change version to 0.10.8 --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1dd6e759..d65d4bd8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -663,7 +663,7 @@ dependencies = [ [[package]] name = "hl" -version = "0.10.7" +version = "0.10.8" dependencies = [ "ansi_term", "anyhow", diff --git a/Cargo.toml b/Cargo.toml index e4791bd9..91a71b2f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ categories = ["command-line-utilities"] description = "Utility for viewing json-formatted log files." keywords = ["cli", "human", "log"] name = "hl" -version = "0.10.7" +version = "0.10.8" edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html