mirror of
https://github.com/uutils/grep.git
synced 2026-06-10 16:15:11 -07:00
Compare commits
8
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6e6db248f1 | ||
|
|
b5816820ed | ||
|
|
ede1676d1a | ||
|
|
b46a86d48a | ||
|
|
b0164440e3 | ||
|
|
96762f26ca | ||
|
|
c8dfef6563 | ||
|
|
ddac723054 |
@@ -0,0 +1,55 @@
|
||||
# See https://pre-commit.com for more information
|
||||
# See https://pre-commit.com/hooks.html for more hooks
|
||||
exclude: ^tests/fixtures/
|
||||
repos:
|
||||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||
rev: v6.0.0
|
||||
hooks:
|
||||
- id: check-added-large-files
|
||||
- id: check-executables-have-shebangs
|
||||
- id: check-json
|
||||
exclude: '\.vscode/(cSpell|extensions)\.json' # cSpell.json and extensions.json use comments
|
||||
- id: check-shebang-scripts-are-executable
|
||||
exclude: '.+\.rs' # would be triggered by #![some_attribute]
|
||||
- id: check-symlinks
|
||||
- id: check-toml
|
||||
- id: check-yaml
|
||||
args: [ --allow-multiple-documents ]
|
||||
- id: destroyed-symlinks
|
||||
- id: end-of-file-fixer
|
||||
- id: mixed-line-ending
|
||||
args: [ --fix=lf ]
|
||||
- id: trailing-whitespace
|
||||
|
||||
- repo: local
|
||||
hooks:
|
||||
- id: rust-linting
|
||||
name: Rust linting
|
||||
description: Run cargo fmt on files included in the commit.
|
||||
entry: cargo +stable fmt --
|
||||
pass_filenames: true
|
||||
types: [file, rust]
|
||||
language: system
|
||||
- id: rust-clippy
|
||||
name: Rust clippy
|
||||
description: Run cargo clippy on files included in the commit.
|
||||
entry: cargo +stable clippy --workspace --all-targets --all-features -- -D warnings
|
||||
pass_filenames: false
|
||||
types: [file, rust]
|
||||
language: system
|
||||
- id: cargo-lock-check
|
||||
name: Cargo.lock sync check
|
||||
description: Ensure Cargo.lock and fuzz/Cargo.lock are up-to-date.
|
||||
entry: bash -c 'for dir in . fuzz; do if [ -d "$dir" ]; then ( cd "$dir" && cargo fetch --quiet ); fi; done'
|
||||
pass_filenames: false
|
||||
files: 'Cargo\.(toml|lock)$'
|
||||
language: system
|
||||
- id: cspell
|
||||
name: Code spell checker (cspell)
|
||||
description: Run cspell to check for spelling errors (if available).
|
||||
entry: bash -c 'if command -v cspell >/dev/null 2>&1; then cspell --no-must-find-files -- "$@"; else echo "cspell not found, skipping spell check"; exit 0; fi' --
|
||||
pass_filenames: true
|
||||
language: system
|
||||
|
||||
ci:
|
||||
skip: [rust-linting, rust-clippy, cargo-lock-check, cspell]
|
||||
@@ -30,6 +30,10 @@ cargo build --release
|
||||
cargo test
|
||||
```
|
||||
|
||||
## Pre-commit hooks
|
||||
|
||||
This project uses [pre-commit](https://pre-commit.com); run `pre-commit install` to enable the git hooks.
|
||||
|
||||
## Known Issues
|
||||
|
||||
* Does not take `LANG`, etc., into account for handling file encodings (non-UTF8 matches are treated as binary)
|
||||
|
||||
+101
-261
@@ -4,285 +4,125 @@
|
||||
// file that was distributed with this source code.
|
||||
|
||||
use criterion::{Criterion, black_box, criterion_group, criterion_main};
|
||||
use uu_grep::matcher::Matcher;
|
||||
use uu_grep::{BinaryMode, ColorConfig, Config, DeviceMode, DirectoryMode, GlobSet, RegexMode};
|
||||
use std::ffi::OsString;
|
||||
use std::path::Path;
|
||||
|
||||
fn make_config<'a>(
|
||||
patterns: &'a [&'a str],
|
||||
regex_mode: RegexMode,
|
||||
ignore_case: bool,
|
||||
invert_match: bool,
|
||||
word_regexp: bool,
|
||||
) -> Config<'a> {
|
||||
Config {
|
||||
directory_mode: DirectoryMode::Read,
|
||||
device_mode: DeviceMode::Default,
|
||||
follow_symlinks: false,
|
||||
include_globs: GlobSet::new(),
|
||||
exclude_globs: GlobSet::new(),
|
||||
exclude_dir_globs: GlobSet::new(),
|
||||
label: "(standard input)",
|
||||
#[cfg(windows)]
|
||||
strip_cr: false,
|
||||
binary_mode: BinaryMode::Binary,
|
||||
max_count: None,
|
||||
before_context: 0,
|
||||
after_context: 0,
|
||||
has_context: false,
|
||||
patterns,
|
||||
regex_mode,
|
||||
ignore_case,
|
||||
invert_match,
|
||||
word_regexp,
|
||||
line_regexp: false,
|
||||
quiet: true,
|
||||
count: false,
|
||||
show_filename: false,
|
||||
files_with_matches: false,
|
||||
files_without_match: false,
|
||||
only_matching: false,
|
||||
byte_offset: false,
|
||||
line_number: false,
|
||||
initial_tab: false,
|
||||
null_separator: false,
|
||||
null_data: false,
|
||||
line_buffered: false,
|
||||
no_messages: true,
|
||||
group_separator: None,
|
||||
use_color: false,
|
||||
color_config: ColorConfig {
|
||||
matched_selected: "",
|
||||
matched_context: "",
|
||||
filename: "",
|
||||
line_number: "",
|
||||
byte_offset: "",
|
||||
separator: "",
|
||||
selected_line: "",
|
||||
context_line: "",
|
||||
reverse_video: false,
|
||||
no_erase: false,
|
||||
},
|
||||
}
|
||||
/// Run grep end-to-end through the real `uumain` entry point. `args` are the
|
||||
/// arguments after the program name (flags, pattern, paths). The exit status is
|
||||
/// ignored — we only care about the work performed.
|
||||
fn run(args: &[&str]) {
|
||||
let mut argv: Vec<OsString> = Vec::with_capacity(args.len() + 1);
|
||||
argv.push(OsString::from("grep"));
|
||||
argv.extend(args.iter().map(OsString::from));
|
||||
let _ = uu_grep::uumain(argv.into_iter());
|
||||
}
|
||||
|
||||
fn bench_compile(c: &mut Criterion) {
|
||||
let mut group = c.benchmark_group("compile");
|
||||
/// Build a multi-megabyte log-like corpus plus a directory holding it alongside
|
||||
/// a binary file. Every line contains `worker-<n>` and a `2024-…` timestamp; a
|
||||
/// rare `RAREHIT` marker appears on a handful of lines (≈ every 10000th).
|
||||
/// Returns `(dir, log_file)`.
|
||||
fn build_corpus() -> (std::path::PathBuf, std::path::PathBuf) {
|
||||
let mut content = String::new();
|
||||
for i in 0..80_000u32 {
|
||||
if i % 10_000 == 0 {
|
||||
content.push_str(&format!(
|
||||
"2024-01-15 10:30:{:02} RAREHIT worker-{i} special marker seen\n",
|
||||
i % 60
|
||||
));
|
||||
} else if i % 100 == 0 {
|
||||
content.push_str(&format!(
|
||||
"2024-01-15 10:30:{:02} ERROR worker-{i} connection reset\n",
|
||||
i % 60
|
||||
));
|
||||
} else {
|
||||
content.push_str(&format!(
|
||||
"2024-01-15 10:30:{:02} INFO worker-{i} request handled in {}ms\n",
|
||||
i % 60,
|
||||
i % 1000
|
||||
));
|
||||
}
|
||||
}
|
||||
assert!(content.len() > 4 * 1024 * 1024);
|
||||
|
||||
group.bench_function("fixed_string", |b| {
|
||||
b.iter(|| {
|
||||
let patterns: &[&str] = &["hello world"];
|
||||
let config = make_config(patterns, RegexMode::Fixed, false, false, false);
|
||||
let matcher = Matcher::compile(black_box(&config)).unwrap();
|
||||
let _ = black_box(&matcher);
|
||||
})
|
||||
});
|
||||
let dir = std::env::temp_dir().join(format!("uu_grep_bench_{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let log = dir.join("app.log");
|
||||
std::fs::write(&log, &content).unwrap();
|
||||
|
||||
group.bench_function("basic_regex", |b| {
|
||||
b.iter(|| {
|
||||
let patterns: &[&str] = &[r"[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}"];
|
||||
let config = make_config(patterns, RegexMode::Basic, false, false, false);
|
||||
let matcher = Matcher::compile(black_box(&config)).unwrap();
|
||||
let _ = black_box(&matcher);
|
||||
})
|
||||
});
|
||||
// A binary file (contains NUL) that also holds the marker, so `-I` has
|
||||
// something to skip while recursing.
|
||||
let mut binary = vec![0u8, 1, 2, 3];
|
||||
binary.extend_from_slice(b"RAREHIT in binary blob");
|
||||
binary.extend(std::iter::repeat_n(0u8, 4096));
|
||||
std::fs::write(dir.join("data.bin"), &binary).unwrap();
|
||||
|
||||
group.bench_function("extended_regex", |b| {
|
||||
b.iter(|| {
|
||||
let patterns: &[&str] = &[r"[0-9]{4}-[0-9]{2}-[0-9]{2}"];
|
||||
let config = make_config(patterns, RegexMode::Extended, false, false, false);
|
||||
let matcher = Matcher::compile(black_box(&config)).unwrap();
|
||||
let _ = black_box(&matcher);
|
||||
})
|
||||
});
|
||||
|
||||
group.bench_function("perl_regex", |b| {
|
||||
b.iter(|| {
|
||||
let patterns: &[&str] = &[r"\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2}"];
|
||||
let config = make_config(patterns, RegexMode::Perl, false, false, false);
|
||||
let matcher = Matcher::compile(black_box(&config)).unwrap();
|
||||
let _ = black_box(&matcher);
|
||||
})
|
||||
});
|
||||
|
||||
group.bench_function("multiple_patterns", |b| {
|
||||
b.iter(|| {
|
||||
let patterns: &[&str] = &["error", "warning", "critical", "fatal", "panic"];
|
||||
let config = make_config(patterns, RegexMode::Fixed, false, false, false);
|
||||
let matcher = Matcher::compile(black_box(&config)).unwrap();
|
||||
let _ = black_box(&matcher);
|
||||
})
|
||||
});
|
||||
|
||||
group.finish();
|
||||
(dir, log)
|
||||
}
|
||||
|
||||
fn bench_match(c: &mut Criterion) {
|
||||
let mut group = c.benchmark_group("match");
|
||||
fn bench_e2e(c: &mut Criterion) {
|
||||
let (dir, log) = build_corpus();
|
||||
let file = log.to_str().unwrap();
|
||||
let dir_str = dir.to_str().unwrap();
|
||||
|
||||
// Fixed string match - hit
|
||||
// Pure scanning throughput: `-q` with a pattern that never matches forces a
|
||||
// full scan and produces no output. A literal (which a buffer-at-a-time
|
||||
// searcher can accelerate) versus an extended-regex control (which cannot).
|
||||
{
|
||||
let patterns: &[&str] = &["ERROR"];
|
||||
let config = make_config(patterns, RegexMode::Fixed, false, false, false);
|
||||
let matcher = Matcher::compile(&config).unwrap();
|
||||
let line = b"2024-01-15 10:30:45 ERROR: Connection timeout on server-42";
|
||||
|
||||
group.bench_function("fixed_string_hit", |b| {
|
||||
b.iter(|| black_box(matcher.match_line(black_box(line))))
|
||||
let mut group = c.benchmark_group("scan");
|
||||
group.bench_function("literal_no_match", |b| {
|
||||
b.iter(|| run(black_box(&["-q", "NONEXISTENT_TOKEN_XYZ", file])))
|
||||
});
|
||||
group.bench_function("regex_no_match", |b| {
|
||||
b.iter(|| run(black_box(&["-q", "-E", "NON[0-9]EXISTENT_TOKEN", file])))
|
||||
});
|
||||
group.finish();
|
||||
}
|
||||
|
||||
// Fixed string match - miss
|
||||
// Real invocation shapes from the `grep` tldr page, each scanning the whole
|
||||
// corpus. The `RAREHIT` marker matches only a handful of lines, so output
|
||||
// stays small while the full-file scan dominates.
|
||||
{
|
||||
let patterns: &[&str] = &["CRITICAL"];
|
||||
let config = make_config(patterns, RegexMode::Fixed, false, false, false);
|
||||
let matcher = Matcher::compile(&config).unwrap();
|
||||
let line = b"2024-01-15 10:30:45 INFO: Server started successfully";
|
||||
let mut group = c.benchmark_group("usage");
|
||||
|
||||
group.bench_function("fixed_string_miss", |b| {
|
||||
b.iter(|| black_box(matcher.match_line(black_box(line))))
|
||||
// Search for a pattern within a file.
|
||||
group.bench_function("search_pattern", |b| {
|
||||
b.iter(|| run(black_box(&["RAREHIT", file])))
|
||||
});
|
||||
// Search for an exact string (-F).
|
||||
group.bench_function("fixed_string", |b| {
|
||||
b.iter(|| run(black_box(&["-F", "RAREHIT", file])))
|
||||
});
|
||||
// Recursive search ignoring binary files (-rI).
|
||||
group.bench_function("recursive_no_binary", |b| {
|
||||
b.iter(|| run(black_box(&["-rI", "RAREHIT", dir_str])))
|
||||
});
|
||||
// Print 3 lines of context (-C 3).
|
||||
group.bench_function("context", |b| {
|
||||
b.iter(|| run(black_box(&["-C", "3", "RAREHIT", file])))
|
||||
});
|
||||
// Filename + line number with forced color (-Hn --color=always).
|
||||
group.bench_function("filename_lineno_color", |b| {
|
||||
b.iter(|| run(black_box(&["-Hn", "--color=always", "RAREHIT", file])))
|
||||
});
|
||||
// Print only the matched text (-o).
|
||||
group.bench_function("only_matching", |b| {
|
||||
b.iter(|| run(black_box(&["-o", "RAREHIT", file])))
|
||||
});
|
||||
// Invert match (-v); `worker-` is on every line, so nothing is printed
|
||||
// and this measures the full inverted scan.
|
||||
group.bench_function("invert_match", |b| {
|
||||
b.iter(|| run(black_box(&["-v", "worker-", file])))
|
||||
});
|
||||
// Extended regex, case-insensitive (-Ei).
|
||||
group.bench_function("extended_icase", |b| {
|
||||
b.iter(|| run(black_box(&["-Ei", "rarehit", file])))
|
||||
});
|
||||
|
||||
group.finish();
|
||||
}
|
||||
|
||||
// Extended regex match
|
||||
{
|
||||
let patterns: &[&str] = &[r"[0-9]{4}-[0-9]{2}-[0-9]{2}"];
|
||||
let config = make_config(patterns, RegexMode::Extended, false, false, false);
|
||||
let matcher = Matcher::compile(&config).unwrap();
|
||||
let line = b"2024-01-15 10:30:45 ERROR: Connection timeout";
|
||||
|
||||
group.bench_function("extended_regex_hit", |b| {
|
||||
b.iter(|| black_box(matcher.match_line(black_box(line))))
|
||||
});
|
||||
}
|
||||
|
||||
// Case-insensitive match
|
||||
{
|
||||
let patterns: &[&str] = &["error"];
|
||||
let config = make_config(patterns, RegexMode::Fixed, true, false, false);
|
||||
let matcher = Matcher::compile(&config).unwrap();
|
||||
let line = b"2024-01-15 10:30:45 ERROR: Connection timeout";
|
||||
|
||||
group.bench_function("case_insensitive_hit", |b| {
|
||||
b.iter(|| black_box(matcher.match_line(black_box(line))))
|
||||
});
|
||||
}
|
||||
|
||||
// Inverted match
|
||||
{
|
||||
let patterns: &[&str] = &["ERROR"];
|
||||
let config = make_config(patterns, RegexMode::Fixed, false, true, false);
|
||||
let matcher = Matcher::compile(&config).unwrap();
|
||||
let line = b"2024-01-15 10:30:45 INFO: Server started successfully";
|
||||
|
||||
group.bench_function("inverted_match", |b| {
|
||||
b.iter(|| black_box(matcher.match_line(black_box(line))))
|
||||
});
|
||||
}
|
||||
|
||||
// Word boundary match
|
||||
{
|
||||
let patterns: &[&str] = &["error"];
|
||||
let config = make_config(patterns, RegexMode::Fixed, true, false, true);
|
||||
let matcher = Matcher::compile(&config).unwrap();
|
||||
let line = b"2024-01-15 10:30:45 error: Connection timeout";
|
||||
|
||||
group.bench_function("word_boundary_hit", |b| {
|
||||
b.iter(|| black_box(matcher.match_line(black_box(line))))
|
||||
});
|
||||
}
|
||||
|
||||
// Multiple patterns
|
||||
{
|
||||
let patterns: &[&str] = &["error", "warning", "critical", "fatal", "panic"];
|
||||
let config = make_config(patterns, RegexMode::Fixed, true, false, false);
|
||||
let matcher = Matcher::compile(&config).unwrap();
|
||||
let line = b"2024-01-15 10:30:45 WARNING: High memory usage detected on node-7";
|
||||
|
||||
group.bench_function("multi_pattern_hit", |b| {
|
||||
b.iter(|| black_box(matcher.match_line(black_box(line))))
|
||||
});
|
||||
}
|
||||
|
||||
// Long line
|
||||
{
|
||||
let patterns: &[&str] = &["needle"];
|
||||
let config = make_config(patterns, RegexMode::Fixed, false, false, false);
|
||||
let matcher = Matcher::compile(&config).unwrap();
|
||||
let mut long_line = "a".repeat(5000);
|
||||
long_line.push_str("needle");
|
||||
long_line.push_str(&"b".repeat(5000));
|
||||
let long_line_bytes = long_line.into_bytes();
|
||||
|
||||
group.bench_function("long_line_hit", |b| {
|
||||
b.iter(|| black_box(matcher.match_line(black_box(&long_line_bytes))))
|
||||
});
|
||||
}
|
||||
|
||||
group.finish();
|
||||
let _ = std::fs::remove_dir_all(Path::new(dir_str));
|
||||
}
|
||||
|
||||
fn bench_throughput(c: &mut Criterion) {
|
||||
let mut group = c.benchmark_group("throughput");
|
||||
|
||||
// Simulate processing many lines (like searching a log file)
|
||||
let lines: Vec<Vec<u8>> = (0..1000)
|
||||
.map(|i| {
|
||||
if i % 50 == 0 {
|
||||
format!(
|
||||
"2024-01-15 10:30:{:02} ERROR: Connection timeout on server-{}",
|
||||
i % 60,
|
||||
i
|
||||
)
|
||||
.into_bytes()
|
||||
} else {
|
||||
format!(
|
||||
"2024-01-15 10:30:{:02} INFO: Request processed in {}ms",
|
||||
i % 60,
|
||||
i * 3
|
||||
)
|
||||
.into_bytes()
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
{
|
||||
let patterns: &[&str] = &["ERROR"];
|
||||
let config = make_config(patterns, RegexMode::Fixed, false, false, false);
|
||||
let matcher = Matcher::compile(&config).unwrap();
|
||||
|
||||
group.bench_function("scan_1000_lines_fixed", |b| {
|
||||
b.iter(|| {
|
||||
let mut matches = 0u64;
|
||||
for line in &lines {
|
||||
if matcher.match_line(black_box(line)).is_some() {
|
||||
matches += 1;
|
||||
}
|
||||
}
|
||||
black_box(matches)
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
let patterns: &[&str] = &[r"[0-9]+ *ms"];
|
||||
let config = make_config(patterns, RegexMode::Extended, false, false, false);
|
||||
let matcher = Matcher::compile(&config).unwrap();
|
||||
|
||||
group.bench_function("scan_1000_lines_regex", |b| {
|
||||
b.iter(|| {
|
||||
let mut matches = 0u64;
|
||||
for line in &lines {
|
||||
if matcher.match_line(black_box(line)).is_some() {
|
||||
matches += 1;
|
||||
}
|
||||
}
|
||||
black_box(matches)
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
group.finish();
|
||||
}
|
||||
|
||||
criterion_group!(benches, bench_compile, bench_match, bench_throughput);
|
||||
criterion_group!(benches, bench_e2e);
|
||||
criterion_main!(benches);
|
||||
|
||||
@@ -864,6 +864,12 @@ fn expand_num_shorthand(args: impl Iterator<Item = OsString>) -> Vec<OsString> {
|
||||
out
|
||||
}
|
||||
|
||||
impl Default for GlobSet {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl GlobSet {
|
||||
/// Create an empty GlobSet.
|
||||
pub fn new() -> Self {
|
||||
|
||||
Reference in New Issue
Block a user