From a8a89b3ed3101981d4d87ce50f924ea499c369fd Mon Sep 17 00:00:00 2001 From: Onur Gündüz Date: Tue, 29 Sep 2026 14:15:13 +0200 Subject: Optimize release throughput and add reproducible CLI benchmarks --- Cargo.toml | 2 +- README.md | 19 ++++++++++++++ bench/benchmark.py | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 bench/benchmark.py diff --git a/Cargo.toml b/Cargo.toml index 6e2d5e4..ed2ec34 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ regex = "1" serde_json = "1" [profile.release] -opt-level = "z" +opt-level = 3 lto = true codegen-units = 1 panic = "abort" diff --git a/README.md b/README.md index 056f76c..f0d487d 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,25 @@ echo '{"user": {"settings": {"theme": "dark"}}}' | \ aip-filter "user.settings.theme = dark" ``` +## Performance + +Release builds optimize for speed rather than minimum executable size. The evaluator reuses compiled regexes and literals, borrows JSON collections, and searches overlapping phrases in linear time. JSON parsing still allocates memory for each record. + +To compare two release binaries, use Python 3: + +```bash +python3 bench/benchmark.py ./target/baseline ./target/release/aip-filter \ + --records 100000 --runs 5 +``` + +The benchmark generates JSON lines under `target/benchmarks`, checks that both binaries produce identical output, and reports median elapsed times for seven workloads. It alternates execution order and discards output during timing. Results depend on the machine and data. + +For a smaller binary, Cargo also supports a size-oriented build without changing the configuration: + +```bash +CARGO_PROFILE_RELEASE_OPT_LEVEL=z cargo build --release --locked +``` + ## API Implement `Filterable` for your type, compile the expression once with `Expr::compile`, and reuse `CompiledFilter::evaluate`. Compilation returns errors for unsupported expressions before any records are read. `serde_json::Value` implements `Filterable` directly and borrows arrays and objects without copying them. diff --git a/bench/benchmark.py b/bench/benchmark.py new file mode 100644 index 0000000..4fe208d --- /dev/null +++ b/bench/benchmark.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python3 +"""Compare CLI throughput and verify identical output on generated JSON lines.""" + +import argparse +import hashlib +import json +from pathlib import Path +import statistics +import subprocess +import time + +ROOT = Path(__file__).resolve().parent.parent +CASES = { + "bare": "needle", + "equality": "state = open", + "numeric": "priority > 50 AND score < 3.5", + "nested": "user.settings.theme = dark", + "list": "tags:beta", + "regex": 'msg =~ "[a-z]+ needle[0-9]+$"', + "regex_group": 'msg =~ ("[a-z]+ needle[0-9]+$" OR "^skip")', +} + + +def digest(binary, expression, data): + result = subprocess.run( + [str(binary), expression, str(data)], capture_output=True, check=True + ) + return hashlib.sha256(result.stdout).hexdigest() + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("baseline", type=Path) + parser.add_argument("candidate", type=Path) + parser.add_argument("--records", type=int, default=100_000) + parser.add_argument("--runs", type=int, default=5) + args = parser.parse_args() + if args.records <= 0 or args.runs <= 0: + parser.error("records and runs must be positive") + binaries = [args.baseline.resolve(), args.candidate.resolve()] + data = ROOT / "target" / "benchmarks" / "records.jsonl" + data.parent.mkdir(parents=True, exist_ok=True) + with data.open("w") as output: + for i in range(args.records): + record = { + "id": i, + "msg": f"hello needle{i}" if i % 5 == 0 else "skip this record", + "state": "open" if i % 2 else "closed", + "priority": i % 100, + "score": (i % 10) / 2, + "user": {"settings": {"theme": "dark" if i % 3 else "light"}}, + "tags": ["stable", "beta" if i % 4 else "alpha"], + } + output.write(json.dumps(record, separators=(",", ":")) + "\n") + print(f"{args.records:,} records; {data.stat().st_size:,} bytes; median of {args.runs} runs") + print("case baseline(s) candidate(s) speedup") + for label, expression in CASES.items(): + assert digest(binaries[0], expression, data) == digest(binaries[1], expression, data), label + samples = [[], []] + for run in range(args.runs): + for index in ([0, 1] if run % 2 == 0 else [1, 0]): + start = time.perf_counter() + subprocess.run( + [str(binaries[index]), expression, str(data)], + stdout=subprocess.DEVNULL, + stderr=subprocess.PIPE, + check=True, + ) + samples[index].append(time.perf_counter() - start) + before, after = (statistics.median(sample) for sample in samples) + print(f"{label:13} {before:11.4f} {after:12.4f} {before / after:6.2f}x") + for binary in binaries: + print(f"{binary.name}: {binary.stat().st_size:,} bytes") + + +if __name__ == "__main__": + main() -- cgit v1.3.1