Skip to content

Commit

Permalink
Merge pull request #117 from pamburus/feature/optimizations
Browse files Browse the repository at this point in the history
new: Performance improvements
  • Loading branch information
pamburus authored Dec 29, 2023
2 parents cfaadd2 + 87fea5c commit 4392186
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ categories = ["command-line-utilities"]
description = "Utility for viewing json-formatted log files."
keywords = ["cli", "human", "log"]
name = "hl"
version = "0.24.0"
version = "0.24.1"
edition = "2021"
build = "build.rs"

Expand Down
1 change: 1 addition & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,7 @@ impl<'a, Formatter: RecordWithSourceFormatter, Filter: RecordFilter> SegmentProc
}
}

#[inline(always)]
fn show_unparsed(&self) -> bool {
self.options.allow_unparsed_data
}
Expand Down
3 changes: 3 additions & 0 deletions src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,21 @@ pub trait RecordWithSourceFormatter {
pub struct RawRecordFormatter {}

impl RecordWithSourceFormatter for RawRecordFormatter {
#[inline(always)]
fn format_record(&self, buf: &mut Buf, rec: model::RecordWithSource) {
buf.extend_from_slice(rec.source);
}
}

impl<T: RecordWithSourceFormatter> RecordWithSourceFormatter for &T {
#[inline(always)]
fn format_record(&self, buf: &mut Buf, rec: model::RecordWithSource) {
(**self).format_record(buf, rec)
}
}

impl RecordWithSourceFormatter for Box<dyn RecordWithSourceFormatter> {
#[inline(always)]
fn format_record(&self, buf: &mut Buf, rec: model::RecordWithSource) {
(**self).format_record(buf, rec)
}
Expand Down
33 changes: 33 additions & 0 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,22 @@ pub struct Record<'a> {
}

impl<'a> Record<'a> {
#[inline(always)]
pub fn fields(&self) -> impl Iterator<Item = &(&'a str, &'a RawValue)> {
self.extra.iter().chain(self.extrax.iter())
}

#[inline(always)]
pub fn fields_for_search(&self) -> impl Iterator<Item = &(&'a str, &'a RawValue)> {
self.fields().chain(self.predefined.iter())
}

#[inline(always)]
pub fn matches<F: RecordFilter>(&self, filter: F) -> bool {
filter.apply(self)
}

#[inline(always)]
pub fn with_prefix(mut self, prefix: &'a [u8]) -> Self {
self.prefix = Some(prefix);

Expand Down Expand Up @@ -96,12 +100,14 @@ pub struct RecordWithSource<'a> {
}

impl<'a> RecordWithSource<'a> {
#[inline(always)]
pub fn new(record: &'a Record<'a>, source: &'a [u8]) -> Self {
Self { record, source }
}
}

impl RecordWithSourceConstructor for Record<'_> {
#[inline(always)]
fn with_source<'a>(&'a self, source: &'a [u8]) -> RecordWithSource<'a> {
RecordWithSource::new(self, source)
}
Expand All @@ -112,6 +118,7 @@ impl RecordWithSourceConstructor for Record<'_> {
pub trait RecordFilter {
fn apply<'a>(&self, record: &'a Record<'a>) -> bool;

#[inline(always)]
fn and<F>(self, rhs: F) -> RecordFilterAnd<Self, F>
where
Self: Sized,
Expand All @@ -120,6 +127,7 @@ pub trait RecordFilter {
RecordFilterAnd { lhs: self, rhs }
}

#[inline(always)]
fn or<F>(self, rhs: F) -> RecordFilterOr<Self, F>
where
Self: Sized,
Expand All @@ -130,18 +138,21 @@ pub trait RecordFilter {
}

impl<T: RecordFilter + ?Sized> RecordFilter for Box<T> {
#[inline(always)]
fn apply<'a>(&self, record: &'a Record<'a>) -> bool {
(**self).apply(record)
}
}

impl<T: RecordFilter + ?Sized> RecordFilter for &T {
#[inline(always)]
fn apply<'a>(&self, record: &'a Record<'a>) -> bool {
(**self).apply(record)
}
}

impl<T: RecordFilter> RecordFilter for Option<T> {
#[inline(always)]
fn apply<'a>(&self, record: &'a Record<'a>) -> bool {
if let Some(filter) = self {
filter.apply(record)
Expand All @@ -159,6 +170,7 @@ pub struct RecordFilterAnd<L: RecordFilter, R: RecordFilter> {
}

impl<L: RecordFilter, R: RecordFilter> RecordFilter for RecordFilterAnd<L, R> {
#[inline(always)]
fn apply<'a>(&self, record: &'a Record<'a>) -> bool {
self.lhs.apply(record) && self.rhs.apply(record)
}
Expand All @@ -172,6 +184,7 @@ pub struct RecordFilterOr<L: RecordFilter, R: RecordFilter> {
}

impl<L: RecordFilter, R: RecordFilter> RecordFilter for RecordFilterOr<L, R> {
#[inline(always)]
fn apply<'a>(&self, record: &'a Record<'a>) -> bool {
self.lhs.apply(record) || self.rhs.apply(record)
}
Expand All @@ -182,6 +195,7 @@ impl<L: RecordFilter, R: RecordFilter> RecordFilter for RecordFilterOr<L, R> {
pub struct RecordFilterNone;

impl RecordFilter for RecordFilterNone {
#[inline(always)]
fn apply<'a>(&self, _: &'a Record<'a>) -> bool {
true
}
Expand Down Expand Up @@ -280,10 +294,12 @@ impl ParserSettings {
}
}

#[inline(always)]
fn apply<'a>(&self, key: &'a str, value: &'a RawValue, to: &mut Record<'a>, pc: &mut PriorityController) {
self.blocks[0].apply(self, key, value, to, pc, true);
}

#[inline(always)]
fn apply_each<'a, 'i, I>(&self, items: I, to: &mut Record<'a>)
where
I: IntoIterator<Item = &'i (&'a str, &'a RawValue)>,
Expand All @@ -293,6 +309,7 @@ impl ParserSettings {
self.apply_each_ctx(items, to, &mut pc);
}

#[inline(always)]
fn apply_each_ctx<'a, 'i, I>(&self, items: I, to: &mut Record<'a>, pc: &mut PriorityController)
where
I: IntoIterator<Item = &'i (&'a str, &'a RawValue)>,
Expand Down Expand Up @@ -351,6 +368,7 @@ impl ParserSettingsBlock {
}
}

#[inline(always)]
fn apply_each_ctx<'a, 'i, I>(
&self,
ps: &ParserSettings,
Expand Down Expand Up @@ -382,6 +400,7 @@ struct PriorityController {
}

impl PriorityController {
#[inline(always)]
fn prioritize<F: FnOnce(&mut Self) -> ()>(&mut self, kind: FieldKind, priority: usize, update: F) -> bool {
let p = match kind {
FieldKind::Time => &mut self.time,
Expand Down Expand Up @@ -466,6 +485,7 @@ impl FieldSettings {
}
}

#[inline(always)]
fn apply_ctx<'a>(
&self,
ps: &ParserSettings,
Expand All @@ -486,6 +506,7 @@ impl FieldSettings {
}
}

#[inline(always)]
fn kind(&self) -> Option<FieldKind> {
match self {
Self::Time => Some(FieldKind::Time),
Expand Down Expand Up @@ -530,6 +551,7 @@ pub struct RawRecord<'a> {
}

impl<'a> RawRecord<'a> {
#[inline(always)]
pub fn fields(&self) -> impl Iterator<Item = &(&'a str, &'a RawValue)> {
self.fields.iter().chain(self.fieldsx.iter())
}
Expand All @@ -551,6 +573,7 @@ struct RawRecordVisitor<'a> {
}

impl<'a> RawRecordVisitor<'a> {
#[inline(always)]
fn new() -> Self {
Self { marker: PhantomData }
}
Expand Down Expand Up @@ -596,6 +619,7 @@ pub struct KeyMatcher<'a> {
}

impl<'a> KeyMatcher<'a> {
#[inline(always)]
pub fn new(key: &'a str) -> Self {
Self { key }
}
Expand Down Expand Up @@ -624,6 +648,7 @@ impl<'a> KeyMatcher<'a> {
}
}

#[inline(always)]
fn norm(c: char) -> char {
if c == '_' {
'-'
Expand Down Expand Up @@ -704,6 +729,7 @@ impl UnaryBoolOp {
}

impl Default for UnaryBoolOp {
#[inline(always)]
fn default() -> Self {
Self::None
}
Expand All @@ -718,6 +744,7 @@ pub enum FieldFilterKey<S> {
}

impl FieldFilterKey<String> {
#[inline(always)]
pub fn borrowed(&self) -> FieldFilterKey<&str> {
match self {
FieldFilterKey::Predefined(kind) => FieldFilterKey::Predefined(*kind),
Expand Down Expand Up @@ -797,6 +824,7 @@ impl FieldFilter {
})
}

#[inline(always)]
fn match_custom_key<'a>(&'a self, key: &str) -> Option<KeyMatch<'a>> {
if let FieldFilterKey::Custom(k) = &self.key {
if self.flat_key && k.len() != key.len() {
Expand Down Expand Up @@ -925,6 +953,7 @@ impl FieldFilterSet {
}

impl RecordFilter for FieldFilterSet {
#[inline(always)]
fn apply<'a>(&self, record: &'a Record<'a>) -> bool {
self.0.iter().all(|field| field.apply(record))
}
Expand All @@ -941,6 +970,7 @@ pub struct Filter {
}

impl Filter {
#[inline(always)]
pub fn is_empty(&self) -> bool {
self.fields.0.is_empty() && self.level.is_none() && self.since.is_none() && self.until.is_none()
}
Expand Down Expand Up @@ -991,6 +1021,7 @@ struct ObjectVisitor<'a> {
marker: PhantomData<fn() -> Object<'a>>,
}
impl<'a> ObjectVisitor<'a> {
#[inline(always)]
fn new() -> Self {
Self { marker: PhantomData }
}
Expand Down Expand Up @@ -1028,6 +1059,7 @@ pub struct Array<'a, const N: usize> {
}

impl<'a, const N: usize> Array<'a, N> {
#[inline(always)]
pub fn iter(&self) -> impl Iterator<Item = &&'a RawValue> {
self.items.iter().chain(self.more.iter())
}
Expand All @@ -1037,6 +1069,7 @@ struct ArrayVisitor<'a, const N: usize> {
marker: PhantomData<fn() -> Array<'a, N>>,
}
impl<'a, const N: usize> ArrayVisitor<'a, N> {
#[inline(always)]
fn new() -> Self {
Self { marker: PhantomData }
}
Expand Down
1 change: 0 additions & 1 deletion src/scanning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,6 @@ impl<'a, 'b> Iterator for ScannerIter<'a, 'b> {
};

let result = self.next.replace(next);
self.placement = placement;
return if result.size != 0 {
Some(Ok(Segment::new(result, placement)))
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/serdex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ where
T: serde::de::Deserialize<'de>,
{
type Item = serde_json::Result<(T, Range<usize>)>;

#[inline(always)]
fn next(&mut self) -> Option<Self::Item> {
let start_offset = self.0.byte_offset();
self.0
Expand Down

0 comments on commit 4392186

Please sign in to comment.