Skip to content

perf(rules): pre-filter walk by fixed glob filename - #203

Open
raysubham wants to merge 1 commit into
step-security:mainfrom
raysubham:fix/rule-scan-filename-prefilter
Open

perf(rules): pre-filter walk by fixed glob filename#203
raysubham wants to merge 1 commit into
step-security:mainfrom
raysubham:fix/rule-scan-filename-prefilter

Conversation

@raysubham

Copy link
Copy Markdown
Contributor

Summary

The malicious-file scanner ran every relative glob regex against every regular file walked (47 full-path checks per file on the current bundle). Most files can never match because their filename matches no rule.

  • walkRoots builds a filename index from relative globs whose final path segment is fixed.
  • walkOneRoot looks up d.Name() before filepath.Rel and any regex; no candidates → skip. Candidates still go through the unchanged regex + evaluate.
  • Any glob with */? in its final segment disables the index and falls back to the original flat matcher loop, preserving matcher order and matched_glob selection.
  • Absolute globs, evaluate, limits, telemetry shape unchanged.

Reference measurement (200,000 non-matching files, 2,001 dirs, depth 10, Ubuntu VM): 33.98s → 0.21s, model.RuleScan JSON byte-for-byte identical.

Tests

  • TestScan_WildcardFilenameGlob: wildcard filename glob still matches, and first glob wins matched_glob when a wildcard and fixed glob overlap (fallback path).
  • Decoy in TestScanRegexMatch: same filename in wrong directory is rejected by the full-path check (indexed path).

go test ./internal/detector/rules/, go vet, golangci-lint (0 issues), gofmt, and CGO_ENABLED=0 GOOS=linux build all pass.

Non-goals

No concurrency, caching, directory exclusion, schema, API, or limit changes. Benchmark / integration workflow deferred.

walkRoots indexes relative globs by their fixed final path component and
walkOneRoot skips files whose name has no candidate before computing the
relative path or running any regex. Any glob with a wildcard in its final
segment disables the index and keeps the original flat matcher loop, so
matcher order and matched_glob selection are unchanged.

Absolute globs, evaluate, limits, and reporting are untouched.

On a 200k-file Ubuntu corpus the walk dropped from 33.98s to 0.21s with
byte-for-byte identical RuleScan output.
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