Skip to content

Performance: reuse immutable node and internal-edge views across rule checks #101

Done

Description

@TristanKruse

Problem and priority

Suggested priority: P1. Graph caching is effective, but a warm graph is not a cheap warm rule.

A 1.1.0 diagnostic on 10,000 sparse constant-reference sources retrieved the same cached graph in ~4.2ms. Twenty repetitions of one nonempty, passing boundary rule still took 4.44s and allocated ~7,006,461 objects (~222ms per check). This is one diagnostic, not a representative 20-distinct-rule suite or a universal regression threshold.

Inspected repeated work

DependOnFileCondition#perform_check obtains the cached graph, then:

  1. FileRuleSupport.selected_nodes calls project_to_nodes, rebuilding incoming/outgoing collections, ordering labels and constructing ProjectedNodes.
  2. project_edges(per_internal_edge) rebuilds mapping/grouping structures and ProjectedEdges.
  3. The assertion filters those structures for the specific rule.

Related source: node projection, edge projection.

Some rule work is necessary; rebuilding graph-invariant views is the candidate to remove. Rubyzen's reusable parsed project and ArchSpec's evaluator illustrate why whole-suite reuse matters, but do not establish equal analysis semantics.

Reproduction and measurement

Extend benchmark/study from #92 rather than add a parallel benchmark system:

  • Prebuild one graph with imports_and_constants.
  • Execute one, twenty identical, and twenty genuinely different file/layer/slice policies.
  • Include passing, forbidden-dependency and empty-selector controls.
  • Measure cold analysis, cold-plus-whole-suite, warm-per-rule, allocations and retained/peak memory separately.
  • If comparing competitors, reuse their project/model too; do not compare our warm model with another tool's fresh CLI for each rule.

Design to evaluate

Prefer analysis-owned immutable node/internal-edge indexes or a private analysis-session view bundle. Keep common projections/assertions pure; orchestration may reuse their immutable outputs.

Evaluate eager core views versus lazy bounded storage. Eagerly calculating every optional projection could worsen single-rule startup/memory. Do not memoize arbitrary custom mapper closures: they can be stateful, lack stable identity or depend on call-specific options.

If a private identity-based cache is chosen, specify concurrency, lifetime and eviction. It must not retain every past graph/root indefinitely or expose mutable arrays. A process-global unbounded query cache is not the default answer.

No new public fluent sentence should be necessary: existing project_files(...).in_path(...).should_not.depend_on_files...check remains unchanged.

Acceptance criteria

  • Exact structured violations/evidence, ordering, both moods and the empty-test guard match the current implementation.
  • Self-edge node presence and internal/external projection differences remain correct.
  • clear_cache, global clearing, edited-source rebuilds, different roots/modes/exclusions and CLI command resets never reuse stale views.
  • Custom projections, filters and logging still work without hidden filesystem work in pure projections/assertions.
  • Stable views are shared within one immutable analysis; selective policies still apply their own filters correctly.
  • Repeated checks show a substantial repeatable allocation reduction and useful whole-suite improvement, not merely a faster isolated hash lookup.
  • Cold single-rule work, memory and thread/fiber isolation have explicit regression checks.
  • Baseline identity and JSON/report contracts remain unchanged.

Related: #11, #46, #92 and #100. Content-validated watch sessions/persistent caches are separate lifecycle decisions; do not silently change the documented snapshot contract.

Activity

  1. added
    enhancement
    on Nov 23, 2026
  2. TristanKruse commented on Nov 24, 2026

    @TristanKruse
    CollaboratorAuthor

    Continuing after #100 merged in #107. This is the next P1 item in the reviewed order: #100 → #101 → #102 → #106 → #104 → #103 → #105. All are feasible; #103/#105 need explicit bounded semantic designs, not guesses about runtime Ruby behavior.

    For #101, attach three lazy immutable projection slots to the existing cached analysis entry, rather than a new graph-keyed global query cache. Share nodes/internal/external edges across file/layer checks, and leave custom closures, selectors, assertions, logging and cycle enumeration per check. Existing explicit source-snapshot invalidation remains unchanged.

    Regression tests now cover exact structured results/evidence/order, snapshot invalidation and GC lifetime, concurrency, modes/exclusions, callbacks and logging. Paired baseline/candidate studies use merged #100 as baseline and identical benchmark/study harnesses, with graph-only/single/repeated/varied suites, forbidden/empty controls, allocation counts and retained-memory tradeoffs. A separate PR is being prepared; no release is being published.

  3. TristanKruse commented on Nov 25, 2026

    @TristanKruse
    CollaboratorAuthor

    Implemented in #108, pending maintainer review/merge (not released).

    • Three lazy immutable views per existing analysis entry; explicit/global/CLI invalidation, callbacks, custom/slice projections, logging and exact ordered structured violations/evidence are covered by regressions.
    • Final CI: 945 examples, zero failures across Ruby 3.3/3.4 Linux and 4.0 Windows; 98.89% line / 92.68% branch coverage. Security, package/install acceptance, RAG fixture, four real-world canaries, docs and research study checks pass.
    • Three alternating baseline/candidate pairs per workload/platform at 10,000 sources pass in run 45446554405. All 48 raw samples and medians are retained in the PR.
    • Twenty varied warm rules: ~48% faster on Linux / ~43% on Windows, ~42% fewer allocations. Twenty identical checks: ~70% / ~66% faster, ~72% fewer allocations. Cold varied suites improve ~18% / ~15%.
    • Explicit tradeoff: ~3.17 MiB extra retained Ruby heap for indexed 10,000-source analyses; peak RSS is not uniformly lower. Cold single-rule allocations remain essentially unchanged; time varies ~+1.3% Linux / -0.9% Windows in these three-trial samples.

    The existing snapshot lifetime is unchanged; no new unbounded query cache or custom-closure memoization. #102 remains next in the reviewed priority order, followed by #106, #104, #103, #105 (the latter semantic capabilities need bounded design work first).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions