Optimize server-mode scan performance (compiled-rule cache, shared compilation, parallel parsing)#218
Conversation
…mpilation, parallel parsing)
This comment has been minimized.
This comment has been minimized.
whitemerch
left a comment
There was a problem hiding this comment.
Changes are sound. One nit: the --x-use-rules-cache and --x-disable-rule-isolation are two independent booleans with no guard pairing them, maybe we should pair them somehow
|
@whitemerch thank you! I ve combined |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: bed18f0257
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
…SEC-2670/server-perf
Motivation
In server mode (
serve), one process handles many/analyzerequests, but today every request rebuilds the full scan pipeline from scratch, it recompiles all rules, re-parses libraries, and discards the result. Warm scans are therefore as slow as cold ones, dominated by redundant per-request Rego compilation rather than the work that actually changed.This PR makes repeated server-mode scans fast by eliminating that redundant work, behind opt-in experimental flags. The default path and the CLI are unchanged.
JIRA Ticket K9CODESEC-2670
Changes
Three independent, opt-in server-mode optimizations plus a correctness fix:
--x-use-rules-cache): process-global cache of compiled OPA queries, keyed by rule identity + library hash + base-store data hash. Warm scans reuse compiled rules and skip recompilation. Off by default; never enabled for one-shot CLI scans (pure memory cost there). Cache keys usexxhash(allocation-free on the hot path).--x-disable-rule-isolation): co-compile all rules + libraries once into a single compiler (rules rewritten to unique packages) instead of isolating each rule, so the shared library AST is retained once rather than copied into every compiled query. Rules that fail to parse/compile fall back to isolated compilation, preserving correctness. UsesWithCapabilities(not the deprecatedWithUnsafeBuiltins) to keephttp.send/opa.runtimeblocked.--x-parallelparsing): fan the per-file parse across CPUs in the in-memory source provider. Reuses the existing provider worker-count policy.Resourcesslice so merged module input data is byte-stable across scans. Required for the compiled-rule cache to hit when modules are present, and a reproducibility fix in its own right.--x-use-rules-cache--x-disable-rule-isolation--x-parallelparsing--x-use-rules-cache --x-disable-rule-isolationReading the table:
Bottom line (all flags on): warm scans go from 1.96 s → 0.65 s (−67%) and CPU from 17 s → 1.7 s (−90%), while live heap stays at ~53 MB — only ~20 MB above the no-cache baseline and ~11× below caching-with-isolation. Disabling rule isolation is the lever that makes the cache affordable.
Author Checklist
QA Instruction
All flags are off by default; the default scan path and CLI are unchanged, so existing behavior should be byte-identical.
To verify the optimizations in server mode:
make build../bin/datadog-iac-scanner serve --port 8000 --enable-shutdown(optionally with--x-use-rules-cache --x-disable-rule-isolation --x-parallelparsing)./ide/v1/iac/analyzetwice and confirm:--x-use-rules-cache, the second (warm) request is dramatically faster than the first.--x-disable-rule-isolation, the server logsRule isolation disabled: N/N queries served from shared compiler.Blast Radius
Datadog IaC Scanner only. The changes are gated behind experimental, hidden flags that default off; with no flags set, the engine, CLI, and existing server behavior are unchanged. The one always-on change is sorting the Terraform module
Resourcesslice, it makes module input data deterministic (correctness/repro improvement) and does not change findings.Additional Notes
-x-use-rules-cacheyou also must use--x-disable-rule-isolationIf you need to share anything else along with your PR, please do it here.
I submit this contribution under the Apache-2.0 license.