Skip to content

Commit

Permalink
new: improved segment scanner performance
Browse files Browse the repository at this point in the history
  • Loading branch information
pamburus committed Jan 27, 2024
1 parent a579414 commit f5ba040
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 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.25.1-beta.1"
version = "0.25.1-beta.2"
edition = "2021"
build = "build.rs"

Expand Down
44 changes: 31 additions & 13 deletions src/scanning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,22 +241,40 @@ impl<'a, 'b> ScannerIter<'a, 'b> {
return None;
}

for i in (0..self.next.size - k + 1).rev() {
if self.next.data[i..].starts_with(self.scanner.delimiter.as_bytes()) {
let n = self.next.size - i - k;
let mut result = self.scanner.sf.new_segment();
if result.data.len() < n {
result.data.resize(n, 0);
}
if n > 0 {
result.data[..n].copy_from_slice(&self.next.data[i + k..i + k + n]);
result.size = n;
self.next.size -= n;
if k == 1 {
let delimiter = self.scanner.delimiter.as_bytes()[0];
self.next.data[..self.next.size]
.rsplit(|x| *x == delimiter)
.next()
.map(|data| data.len())
.and_then(|n| self.split_n(n))
} else {
for i in (0..self.next.size - k + 1).rev() {
if self.next.data[i..self.next.size].starts_with(self.scanner.delimiter.as_bytes()) {
return self.split_n(self.next.size - i - k);
}
return Some(result);
}
None
}
}

#[inline(always)]
fn split_n(&mut self, n: usize) -> Option<SegmentBuf> {
let s = self.next.size;
if n == 0 || n == s {
return None;
}

let mut result = self.scanner.sf.new_segment();
if result.data.len() < n {
result.data.resize(n, 0);
}
None

result.data[..n].copy_from_slice(&self.next.data[s - n..s]);
result.size = n;
self.next.size -= n;

Some(result)
}
}

Expand Down

0 comments on commit f5ba040

Please sign in to comment.