Skip to content

perf(rules): build the per-rule profile line only when profiling #274 - #925

Merged
nc9 merged 1 commit into
mainfrom
perf/274-rule-profile-payload
Oct 17, 2026
Merged

nc9 merged 1 commit into
mainfrom
perf/274-rule-profile-payload

Conversation

@nc9

@nc9 nc9 commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

The rule runner now builds its per-rule [rule-profile] payload only when SQUIRREL_RULE_PROFILE is set. Before, finalize in runOneRule ran three filter passes, built a nine-key object and called performance.now() on every rule invocation, and the logger then threw the result away.

Closes #274

Measurement

The corpus is real: a 300-page offline crawl of docs.squirrelscan.com. This box's gymshark crawl takes more than 10 minutes per pass, too long to repeat. The 300-page pass makes 53,450 rule invocations with template fan-out on, counted from the profile lines.

The bench is apps/cli/scripts/template-fanout-bench.ts --child --arm on, which times the streamed page-rule pass with no network. Each run is its own process. The arms were interleaved (after, before, before, after, after, before) and the load average was 1.4 to 2.5.

Arm CPU ms Median CPU Wall ms Median wall
before (main) 38,341 / 38,966 / 38,491 38,491 35,329 / 36,023 / 35,384 35,384
after 38,615 / 38,931 / 39,435 38,931 35,430 / 35,978 / 36,700 35,978

No measurable difference. Runs of the same arm spread by about 1.1 s. Measured alone, the removed work costs about 5 ms for 53,450 calls, about 0.01% of the pass. The change is still worth having: it removes an allocation and three array passes per rule run that nothing reads.

Acceptance criteria

  • The payload and its three filter passes are not evaluated when SQUIRREL_RULE_PROFILE is unset. tests/rule-profile-274.test.ts runs a child process with a probe rule whose checks count filter calls: 0 without the variable (3 on main), 3 with it.
  • SQUIRREL_RULE_PROFILE=1 still prints the same [rule-profile] lines with the same fields, for page rules (pageUrl) and site rules (scope: "site"). The same test checks the keys.
  • Measured on a real corpus before and after; the result is in the table above.

Checks

  • Rules typecheck is clean.
  • The new test passes.
  • No docs change: SQUIRREL_RULE_PROFILE is a developer profiling variable and the docs do not mention it.

Skill impact

None.

@nc9 nc9 added the performance Latency, memory, query cost or size label Oct 17, 2026
@claude

claude Bot commented Oct 17, 2026

Copy link
Copy Markdown

Review of #925: the change is small and correct.

I found no blocking issues. profileRules is exported from logger.ts and read once at module load. The new early return in finalize skips only the profile payload, and ruleStart is used only inside that payload. The [rule-profile] output is unchanged when profiling is on. collected-signals.ts already uses the same profileRules guard, so this matches the existing pattern.

  • (low) packages/rules/src/logger.ts:3 The header says the CLI injects a richer logger at runtime. I found no injection point in the repo, so I couldn't rule it out. If the CLI does swap in a logger that handles debug("rule", …) independently of SQUIRREL_RULE_PROFILE, this change would silently drop that data. It is worth a quick confirmation.
  • (nit) The test spawns Bun subprocesses with a 15 s kill timer. This is reasonable because the flag is read at module load. Clearing the timer in a finally would avoid a stray kill if the spawn throws.
  • (nit) The CHANGELOG entry is more detailed than the change warrants, since the PR itself says the saving is unmeasurable. A one-line entry would do.

Test coverage is good. The tests cover both the profiled and unprofiled paths, and for page and site rules.

I haven't run the tests or CI myself.

runOneRule's finalize built the [rule-profile] payload (three filters over
the checks, a nine-key object, a performance.now()) on every rule
invocation, and the logger dropped it unless SQUIRREL_RULE_PROFILE was set.
It is now built only when profiling.

Measured on a real corpus, a 300-page crawl of docs.squirrelscan.com
(53,450 rule invocations with template fan-out on). The streamed page-rule
pass ran 3 interleaved child processes per arm. Median CPU was 38,491 ms
before and 38,931 ms after; median wall was 35,384 ms before and 35,978 ms
after. That is no measurable difference: the spread between runs is about
1.1 s. In isolation the removed work is about 5 ms for 53,450 calls.

Closes #274

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the perf/274-rule-profile-payload branch from c5d16a9 to fb02adb Compare October 17, 2026 18:01
@claude

claude Bot commented Oct 17, 2026

Copy link
Copy Markdown

Review of #925: I found no blocking issues.

The change is correct. profileRules is read once at module load. The timer and the three filters plus the payload are now skipped unless profiling is on, and ruleCheckCount still feeds withTrace. Nothing else in the package reads the rule debug line, so the shim is the only consumer.

  • (nit) packages/rules/src/runner.ts: the comment in finalize says the logger "drops this line". If the CLI later injects a real logger that honors debug('rule'), those lines would now be silently gone unless SQUIRREL_RULE_PROFILE is set. A short note that the env var is the single switch would help the next person.
  • (nit) rule-profile-274.test.ts: it spawns two Bun subprocesses, which adds CI time. That is acceptable because the flag is read at load. The 15s kill timer inside a 30s test timeout is fine.
  • (low) The CHANGELOG says the difference is not measurable (about 5 ms against 38 s). That is honest. It does mean this is a tidy-up and not a perf fix, so the test and fixture are a lot of machinery for it. I'm fine with keeping them.

Test coverage is good. It checks the filter-count probe and the field shape for both page and site rules.

🤖 Generated with Claude Code

@nc9

nc9 commented Oct 17, 2026

Copy link
Copy Markdown
Contributor Author

Triage: nits only. The env var is the single switch because logger.debug in the rules shim prints the rule line only when profileRules is set, and nothing injects another logger into the rules package. The guard follows that switch exactly. Merging.

@nc9
nc9 merged commit 1c4d948 into main Oct 17, 2026
15 checks passed
@nc9
nc9 deleted the perf/274-rule-profile-payload branch October 17, 2026 18:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Latency, memory, query cost or size

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The rule runner builds a profile payload for every rule invocation, whether or not profiling is on

1 participant