Skip to content

perf(jq): reuse compiled filters within an execution - #2966

Merged
chaliy merged 1 commit into
mainfrom
perf/jq-filter-cache
Nov 22, 2026
Merged

chaliy merged 1 commit into
mainfrom
perf/jq-filter-cache

Conversation

@chaliy

@chaliy chaliy commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

What changed

Bashkit reuses up to four compiled inline jq filters within a single execution. The least recently used filter is evicted when the cache fills. The cache key includes the source and ordered binding names. Each invocation evaluates with its own input, bindings, environment, and limits. Bashkit also parses the fixed jaq and compatibility definitions once per process.

Why

The original fan-out profile attributed about half of sampled CPU time to jq filter parsing and compilation. The repeated and interleaved workloads capture reuse, and the distinct-filter workload measures compilation on cache misses.

Before / After

Workload Before After
Interleaved filters, one-entry cache to four-entry LRU 48.32 ms 4.53 ms
Distinct filters, LRU alone to LRU plus parsed compatibility definitions 49.47 ms 37.09 ms
Existing 100-warehouse fan-out, original interpreter to initial cache build 107.88 ms 9.48 ms
Existing fan-out, LRU alone to final build 9.09 ms 8.78 ms

The fan-out runs verified exact output, stderr, and exit status. Both isolated reruns of the 4,096-row aggregate favored the pre-parsed build after an initial slowdown.

Risk

  • Medium. Filter reuse must preserve variable slots, name resolution, diagnostics, resource limits, and session isolation.
  • Successful inline plans have a four-entry cap, with limits on retained source and binding names. The execution scope drops the cache on completion or cancellation. File filters are read and compiled on each call.

Checklist

  • Tests added or updated
  • Backward compatibility considered
  • 395 jq-related tests and targeted Clippy passed, with jq 1.8.1 as the differential oracle

Get evidence
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
bashkit d16a5cd Commit Preview URL

Branch Preview URL
Nov 22 2026, 02:01 AM

@bashkit-pr-reviewer

Copy link
Copy Markdown

Review verdict: approve-worthy, no findings

This is a clean, well-scoped perf change. jq filter compilation (parse + resolve + compile) previously ran on every invocation; now small inline filters are compiled once per Bash::exec and reused, with a bounded per-execution LRU cache plus a process-wide OnceLock for the immutable library/compat prelude parse.

Correctness of the cache key (verified): compilation depends only on the expanded source (loc/altpat are deterministic transforms of it) and the ordered global binding names (slot assignment order). Everything else that varies per call — binding values, ENV/$ENV, filename/lineno, $ARGS, inputs, and evaluation limits — is supplied fresh at evaluation time in mod.rs. The key correctly includes ordered user binding names (all_var_bindings is CLI/file order, deterministic; order-sensitive equality is required since slot order follows it). -f file filters are explicitly excluded from caching, so re-read-per-call semantics are preserved (pinned by the new jq_filter_file_is_read_again_on_each_call test).

Concurrency and bounds (verified): the mutex covers only lookup/replacement; compilation happens outside the lock, and concurrent duplicate misses collapse to a single slot via the dedupe in insert. Hit moves the entry to MRU-back, insertion evicts front — a correct 4-entry LRU. The accepts() gate (≤1 KiB source, ≤16 names totaling ≤1 KiB) bounds per-entry retention, and the cache lives in execution extensions so it drops with the request — no cross-request state except the trusted immutable prelude defs. The new jq_compiled_filter_uses_current_values_and_input test pins that cached plans don't go stale across --arg/env/input changes.

What I read beyond the diff: filter.rs (cache + compile), mod.rs run path (key construction, per-call value supply), compat.rs, args.rs binding order, lib.rs/builtins/mod.rs extension plumbing, the new benches/jq.rs (fresh Bash per iteration with 64-call scripts correctly exercises within-execution hits vs. one-off compiles), knowledge/foundations/jq.md (the new "Filter compilation" section matches the code — including the once-per-process prelude claim, which I verified against the PRELUDE OnceLock), and AGENTS.md. The committed benches/results/criterion-jq-*.md files follow existing repo convention.

No inline findings; no review submitted per workflow. I could not build or run tests here (no cargo in this shell), so this is reasoning-from-code only.

@chaliy
chaliy merged commit a372b69 into main Nov 22, 2026
68 checks passed
@chaliy
chaliy deleted the perf/jq-filter-cache branch November 22, 2026 02:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant