diff --git a/.github/workflows/performance-study.yml b/.github/workflows/performance-study.yml index 9ce2344..8562f28 100644 --- a/.github/workflows/performance-study.yml +++ b/.github/workflows/performance-study.yml @@ -86,3 +86,69 @@ jobs: archunit/benchmark/study/Gemfile.lock if-no-files-found: warn retention-days: 14 + + discovery: + if: github.event_name == 'workflow_dispatch' + name: Paired discovery — ${{ matrix.os }} + runs-on: ${{ matrix.os }} + timeout-minutes: 20 + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, windows-latest] + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 + with: + path: candidate + persist-credentials: false + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 + with: + ref: v1.1.0 + path: baseline + persist-credentials: false + - uses: ruby/setup-ruby@be3c7df29b3349ec6bcfc6b4792f1cf9f5237d18 # v1 + with: + ruby-version: '4.0' + bundler-cache: true + working-directory: candidate + - name: Run alternating fresh-process pairs with the existing extraction benchmark + shell: pwsh + run: | + $ErrorActionPreference = 'Stop' + $candidate = Join-Path $env:GITHUB_WORKSPACE 'candidate' + $baseline = Join-Path $env:GITHUB_WORKSPACE 'baseline' + $reports = Join-Path $env:GITHUB_WORKSPACE 'reports' + New-Item -ItemType Directory -Path $reports -Force | Out-Null + Copy-Item -LiteralPath (Join-Path $candidate 'benchmark/extraction.rb') -Destination (Join-Path $baseline 'benchmark/extraction.rb') + $env:BUNDLE_PATH = Join-Path $candidate 'vendor/bundle' + $cases = @( + @{name='sparse-5000'; files=5000; imports=0; excluded=0}, + @{name='sparse-10000'; files=10000; imports=0; excluded=0}, + @{name='imports-1000'; files=1000; imports=3; excluded=0}, + @{name='excluded-1000'; files=1000; imports=0; excluded=3000} + ) + foreach ($case in $cases) { + foreach ($trial in 1..3) { + $results = @{} + foreach ($side in 'baseline','candidate') { + $checkout = if ($side -eq 'baseline') { $baseline } else { $candidate } + $env:BUNDLE_GEMFILE = Join-Path $checkout 'Gemfile' + $output = Join-Path $reports "$($case.name)-$trial-$side.json" + bundle exec ruby (Join-Path $checkout 'benchmark/extraction.rb') --files $case.files --gems 2 --imports-per-file $case.imports --excluded-files $case.excluded --constants --output $output | Out-Null + if ($LASTEXITCODE -ne 0) { throw "Failed $($case.name) / $side" } + $results[$side] = Get-Content -LiteralPath $output -Raw | ConvertFrom-Json + if (-not $results[$side].graph_cache_reused) { throw 'Graph cache was not reused' } + if ($results[$side].cold.counters.source_files -ne ($case.files + 1)) { throw 'Wrong source inventory' } + Write-Output "$($case.name) trial=$trial side=$side cold=$($results[$side].cold.stage_seconds.total) discovery=$($results[$side].cold.stage_seconds.source_enumeration) allocated=$($results[$side].cold.memory_growth.allocated_objects)" + } + if ($results.baseline.graph_sha256 -ne $results.candidate.graph_sha256) { throw 'Graph or retained evidence changed' } + } + } + - name: Retain raw paired measurements + if: always() + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7 + with: + name: discovery-${{ matrix.os }}-${{ github.run_id }} + path: reports/*.json + if-no-files-found: error + retention-days: 14 diff --git a/API.md b/API.md index 029207f..e2c931b 100644 --- a/API.md +++ b/API.md @@ -75,7 +75,12 @@ Without a locator, ArchUnitRuby searches upward for a `Gemfile` or gemspec. Discovery skips symlinked source files and directories, including aliases inside the project root. Direct `FileInfo` extraction validates canonical as well as lexical containment before reading. -The source walker reuses a single file-status result per entry for type and symlink decisions. +The source walker owns a pruned iterative traversal and reuses one status per entry. Identifiers +come from directory child components under the canonical root, not unchecked absolute-prefix +slicing; the final source list is sorted once. Included entry/listing failures raise +`TechnicalError`, including unreadable or disappearing directories; they are not silently skipped. +This stricter listing-error handling is part of unreleased 1.1.1. Excluded directories are pruned +before their children are listed. Concurrent hostile filesystem mutation is not sandboxed. The detected root's `.archignore` is applied centrally to graphs and file-based metrics. It accepts blank lines, full-line `#` comments, basename globs, root-relative path globs, leading `/` anchors, diff --git a/CHANGELOG.md b/CHANGELOG.md index 103bb4d..94e6198 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ # Changelog +## 1.1.1 (unreleased) + +- Reuse one source-entry status, compile exclusion matching once, and build relative identifiers + from directory children in a pruned iterative source walker, preserving sorted graph inputs. +- Fail closed on unreadable/disappearing included directory listings instead of inheriting Find's + silent listing-error skip. Excluded trees are still pruned without opening their descendants. +- Extend the existing extraction benchmark with allocation counts, full graph/evidence digests, + and excluded-tree workloads; retain generous CI timing ceilings. + ## 1.1.0 - 2026-11-23 Additive release over 1.0.0. Import-only analysis remains the default; the CLI and Rails helpers diff --git a/Gemfile.lock b/Gemfile.lock index cdd35e9..80924a9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - archunit (1.1.0) + archunit (1.1.1) csv (>= 3.3, < 4.0) json (>= 2.7, < 3.0) prism (>= 1.0, < 2.0) @@ -114,7 +114,7 @@ DEPENDENCIES yard (~> 0.9.45) CHECKSUMS - archunit (1.1.0) + archunit (1.1.1) ast (2.4.3) sha256=954615157c1d6a382bc27d690d973195e79db7f55e9765ac7c481c60bdb4d383 bundler-audit (0.9.3) sha256=81c8766c71e47d0d28a0f98c7eed028539f21a6ea3cd8f685eb6f42333c9b4e9 csv (3.3.7) sha256=15a3f16f6628da8664d455eb634e7f22031bcf53044923caf56975109e71b9f8 diff --git a/benchmark/README.md b/benchmark/README.md index 6a3edb9..af7bd42 100644 --- a/benchmark/README.md +++ b/benchmark/README.md @@ -6,6 +6,64 @@ pinned Rails production-source evidence, see the [representative study](study/RE For pinned production-library compatibility checks, public passing/failing rule witnesses, and real-world timing artifacts, see the [static canary guide](canary/README.md). +## Source-discovery follow-up (#100) + +The same extraction benchmark now reports `cold.memory_growth.allocated_objects` and a +`graph_sha256` covering ordered edges and their structured source evidence. Allocation counts +are work performed during cold analysis, not retained-memory measurements. `--excluded-files N` +adds a deliberately malformed `vendor/` tree; source counts must still be requested files + 1 +(the generator's shared source), and no excluded file may be parsed. + +`retained` reports Ruby-heap growth after an explicit GC following both timed operations; this +extra GC is outside the cold/warm timers. Peak RSS remains a whole-process high-water mark, +including fixture generation, and pre-GC cold heap growth can vary with collection timing. + +For example: + +```sh +bundle exec ruby benchmark/extraction.rb --files 10000 --gems 2 --imports-per-file 0 --constants +bundle exec ruby benchmark/extraction.rb --files 1000 --excluded-files 3000 --constants +``` + +Manually dispatch `performance-study.yml` on the candidate ref to run three alternating fresh +baseline/candidate pairs for sparse 5k/10k, import-heavy and excluded-tree cases on Linux and +Windows. The discovery job uses the normal production bundle, v1.1.0 library baseline and the +identical updated `benchmark/extraction.rb` harness in both checkouts. It checks full graph/evidence +digests, actual source inventories and cached graph identity, and retains every JSON result. +No competitor backend is involved in these paired measurements. Normal PRs do not run the heavy +discovery matrix; existing generous extraction-smoke ceilings stay unchanged. + +Local Windows x64 / Ruby 4.0.6 medians (2026-11-24; three pairs per case): + +| Case | Before cold | After cold | Before discovery | After discovery | Before allocations | After allocations | +| --- | ---: | ---: | ---: | ---: | ---: | ---: | +| Sparse 5,000 (+1 shared) | 6.891 s | 4.263 s | 3.494 s | 1.172 s | 5,209,871 | 2,627,199 | +| Sparse 10,000 (+1 shared) | 13.830 s | 7.839 s | 7.253 s | 1.948 s | 10,414,873 | 5,252,199 | +| 1,000 / three imports | 2.221 s | 1.490 s | 0.425 s | 0.126 s | 1,466,479 | 947,810 | +| 1,000 / 3,000 excluded | 1.420 s | 0.831 s | 0.774 s | 0.200 s | 1,046,126 | 527,204 | + +[Every local sample](study/discovery-windows.json) is retained. All pairs have identical graph +and evidence digests, including source ordering. Timings depend on filesystem cache, host and +load; fixtures are freshly generated outside profiled analysis, not a cold-disk experiment. +Windows peak RSS is unavailable, not zero. Heap snapshots are not post-GC retained memory. +Do not turn these local medians into portable timing gates or a universal speed claim. + +Hosted Linux/Windows verification (Ruby 4.0.10, three pairs per case) is retained in +[discovery-ci.json](study/discovery-ci.json), including separate post-GC measurements: + +| 10,000-file case | Before cold | After cold | Before discovery | After discovery | Before allocations | After allocations | +| --- | ---: | ---: | ---: | ---: | ---: | ---: | +| Linux | 1.750 s | 1.078 s | 0.716 s | 0.030 s | 8,453,786 | 4,891,912 | +| Windows | 5.922 s | 3.688 s | 3.228 s | 0.962 s | 9,064,085 | 5,102,011 | + +Post-GC Ruby-heap growth for this case is essentially unchanged: Linux 8,417,117 to 8,414,189 +bytes; Windows 8,414,189 to 8,414,077 bytes. This supports lower transient work, not a new claim +that every memory measure improves. Linux whole-process peak RSS drops from 97,212 to 83,144 KiB +at 10k, but rises from 58,760 to 64,408 KiB at 5k despite similar retained heap. Pre-GC growth is +similarly mixed; Windows RSS is unavailable. Warm lookup medians remain below 1.3 ms in these +samples, with one small Linux excluded-tree case increasing from 0.117 to 0.902 ms despite +confirmed graph reuse. Keep these tradeoffs visible rather than optimizing to a single number. + The benchmark generates a deterministic multi-gem Ruby repository, then measures one cold graph build and one same-process cached lookup. Target application code and generated gemspecs are never executed. @@ -85,8 +143,8 @@ symlink-skipping walker on the same 1,000-file / 10-gem / 3-import corpus. Ruby Every pair retained 4,001 graph edges and reused its cached graph. Warm lookups ranged from 3.6–11.0 ms. Enumeration improved in each pair, but the large run-to-run variation makes an exact speedup claim inappropriate. This is evidence against an obvious regression, not a portable speed -guarantee. The walker now uses one explicit `lstat` result for its own type/symlink decisions; -Ruby's `Find` traversal still performs its own metadata checks. +guarantee. That audit used one explicit `lstat` result for its own type/symlink decisions; +Ruby's `Find` traversal still performed its own metadata checks at that revision. ### Original extraction baseline diff --git a/benchmark/extraction.rb b/benchmark/extraction.rb index d8f7d5b..5e4f94f 100644 --- a/benchmark/extraction.rb +++ b/benchmark/extraction.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'fileutils' +require 'digest' require 'json' require 'objspace' require 'optparse' @@ -13,6 +14,7 @@ module ExtractionBenchmark files: 1_000, gems: 10, imports_per_file: 3, + excluded_files: 0, dependency_analysis: :imports, max_cold_seconds: nil, max_warm_seconds: nil, @@ -21,19 +23,23 @@ module ExtractionBenchmark # Deterministic multi-gem Ruby corpus with both repeated and cross-gem requires. class Corpus - attr_reader :root, :file_count, :gem_count, :imports_per_file + attr_reader :root, :file_count, :gem_count, :imports_per_file, :excluded_file_count - def initialize(root, file_count:, gem_count:, imports_per_file:) + def initialize(root, file_count:, gem_count:, imports_per_file:, excluded_file_count: 0) @root = root @file_count = positive_integer(file_count, :files) @gem_count = positive_integer(gem_count, :gems) @imports_per_file = non_negative_integer(imports_per_file, :imports_per_file) + @excluded_file_count = non_negative_integer(excluded_file_count, :excluded_files) end def generate gem_count.times { |index| write_gemspec(index) } file_count.times { |index| write_source(index) } write('packages/gem_00/lib/shared/common.rb', "# shared benchmark dependency\n") + excluded_file_count.times do |index| + write("vendor/generated/deep/file_#{index}.rb", "# excluded malformed generator\nclass <") + end root end @@ -97,7 +103,7 @@ def write(relative_path, contents) # Runs one cold graph build and one same-process graph-cache lookup. class Runner RunValues = Data.define( - :cold_graph, :cold_profile, :warm_graph, :warm_profile, :before, :after_cold + :cold_graph, :cold_profile, :warm_graph, :warm_profile, :before, :after_cold, :after_gc ) private_constant :RunValues @@ -123,7 +129,8 @@ def collect_values after_cold = memory_snapshot warm_graph, warm_profile = extract_profiled RunValues.new( - cold_graph:, cold_profile:, warm_graph:, warm_profile:, before:, after_cold: + cold_graph:, cold_profile:, warm_graph:, warm_profile:, before:, after_cold:, + after_gc: retained_snapshot ) end @@ -138,18 +145,44 @@ def extract_profiled def benchmark_result(values) { - corpus: @configuration.slice(:files, :gems, :imports_per_file, :dependency_analysis), + corpus: corpus_description, graph_edges: values.cold_graph.length, + graph_sha256: graph_digest(values.cold_graph), graph_cache_reused: values.cold_graph.equal?(values.warm_graph), cold: values.cold_profile.to_h.merge(memory_delta(values.before, values.after_cold)), warm: values.warm_profile.to_h, + retained: retained_memory(values), runtime: { ruby: RUBY_VERSION, platform: RUBY_PLATFORM } } end + def graph_digest(graph) + edges = graph.map { |edge| edge.to_h.merge(evidence: edge.evidence.map(&:to_h)) } + Digest::SHA256.hexdigest(JSON.generate(edges)) + end + + def corpus_description + @configuration.slice(:files, :gems, :imports_per_file, :excluded_files, :dependency_analysis) + end + + def retained_snapshot + GC.start + memory_snapshot + end + + def retained_memory(values) + { + ruby_heap_bytes: values.after_gc[:ruby_heap_bytes], + ruby_heap_growth_bytes: values.after_gc[:ruby_heap_bytes] - values.before[:ruby_heap_bytes], + heap_live_slots_growth: values.after_gc[:heap_live_slots] - values.before[:heap_live_slots], + process_peak_rss_kb: values.after_gc[:process_peak_rss_kb] + } + end + def memory_snapshot { ruby_heap_bytes: ObjectSpace.memsize_of_all, + allocated_objects: GC.stat(:total_allocated_objects), heap_live_slots: GC.stat(:heap_live_slots), process_peak_rss_kb: linux_peak_rss_kb } @@ -166,6 +199,7 @@ def memory_delta(before, after) { memory: after, memory_growth: { + allocated_objects: after[:allocated_objects] - before[:allocated_objects], ruby_heap_bytes: after[:ruby_heap_bytes] - before[:ruby_heap_bytes], heap_live_slots: after[:heap_live_slots] - before[:heap_live_slots] } @@ -210,6 +244,7 @@ def configure_corpus_options(parser, options) parser.on('--files N', Integer) { |value| options[:files] = value } parser.on('--gems N', Integer) { |value| options[:gems] = value } parser.on('--imports-per-file N', Integer) { |value| options[:imports_per_file] = value } + parser.on('--excluded-files N', Integer) { |value| options[:excluded_files] = value } end def configure_threshold_options(parser, options) @@ -229,7 +264,8 @@ def run_in(directory, options) directory, file_count: options.fetch(:files), gem_count: options.fetch(:gems), - imports_per_file: options.fetch(:imports_per_file) + imports_per_file: options.fetch(:imports_per_file), + excluded_file_count: options.fetch(:excluded_files) ).generate payload = JSON.pretty_generate(Runner.new(directory, options).run) write_payload(options[:output], payload) diff --git a/benchmark/study/Gemfile.lock b/benchmark/study/Gemfile.lock index c2eb756..b8ddf3e 100644 --- a/benchmark/study/Gemfile.lock +++ b/benchmark/study/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: ../.. specs: - archunit (1.1.0) + archunit (1.1.1) csv (>= 3.3, < 4.0) json (>= 2.7, < 3.0) prism (>= 1.0, < 2.0) @@ -32,7 +32,7 @@ DEPENDENCIES CHECKSUMS archspec (1.1.0) sha256=861d4869c81617139ea9e42e6de1456ba00d0538f3994476073829de223a754d - archunit (1.1.0) + archunit (1.1.1) csv (3.3.7) sha256=15a3f16f6628da8664d455eb634e7f22031bcf53044923caf56975109e71b9f8 json (2.21.2) sha256=1f1d3b7cf2b3ba1a69beca0bb6db13d5438b80bff3cd54cdaaa620b9b07c1c6a prism (1.9.0) sha256=7b530c6a9f92c24300014919c9dcbc055bf4cdf51ec30aed099b06cd6674ef85 diff --git a/benchmark/study/README.md b/benchmark/study/README.md index 6c04155..4fb5be7 100644 --- a/benchmark/study/README.md +++ b/benchmark/study/README.md @@ -5,6 +5,11 @@ RubyDEX 0.4.0, Prism 1.9.0, JSON 2.21.2 and CSV 3.3.7. Its lock contains Linux/W package checksums without changing root dependencies. The candidate is the checkout revision, not necessarily the published gem. +For the separate source-discovery optimization follow-up (#100), see the +[root extraction benchmark](../README.md#source-discovery-follow-up-100) and its paired-sample +JSON. The manual performance workflow also runs that unchanged generator/harness against the +v1.1.0 and candidate libraries on Linux/Windows, using only the normal production bundle. + ## Shared-policy study `run.rb` generates Rails-like and four-engine layouts with 200 Ruby files by default. Density diff --git a/benchmark/study/discovery-ci.json b/benchmark/study/discovery-ci.json new file mode 100644 index 0000000..669f9df --- /dev/null +++ b/benchmark/study/discovery-ci.json @@ -0,0 +1,1109 @@ +{ + "schema_version": 1, + "date": "2026-11-24", + "run_url": "https://github.com/dragonsec-ai/ArchUnitRuby/actions/runs/45391662012", + "baseline_ref": "v1.1.0", + "baseline_commit": "3dd615305b30428be7b5205cf396d360bb0833b4", + "candidate_commit": "78653585f47c72a487687e6df210149c8646e643", + "method": "Three alternating baseline/candidate fresh processes per case using the identical updated extraction benchmark. Sample-array indexes correspond to trials 1, 2 and 3. Fixture generation is outside profiled cold analysis; file caches are not flushed. Cold allocations are work, heap_growth_bytes is a pre-GC snapshot, retained_heap_growth_bytes/slots follow explicit GC after both timed operations, and process peak RSS includes fixture generation. Windows RSS is unavailable (null). No portable timing gate or competitor speed claim.", + "platforms": [ + { + "platform": "ubuntu-latest", + "runtime": { + "ruby": "4.0.10", + "platform": "x86_64-linux" + }, + "cases": [ + { + "name": "excluded-1000", + "corpus": { + "files": 1000, + "gems": 2, + "imports_per_file": 0, + "excluded_files": 3000, + "dependency_analysis": "imports_and_constants" + }, + "source_files": 1001, + "graph_edges": 2001, + "graph_sha256": "5774d7f750a28a281d9cfabbcc866f62ea47fc7c58ea15aa076ed42b7327f2e2", + "all_graphs_and_evidence_equal": true, + "all_warm_graphs_reused": true, + "samples": { + "baseline": { + "cold_seconds": [ + 0.179575, + 0.181208, + 0.185437 + ], + "discovery_seconds": [ + 0.068841, + 0.068878, + 0.072601 + ], + "warm_seconds": [ + 0.000113, + 0.000117, + 0.000139 + ], + "allocated_objects": [ + 848960, + 848960, + 848960 + ], + "heap_growth_bytes": [ + 3076117, + 3076277, + 3072277 + ], + "peak_rss_kb": [ + 40200, + 40476, + 40456 + ], + "retained_heap_growth_bytes": [ + 857117, + 857117, + 857117 + ], + "retained_heap_slots_growth": [ + 13602, + 13603, + 13603 + ], + "retained_peak_rss_kb": [ + 40200, + 40476, + 40456 + ] + }, + "candidate": { + "cold_seconds": [ + 0.103522, + 0.102535, + 0.100945 + ], + "discovery_seconds": [ + 0.003035, + 0.003, + 0.002965 + ], + "warm_seconds": [ + 0.000933, + 0.000902, + 0.000102 + ], + "allocated_objects": [ + 490918, + 490918, + 490918 + ], + "heap_growth_bytes": [ + 6319773, + 6320293, + 3210741 + ], + "peak_rss_kb": [ + 40840, + 40860, + 40904 + ], + "retained_heap_growth_bytes": [ + 854189, + 854189, + 854189 + ], + "retained_heap_slots_growth": [ + 13593, + 13593, + 13593 + ], + "retained_peak_rss_kb": [ + 40840, + 40860, + 40904 + ] + } + }, + "medians": { + "baseline": { + "cold_seconds": 0.181208, + "discovery_seconds": 0.068878, + "warm_seconds": 0.000117, + "allocated_objects": 848960, + "heap_growth_bytes": 3076117, + "peak_rss_kb": 40456, + "retained_heap_growth_bytes": 857117, + "retained_heap_slots_growth": 13603, + "retained_peak_rss_kb": 40456 + }, + "candidate": { + "cold_seconds": 0.102535, + "discovery_seconds": 0.003, + "warm_seconds": 0.000902, + "allocated_objects": 490918, + "heap_growth_bytes": 6319773, + "peak_rss_kb": 40860, + "retained_heap_growth_bytes": 854189, + "retained_heap_slots_growth": 13593, + "retained_peak_rss_kb": 40860 + } + } + }, + { + "name": "imports-1000", + "corpus": { + "files": 1000, + "gems": 2, + "imports_per_file": 3, + "excluded_files": 0, + "dependency_analysis": "imports_and_constants" + }, + "source_files": 1001, + "graph_edges": 4001, + "graph_sha256": "3c8f2a84037a035f4a08fb7cf9c4eaf3b685f4b63c9ef52a25f868b3df3aed26", + "all_graphs_and_evidence_equal": true, + "all_warm_graphs_reused": true, + "samples": { + "baseline": { + "cold_seconds": [ + 0.296839, + 0.297147, + 0.299422 + ], + "discovery_seconds": [ + 0.070016, + 0.069896, + 0.070007 + ], + "warm_seconds": [ + 0.000122, + 0.000124, + 0.000119 + ], + "allocated_objects": [ + 1180391, + 1180391, + 1180391 + ], + "heap_growth_bytes": [ + 5318037, + 10880641, + 5322717 + ], + "peak_rss_kb": [ + 44784, + 45684, + 44520 + ], + "retained_heap_growth_bytes": [ + 2179181, + 2179181, + 2179181 + ], + "retained_heap_slots_growth": [ + 32693, + 32693, + 32692 + ], + "retained_peak_rss_kb": [ + 44784, + 45684, + 44520 + ] + }, + "candidate": { + "cold_seconds": [ + 0.233317, + 0.227768, + 0.220467 + ], + "discovery_seconds": [ + 0.002944, + 0.002889, + 0.00292 + ], + "warm_seconds": [ + 0.000122, + 0.000114, + 0.000132 + ], + "allocated_objects": [ + 822523, + 822523, + 822523 + ], + "heap_growth_bytes": [ + 5471989, + 4789349, + 10909153 + ], + "peak_rss_kb": [ + 43720, + 44672, + 45828 + ], + "retained_heap_growth_bytes": [ + 2176253, + 2176253, + 2176253 + ], + "retained_heap_slots_growth": [ + 32687, + 32688, + 32688 + ], + "retained_peak_rss_kb": [ + 43720, + 44672, + 45828 + ] + } + }, + "medians": { + "baseline": { + "cold_seconds": 0.297147, + "discovery_seconds": 0.070007, + "warm_seconds": 0.000122, + "allocated_objects": 1180391, + "heap_growth_bytes": 5322717, + "peak_rss_kb": 44784, + "retained_heap_growth_bytes": 2179181, + "retained_heap_slots_growth": 32693, + "retained_peak_rss_kb": 44784 + }, + "candidate": { + "cold_seconds": 0.227768, + "discovery_seconds": 0.00292, + "warm_seconds": 0.000122, + "allocated_objects": 822523, + "heap_growth_bytes": 5471989, + "peak_rss_kb": 44672, + "retained_heap_growth_bytes": 2176253, + "retained_heap_slots_growth": 32688, + "retained_peak_rss_kb": 44672 + } + } + }, + { + "name": "sparse-10000", + "corpus": { + "files": 10000, + "gems": 2, + "imports_per_file": 0, + "excluded_files": 0, + "dependency_analysis": "imports_and_constants" + }, + "source_files": 10001, + "graph_edges": 20001, + "graph_sha256": "e6030df42463fb67069568d3753ee1ab73041ab37d90c23b8d196250c403b75e", + "all_graphs_and_evidence_equal": true, + "all_warm_graphs_reused": true, + "samples": { + "baseline": { + "cold_seconds": [ + 1.750284, + 1.742093, + 1.787126 + ], + "discovery_seconds": [ + 0.720229, + 0.707029, + 0.716253 + ], + "warm_seconds": [ + 0.000156, + 0.000179, + 0.000239 + ], + "allocated_objects": [ + 8453786, + 8453786, + 8453786 + ], + "heap_growth_bytes": [ + 28868765, + 61247741, + 61009221 + ], + "peak_rss_kb": [ + 82824, + 97928, + 97212 + ], + "retained_heap_growth_bytes": [ + 8417117, + 8417117, + 8417117 + ], + "retained_heap_slots_growth": [ + 130598, + 130599, + 130598 + ], + "retained_peak_rss_kb": [ + 82824, + 97808, + 97108 + ] + }, + "candidate": { + "cold_seconds": [ + 1.078077, + 1.049611, + 1.085911 + ], + "discovery_seconds": [ + 0.030216, + 0.030404, + 0.030774 + ], + "warm_seconds": [ + 0.000178, + 0.000147, + 0.000191 + ], + "allocated_objects": [ + 4891912, + 4891912, + 4891912 + ], + "heap_growth_bytes": [ + 28470717, + 29771517, + 26913357 + ], + "peak_rss_kb": [ + 83144, + 82520, + 90892 + ], + "retained_heap_growth_bytes": [ + 8414189, + 8414189, + 8414189 + ], + "retained_heap_slots_growth": [ + 130593, + 130594, + 130593 + ], + "retained_peak_rss_kb": [ + 83072, + 82356, + 90772 + ] + } + }, + "medians": { + "baseline": { + "cold_seconds": 1.750284, + "discovery_seconds": 0.716253, + "warm_seconds": 0.000179, + "allocated_objects": 8453786, + "heap_growth_bytes": 61009221, + "peak_rss_kb": 97212, + "retained_heap_growth_bytes": 8417117, + "retained_heap_slots_growth": 130598, + "retained_peak_rss_kb": 97108 + }, + "candidate": { + "cold_seconds": 1.078077, + "discovery_seconds": 0.030404, + "warm_seconds": 0.000178, + "allocated_objects": 4891912, + "heap_growth_bytes": 28470717, + "peak_rss_kb": 83144, + "retained_heap_growth_bytes": 8414189, + "retained_heap_slots_growth": 130593, + "retained_peak_rss_kb": 83072 + } + } + }, + { + "name": "sparse-5000", + "corpus": { + "files": 5000, + "gems": 2, + "imports_per_file": 0, + "excluded_files": 0, + "dependency_analysis": "imports_and_constants" + }, + "source_files": 5001, + "graph_edges": 10001, + "graph_sha256": "124950cdd40283daaad7784b48515256398efb305d91350b4113bb233217c0b3", + "all_graphs_and_evidence_equal": true, + "all_warm_graphs_reused": true, + "samples": { + "baseline": { + "cold_seconds": [ + 0.879333, + 0.872425, + 0.872172 + ], + "discovery_seconds": [ + 0.345165, + 0.345136, + 0.344143 + ], + "warm_seconds": [ + 0.000153, + 0.000169, + 0.000145 + ], + "allocated_objects": [ + 4228784, + 4228784, + 4228784 + ], + "heap_growth_bytes": [ + 14526157, + 26349069, + 14447197 + ], + "peak_rss_kb": [ + 58760, + 64808, + 58496 + ], + "retained_heap_growth_bytes": [ + 4217117, + 4217117, + 4217117 + ], + "retained_heap_slots_growth": [ + 65599, + 65598, + 65598 + ], + "retained_peak_rss_kb": [ + 58692, + 64644, + 58364 + ] + }, + "candidate": { + "cold_seconds": [ + 0.523664, + 0.519039, + 0.516518 + ], + "discovery_seconds": [ + 0.015293, + 0.015066, + 0.015477 + ], + "warm_seconds": [ + 0.000192, + 0.000159, + 0.000157 + ], + "allocated_objects": [ + 2446912, + 2446912, + 2446912 + ], + "heap_growth_bytes": [ + 29636461, + 26407045, + 29953301 + ], + "peak_rss_kb": [ + 64408, + 62772, + 64588 + ], + "retained_heap_growth_bytes": [ + 4214189, + 4214189, + 4214189 + ], + "retained_heap_slots_growth": [ + 65594, + 65593, + 65593 + ], + "retained_peak_rss_kb": [ + 64384, + 62632, + 64456 + ] + } + }, + "medians": { + "baseline": { + "cold_seconds": 0.872425, + "discovery_seconds": 0.345136, + "warm_seconds": 0.000153, + "allocated_objects": 4228784, + "heap_growth_bytes": 14526157, + "peak_rss_kb": 58760, + "retained_heap_growth_bytes": 4217117, + "retained_heap_slots_growth": 65598, + "retained_peak_rss_kb": 58692 + }, + "candidate": { + "cold_seconds": 0.519039, + "discovery_seconds": 0.015293, + "warm_seconds": 0.000159, + "allocated_objects": 2446912, + "heap_growth_bytes": 29636461, + "peak_rss_kb": 64408, + "retained_heap_growth_bytes": 4214189, + "retained_heap_slots_growth": 65593, + "retained_peak_rss_kb": 64384 + } + } + } + ] + }, + { + "platform": "windows-latest", + "runtime": { + "ruby": "4.0.10", + "platform": "x64-mingw-ucrt" + }, + "cases": [ + { + "name": "excluded-1000", + "corpus": { + "files": 1000, + "gems": 2, + "imports_per_file": 0, + "excluded_files": 3000, + "dependency_analysis": "imports_and_constants" + }, + "source_files": 1001, + "graph_edges": 2001, + "graph_sha256": "5774d7f750a28a281d9cfabbcc866f62ea47fc7c58ea15aa076ed42b7327f2e2", + "all_graphs_and_evidence_equal": true, + "all_warm_graphs_reused": true, + "samples": { + "baseline": { + "cold_seconds": [ + 0.628046, + 0.603181, + 0.617796 + ], + "discovery_seconds": [ + 0.317824, + 0.316312, + 0.315977 + ], + "warm_seconds": [ + 0.001286, + 0.001236, + 0.001406 + ], + "allocated_objects": [ + 910278, + 910278, + 910278 + ], + "heap_growth_bytes": [ + 4025253, + 6673909, + 6671749 + ], + "peak_rss_kb": [ + null, + null, + null + ], + "retained_heap_growth_bytes": [ + 854189, + 854189, + 854189 + ], + "retained_heap_slots_growth": [ + 13601, + 13601, + 13601 + ], + "retained_peak_rss_kb": [ + null, + null, + null + ] + }, + "candidate": { + "cold_seconds": [ + 0.366345, + 0.365392, + 0.364036 + ], + "discovery_seconds": [ + 0.096164, + 0.095895, + 0.096541 + ], + "warm_seconds": [ + 0.001267, + 0.001252, + 0.001276 + ], + "allocated_objects": [ + 512016, + 512016, + 512016 + ], + "heap_growth_bytes": [ + 5433349, + 2249485, + 2356405 + ], + "peak_rss_kb": [ + null, + null, + null + ], + "retained_heap_growth_bytes": [ + 854077, + 854157, + 854157 + ], + "retained_heap_slots_growth": [ + 13592, + 13593, + 13593 + ], + "retained_peak_rss_kb": [ + null, + null, + null + ] + } + }, + "medians": { + "baseline": { + "cold_seconds": 0.617796, + "discovery_seconds": 0.316312, + "warm_seconds": 0.001286, + "allocated_objects": 910278, + "heap_growth_bytes": 6671749, + "peak_rss_kb": null, + "retained_heap_growth_bytes": 854189, + "retained_heap_slots_growth": 13601, + "retained_peak_rss_kb": null + }, + "candidate": { + "cold_seconds": 0.365392, + "discovery_seconds": 0.096164, + "warm_seconds": 0.001267, + "allocated_objects": 512016, + "heap_growth_bytes": 2356405, + "peak_rss_kb": null, + "retained_heap_growth_bytes": 854157, + "retained_heap_slots_growth": 13593, + "retained_peak_rss_kb": null + } + } + }, + { + "name": "imports-1000", + "corpus": { + "files": 1000, + "gems": 2, + "imports_per_file": 3, + "excluded_files": 0, + "dependency_analysis": "imports_and_constants" + }, + "source_files": 1001, + "graph_edges": 4001, + "graph_sha256": "3c8f2a84037a035f4a08fb7cf9c4eaf3b685f4b63c9ef52a25f868b3df3aed26", + "all_graphs_and_evidence_equal": true, + "all_warm_graphs_reused": true, + "samples": { + "baseline": { + "cold_seconds": [ + 0.971425, + 0.984057, + 0.992683 + ], + "discovery_seconds": [ + 0.33084, + 0.335338, + 0.334054 + ], + "warm_seconds": [ + 0.001278, + 0.001209, + 0.001308 + ], + "allocated_objects": [ + 1285693, + 1285692, + 1285693 + ], + "heap_growth_bytes": [ + 5487301, + 5483301, + 5550453 + ], + "peak_rss_kb": [ + null, + null, + null + ], + "retained_heap_growth_bytes": [ + 2176381, + 2176381, + 2176253 + ], + "retained_heap_slots_growth": [ + 32691, + 32691, + 32691 + ], + "retained_peak_rss_kb": [ + null, + null, + null + ] + }, + "candidate": { + "cold_seconds": [ + 0.742433, + 0.724763, + 0.729895 + ], + "discovery_seconds": [ + 0.102825, + 0.096019, + 0.095302 + ], + "warm_seconds": [ + 0.001276, + 0.001261, + 0.001241 + ], + "allocated_objects": [ + 887626, + 887621, + 887622 + ], + "heap_growth_bytes": [ + 9597951, + 9530151, + 12075143 + ], + "peak_rss_kb": [ + null, + null, + null + ], + "retained_heap_growth_bytes": [ + 2176269, + 2176269, + 2176269 + ], + "retained_heap_slots_growth": [ + 32686, + 32686, + 32686 + ], + "retained_peak_rss_kb": [ + null, + null, + null + ] + } + }, + "medians": { + "baseline": { + "cold_seconds": 0.984057, + "discovery_seconds": 0.334054, + "warm_seconds": 0.001278, + "allocated_objects": 1285693, + "heap_growth_bytes": 5487301, + "peak_rss_kb": null, + "retained_heap_growth_bytes": 2176381, + "retained_heap_slots_growth": 32691, + "retained_peak_rss_kb": null + }, + "candidate": { + "cold_seconds": 0.729895, + "discovery_seconds": 0.096019, + "warm_seconds": 0.001261, + "allocated_objects": 887622, + "heap_growth_bytes": 9597951, + "peak_rss_kb": null, + "retained_heap_growth_bytes": 2176269, + "retained_heap_slots_growth": 32686, + "retained_peak_rss_kb": null + } + } + }, + { + "name": "sparse-10000", + "corpus": { + "files": 10000, + "gems": 2, + "imports_per_file": 0, + "excluded_files": 0, + "dependency_analysis": "imports_and_constants" + }, + "source_files": 10001, + "graph_edges": 20001, + "graph_sha256": "e6030df42463fb67069568d3753ee1ab73041ab37d90c23b8d196250c403b75e", + "all_graphs_and_evidence_equal": true, + "all_warm_graphs_reused": true, + "samples": { + "baseline": { + "cold_seconds": [ + 6.515925, + 5.921527, + 5.918766 + ], + "discovery_seconds": [ + 3.209089, + 3.241515, + 3.228196 + ], + "warm_seconds": [ + 0.001263, + 0.001257, + 0.001337 + ], + "allocated_objects": [ + 9064087, + 9064085, + 9064085 + ], + "heap_growth_bytes": [ + 55033381, + 49918317, + 50397477 + ], + "peak_rss_kb": [ + null, + null, + null + ], + "retained_heap_growth_bytes": [ + 8414189, + 8414189, + 8414189 + ], + "retained_heap_slots_growth": [ + 130597, + 130597, + 130597 + ], + "retained_peak_rss_kb": [ + null, + null, + null + ] + }, + "candidate": { + "cold_seconds": [ + 3.688421, + 3.695359, + 3.659328 + ], + "discovery_seconds": [ + 0.951477, + 0.96397, + 0.962094 + ], + "warm_seconds": [ + 0.001338, + 0.001252, + 0.001237 + ], + "allocated_objects": [ + 5102011, + 5102013, + 5102011 + ], + "heap_growth_bytes": [ + 55645629, + 55831749, + 49947005 + ], + "peak_rss_kb": [ + null, + null, + null + ], + "retained_heap_growth_bytes": [ + 8414077, + 8414077, + 8414077 + ], + "retained_heap_slots_growth": [ + 130592, + 130592, + 130592 + ], + "retained_peak_rss_kb": [ + null, + null, + null + ] + } + }, + "medians": { + "baseline": { + "cold_seconds": 5.921527, + "discovery_seconds": 3.228196, + "warm_seconds": 0.001263, + "allocated_objects": 9064085, + "heap_growth_bytes": 50397477, + "peak_rss_kb": null, + "retained_heap_growth_bytes": 8414189, + "retained_heap_slots_growth": 130597, + "retained_peak_rss_kb": null + }, + "candidate": { + "cold_seconds": 3.688421, + "discovery_seconds": 0.962094, + "warm_seconds": 0.001252, + "allocated_objects": 5102011, + "heap_growth_bytes": 55645629, + "peak_rss_kb": null, + "retained_heap_growth_bytes": 8414077, + "retained_heap_slots_growth": 130592, + "retained_peak_rss_kb": null + } + } + }, + { + "name": "sparse-5000", + "corpus": { + "files": 5000, + "gems": 2, + "imports_per_file": 0, + "excluded_files": 0, + "dependency_analysis": "imports_and_constants" + }, + "source_files": 5001, + "graph_edges": 10001, + "graph_sha256": "124950cdd40283daaad7784b48515256398efb305d91350b4113bb233217c0b3", + "all_graphs_and_evidence_equal": true, + "all_warm_graphs_reused": true, + "samples": { + "baseline": { + "cold_seconds": [ + 3.103572, + 2.946973, + 2.93277 + ], + "discovery_seconds": [ + 1.691806, + 1.629993, + 1.631308 + ], + "warm_seconds": [ + 0.001258, + 0.001236, + 0.001256 + ], + "allocated_objects": [ + 4534083, + 4534085, + 4534083 + ], + "heap_growth_bytes": [ + 30523909, + 30524989, + 30526109 + ], + "peak_rss_kb": [ + null, + null, + null + ], + "retained_heap_growth_bytes": [ + 4214189, + 4214189, + 4214189 + ], + "retained_heap_slots_growth": [ + 65597, + 65597, + 65597 + ], + "retained_peak_rss_kb": [ + null, + null, + null + ] + }, + "candidate": { + "cold_seconds": [ + 1.809236, + 1.802747, + 1.821017 + ], + "discovery_seconds": [ + 0.476424, + 0.470249, + 0.482749 + ], + "warm_seconds": [ + 0.00128, + 0.001289, + 0.001301 + ], + "allocated_objects": [ + 2552011, + 2552011, + 2552011 + ], + "heap_growth_bytes": [ + 23998093, + 23963933, + 24006573 + ], + "peak_rss_kb": [ + null, + null, + null + ], + "retained_heap_growth_bytes": [ + 4214077, + 4214077, + 4214077 + ], + "retained_heap_slots_growth": [ + 65592, + 65592, + 65592 + ], + "retained_peak_rss_kb": [ + null, + null, + null + ] + } + }, + "medians": { + "baseline": { + "cold_seconds": 2.946973, + "discovery_seconds": 1.631308, + "warm_seconds": 0.001256, + "allocated_objects": 4534083, + "heap_growth_bytes": 30524989, + "peak_rss_kb": null, + "retained_heap_growth_bytes": 4214189, + "retained_heap_slots_growth": 65597, + "retained_peak_rss_kb": null + }, + "candidate": { + "cold_seconds": 1.809236, + "discovery_seconds": 0.476424, + "warm_seconds": 0.001289, + "allocated_objects": 2552011, + "heap_growth_bytes": 23998093, + "peak_rss_kb": null, + "retained_heap_growth_bytes": 4214077, + "retained_heap_slots_growth": 65592, + "retained_peak_rss_kb": null + } + } + } + ] + } + ] +} diff --git a/benchmark/study/discovery-windows.json b/benchmark/study/discovery-windows.json new file mode 100644 index 0000000..a7fd5d3 --- /dev/null +++ b/benchmark/study/discovery-windows.json @@ -0,0 +1,633 @@ +{ + "schema_version": 1, + "date": "2026-11-24", + "baseline_ref": "v1.1.0", + "baseline_commit": "3dd615305b30428be7b5205cf396d360bb0833b4", + "candidate_branch": "perf/source-walk-100", + "candidate_version": "1.1.1 (unreleased)", + "method": "Three alternating baseline/candidate fresh processes per shape, same benchmark/extraction.rb harness. Fixture generation is outside profiled analysis. Full graph/evidence digests and cache/source counts checked per pair. Windows exposes no process peak RSS here. Timing samples are machine- and load-dependent; allocation counts are cold-analysis totals, not retained memory.", + "summary": [ + { + "name": "excluded-1000", + "pairs": 3, + "before_cold": 1.420008, + "after_cold": 0.831067, + "before_discovery": 0.773813, + "after_discovery": 0.199927, + "before_objects": 1046126, + "after_objects": 527204, + "digest_equal": true + }, + { + "name": "imports-1000", + "pairs": 3, + "before_cold": 2.221244, + "after_cold": 1.489974, + "before_discovery": 0.424598, + "after_discovery": 0.125774, + "before_objects": 1466479, + "after_objects": 947810, + "digest_equal": true + }, + { + "name": "sparse-10000", + "pairs": 3, + "before_cold": 13.830182, + "after_cold": 7.838517, + "before_discovery": 7.252739, + "after_discovery": 1.948354, + "before_objects": 10414873, + "after_objects": 5252199, + "digest_equal": true + }, + { + "name": "sparse-5000", + "pairs": 3, + "before_cold": 6.891356, + "after_cold": 4.262591, + "before_discovery": 3.493604, + "after_discovery": 1.172176, + "before_objects": 5209871, + "after_objects": 2627199, + "digest_equal": true + } + ], + "samples": [ + { + "file": "excluded-1000-1-baseline.json", + "runtime": { + "ruby": "4.0.6", + "platform": "x64-mingw-ucrt" + }, + "corpus": { + "files": 1000, + "gems": 2, + "imports_per_file": 0, + "excluded_files": 3000, + "dependency_analysis": "imports_and_constants" + }, + "graph_sha256": "5774d7f750a28a281d9cfabbcc866f62ea47fc7c58ea15aa076ed42b7327f2e2", + "graph_edges": 2001, + "source_files": 1001, + "cache_reused": true, + "cold_seconds": 1.420008, + "discovery_seconds": 0.773813, + "warm_seconds": 0.004125, + "allocated_objects": 1046126, + "heap_growth_bytes": 2066483, + "peak_rss_kb": null + }, + { + "file": "excluded-1000-1-candidate.json", + "runtime": { + "ruby": "4.0.6", + "platform": "x64-mingw-ucrt" + }, + "corpus": { + "files": 1000, + "gems": 2, + "imports_per_file": 0, + "excluded_files": 3000, + "dependency_analysis": "imports_and_constants" + }, + "graph_sha256": "5774d7f750a28a281d9cfabbcc866f62ea47fc7c58ea15aa076ed42b7327f2e2", + "graph_edges": 2001, + "source_files": 1001, + "cache_reused": true, + "cold_seconds": 0.671696, + "discovery_seconds": 0.199927, + "warm_seconds": 0.00365, + "allocated_objects": 527204, + "heap_growth_bytes": 6324475, + "peak_rss_kb": null + }, + { + "file": "excluded-1000-2-baseline.json", + "runtime": { + "ruby": "4.0.6", + "platform": "x64-mingw-ucrt" + }, + "corpus": { + "files": 1000, + "gems": 2, + "imports_per_file": 0, + "excluded_files": 3000, + "dependency_analysis": "imports_and_constants" + }, + "graph_sha256": "5774d7f750a28a281d9cfabbcc866f62ea47fc7c58ea15aa076ed42b7327f2e2", + "graph_edges": 2001, + "source_files": 1001, + "cache_reused": true, + "cold_seconds": 1.081957, + "discovery_seconds": 0.556284, + "warm_seconds": 0.004239, + "allocated_objects": 1046126, + "heap_growth_bytes": 2258323, + "peak_rss_kb": null + }, + { + "file": "excluded-1000-2-candidate.json", + "runtime": { + "ruby": "4.0.6", + "platform": "x64-mingw-ucrt" + }, + "corpus": { + "files": 1000, + "gems": 2, + "imports_per_file": 0, + "excluded_files": 3000, + "dependency_analysis": "imports_and_constants" + }, + "graph_sha256": "5774d7f750a28a281d9cfabbcc866f62ea47fc7c58ea15aa076ed42b7327f2e2", + "graph_edges": 2001, + "source_files": 1001, + "cache_reused": true, + "cold_seconds": 0.831067, + "discovery_seconds": 0.229944, + "warm_seconds": 0.006048, + "allocated_objects": 527204, + "heap_growth_bytes": 6201435, + "peak_rss_kb": null + }, + { + "file": "excluded-1000-3-baseline.json", + "runtime": { + "ruby": "4.0.6", + "platform": "x64-mingw-ucrt" + }, + "corpus": { + "files": 1000, + "gems": 2, + "imports_per_file": 0, + "excluded_files": 3000, + "dependency_analysis": "imports_and_constants" + }, + "graph_sha256": "5774d7f750a28a281d9cfabbcc866f62ea47fc7c58ea15aa076ed42b7327f2e2", + "graph_edges": 2001, + "source_files": 1001, + "cache_reused": true, + "cold_seconds": 1.678368, + "discovery_seconds": 0.891528, + "warm_seconds": 0.007522, + "allocated_objects": 1046126, + "heap_growth_bytes": 2066723, + "peak_rss_kb": null + }, + { + "file": "excluded-1000-3-candidate.json", + "runtime": { + "ruby": "4.0.6", + "platform": "x64-mingw-ucrt" + }, + "corpus": { + "files": 1000, + "gems": 2, + "imports_per_file": 0, + "excluded_files": 3000, + "dependency_analysis": "imports_and_constants" + }, + "graph_sha256": "5774d7f750a28a281d9cfabbcc866f62ea47fc7c58ea15aa076ed42b7327f2e2", + "graph_edges": 2001, + "source_files": 1001, + "cache_reused": true, + "cold_seconds": 0.838681, + "discovery_seconds": 0.177938, + "warm_seconds": 0.003793, + "allocated_objects": 527204, + "heap_growth_bytes": 6426715, + "peak_rss_kb": null + }, + { + "file": "imports-1000-1-baseline.json", + "runtime": { + "ruby": "4.0.6", + "platform": "x64-mingw-ucrt" + }, + "corpus": { + "files": 1000, + "gems": 2, + "imports_per_file": 3, + "excluded_files": 0, + "dependency_analysis": "imports_and_constants" + }, + "graph_sha256": "3c8f2a84037a035f4a08fb7cf9c4eaf3b685f4b63c9ef52a25f868b3df3aed26", + "graph_edges": 4001, + "source_files": 1001, + "cache_reused": true, + "cold_seconds": 2.545284, + "discovery_seconds": 0.714036, + "warm_seconds": 0.00304, + "allocated_objects": 1466479, + "heap_growth_bytes": 14208693, + "peak_rss_kb": null + }, + { + "file": "imports-1000-1-candidate.json", + "runtime": { + "ruby": "4.0.6", + "platform": "x64-mingw-ucrt" + }, + "corpus": { + "files": 1000, + "gems": 2, + "imports_per_file": 3, + "excluded_files": 0, + "dependency_analysis": "imports_and_constants" + }, + "graph_sha256": "3c8f2a84037a035f4a08fb7cf9c4eaf3b685f4b63c9ef52a25f868b3df3aed26", + "graph_edges": 4001, + "source_files": 1001, + "cache_reused": true, + "cold_seconds": 1.376539, + "discovery_seconds": 0.115167, + "warm_seconds": 0.00355, + "allocated_objects": 947810, + "heap_growth_bytes": 14378021, + "peak_rss_kb": null + }, + { + "file": "imports-1000-2-baseline.json", + "runtime": { + "ruby": "4.0.6", + "platform": "x64-mingw-ucrt" + }, + "corpus": { + "files": 1000, + "gems": 2, + "imports_per_file": 3, + "excluded_files": 0, + "dependency_analysis": "imports_and_constants" + }, + "graph_sha256": "3c8f2a84037a035f4a08fb7cf9c4eaf3b685f4b63c9ef52a25f868b3df3aed26", + "graph_edges": 4001, + "source_files": 1001, + "cache_reused": true, + "cold_seconds": 2.221244, + "discovery_seconds": 0.383856, + "warm_seconds": 0.00691, + "allocated_objects": 1466478, + "heap_growth_bytes": 12696717, + "peak_rss_kb": null + }, + { + "file": "imports-1000-2-candidate.json", + "runtime": { + "ruby": "4.0.6", + "platform": "x64-mingw-ucrt" + }, + "corpus": { + "files": 1000, + "gems": 2, + "imports_per_file": 3, + "excluded_files": 0, + "dependency_analysis": "imports_and_constants" + }, + "graph_sha256": "3c8f2a84037a035f4a08fb7cf9c4eaf3b685f4b63c9ef52a25f868b3df3aed26", + "graph_edges": 4001, + "source_files": 1001, + "cache_reused": true, + "cold_seconds": 1.489974, + "discovery_seconds": 0.134539, + "warm_seconds": 0.006134, + "allocated_objects": 947808, + "heap_growth_bytes": 14379021, + "peak_rss_kb": null + }, + { + "file": "imports-1000-3-baseline.json", + "runtime": { + "ruby": "4.0.6", + "platform": "x64-mingw-ucrt" + }, + "corpus": { + "files": 1000, + "gems": 2, + "imports_per_file": 3, + "excluded_files": 0, + "dependency_analysis": "imports_and_constants" + }, + "graph_sha256": "3c8f2a84037a035f4a08fb7cf9c4eaf3b685f4b63c9ef52a25f868b3df3aed26", + "graph_edges": 4001, + "source_files": 1001, + "cache_reused": true, + "cold_seconds": 2.052125, + "discovery_seconds": 0.424598, + "warm_seconds": 0.004758, + "allocated_objects": 1466482, + "heap_growth_bytes": 14213693, + "peak_rss_kb": null + }, + { + "file": "imports-1000-3-candidate.json", + "runtime": { + "ruby": "4.0.6", + "platform": "x64-mingw-ucrt" + }, + "corpus": { + "files": 1000, + "gems": 2, + "imports_per_file": 3, + "excluded_files": 0, + "dependency_analysis": "imports_and_constants" + }, + "graph_sha256": "3c8f2a84037a035f4a08fb7cf9c4eaf3b685f4b63c9ef52a25f868b3df3aed26", + "graph_edges": 4001, + "source_files": 1001, + "cache_reused": true, + "cold_seconds": 2.090173, + "discovery_seconds": 0.125774, + "warm_seconds": 0.00483, + "allocated_objects": 947811, + "heap_growth_bytes": 14378981, + "peak_rss_kb": null + }, + { + "file": "sparse-10000-1-baseline.json", + "runtime": { + "ruby": "4.0.6", + "platform": "x64-mingw-ucrt" + }, + "corpus": { + "files": 10000, + "gems": 2, + "imports_per_file": 0, + "excluded_files": 0, + "dependency_analysis": "imports_and_constants" + }, + "graph_sha256": "e6030df42463fb67069568d3753ee1ab73041ab37d90c23b8d196250c403b75e", + "graph_edges": 20001, + "source_files": 10001, + "cache_reused": true, + "cold_seconds": 15.3068, + "discovery_seconds": 8.06588, + "warm_seconds": 0.005482, + "allocated_objects": 10414873, + "heap_growth_bytes": 57596347, + "peak_rss_kb": null + }, + { + "file": "sparse-10000-1-candidate.json", + "runtime": { + "ruby": "4.0.6", + "platform": "x64-mingw-ucrt" + }, + "corpus": { + "files": 10000, + "gems": 2, + "imports_per_file": 0, + "excluded_files": 0, + "dependency_analysis": "imports_and_constants" + }, + "graph_sha256": "e6030df42463fb67069568d3753ee1ab73041ab37d90c23b8d196250c403b75e", + "graph_edges": 20001, + "source_files": 10001, + "cache_reused": true, + "cold_seconds": 7.646296, + "discovery_seconds": 1.736898, + "warm_seconds": 0.008675, + "allocated_objects": 5252201, + "heap_growth_bytes": 57355435, + "peak_rss_kb": null + }, + { + "file": "sparse-10000-2-baseline.json", + "runtime": { + "ruby": "4.0.6", + "platform": "x64-mingw-ucrt" + }, + "corpus": { + "files": 10000, + "gems": 2, + "imports_per_file": 0, + "excluded_files": 0, + "dependency_analysis": "imports_and_constants" + }, + "graph_sha256": "e6030df42463fb67069568d3753ee1ab73041ab37d90c23b8d196250c403b75e", + "graph_edges": 20001, + "source_files": 10001, + "cache_reused": true, + "cold_seconds": 13.743879, + "discovery_seconds": 7.252739, + "warm_seconds": 0.005185, + "allocated_objects": 10414873, + "heap_growth_bytes": 53560507, + "peak_rss_kb": null + }, + { + "file": "sparse-10000-2-candidate.json", + "runtime": { + "ruby": "4.0.6", + "platform": "x64-mingw-ucrt" + }, + "corpus": { + "files": 10000, + "gems": 2, + "imports_per_file": 0, + "excluded_files": 0, + "dependency_analysis": "imports_and_constants" + }, + "graph_sha256": "e6030df42463fb67069568d3753ee1ab73041ab37d90c23b8d196250c403b75e", + "graph_edges": 20001, + "source_files": 10001, + "cache_reused": true, + "cold_seconds": 7.838517, + "discovery_seconds": 1.948354, + "warm_seconds": 0.00574, + "allocated_objects": 5252199, + "heap_growth_bytes": 53483875, + "peak_rss_kb": null + }, + { + "file": "sparse-10000-3-baseline.json", + "runtime": { + "ruby": "4.0.6", + "platform": "x64-mingw-ucrt" + }, + "corpus": { + "files": 10000, + "gems": 2, + "imports_per_file": 0, + "excluded_files": 0, + "dependency_analysis": "imports_and_constants" + }, + "graph_sha256": "e6030df42463fb67069568d3753ee1ab73041ab37d90c23b8d196250c403b75e", + "graph_edges": 20001, + "source_files": 10001, + "cache_reused": true, + "cold_seconds": 13.830182, + "discovery_seconds": 6.83966, + "warm_seconds": 0.004264, + "allocated_objects": 10414873, + "heap_growth_bytes": 54961067, + "peak_rss_kb": null + }, + { + "file": "sparse-10000-3-candidate.json", + "runtime": { + "ruby": "4.0.6", + "platform": "x64-mingw-ucrt" + }, + "corpus": { + "files": 10000, + "gems": 2, + "imports_per_file": 0, + "excluded_files": 0, + "dependency_analysis": "imports_and_constants" + }, + "graph_sha256": "e6030df42463fb67069568d3753ee1ab73041ab37d90c23b8d196250c403b75e", + "graph_edges": 20001, + "source_files": 10001, + "cache_reused": true, + "cold_seconds": 9.575185, + "discovery_seconds": 2.298649, + "warm_seconds": 0.004332, + "allocated_objects": 5252199, + "heap_growth_bytes": 54647235, + "peak_rss_kb": null + }, + { + "file": "sparse-5000-1-baseline.json", + "runtime": { + "ruby": "4.0.6", + "platform": "x64-mingw-ucrt" + }, + "corpus": { + "files": 5000, + "gems": 2, + "imports_per_file": 0, + "excluded_files": 0, + "dependency_analysis": "imports_and_constants" + }, + "graph_sha256": "124950cdd40283daaad7784b48515256398efb305d91350b4113bb233217c0b3", + "graph_edges": 10001, + "source_files": 5001, + "cache_reused": true, + "cold_seconds": 8.373332, + "discovery_seconds": 4.146034, + "warm_seconds": 0.004838, + "allocated_objects": 5209871, + "heap_growth_bytes": 23956611, + "peak_rss_kb": null + }, + { + "file": "sparse-5000-1-candidate.json", + "runtime": { + "ruby": "4.0.6", + "platform": "x64-mingw-ucrt" + }, + "corpus": { + "files": 5000, + "gems": 2, + "imports_per_file": 0, + "excluded_files": 0, + "dependency_analysis": "imports_and_constants" + }, + "graph_sha256": "124950cdd40283daaad7784b48515256398efb305d91350b4113bb233217c0b3", + "graph_edges": 10001, + "source_files": 5001, + "cache_reused": true, + "cold_seconds": 4.245376, + "discovery_seconds": 0.963274, + "warm_seconds": 0.00651, + "allocated_objects": 2627199, + "heap_growth_bytes": 29459619, + "peak_rss_kb": null + }, + { + "file": "sparse-5000-2-baseline.json", + "runtime": { + "ruby": "4.0.6", + "platform": "x64-mingw-ucrt" + }, + "corpus": { + "files": 5000, + "gems": 2, + "imports_per_file": 0, + "excluded_files": 0, + "dependency_analysis": "imports_and_constants" + }, + "graph_sha256": "124950cdd40283daaad7784b48515256398efb305d91350b4113bb233217c0b3", + "graph_edges": 10001, + "source_files": 5001, + "cache_reused": true, + "cold_seconds": 6.560909, + "discovery_seconds": 3.493604, + "warm_seconds": 0.006888, + "allocated_objects": 5209871, + "heap_growth_bytes": 29776691, + "peak_rss_kb": null + }, + { + "file": "sparse-5000-2-candidate.json", + "runtime": { + "ruby": "4.0.6", + "platform": "x64-mingw-ucrt" + }, + "corpus": { + "files": 5000, + "gems": 2, + "imports_per_file": 0, + "excluded_files": 0, + "dependency_analysis": "imports_and_constants" + }, + "graph_sha256": "124950cdd40283daaad7784b48515256398efb305d91350b4113bb233217c0b3", + "graph_edges": 10001, + "source_files": 5001, + "cache_reused": true, + "cold_seconds": 4.856391, + "discovery_seconds": 1.172176, + "warm_seconds": 0.005832, + "allocated_objects": 2627199, + "heap_growth_bytes": 29436899, + "peak_rss_kb": null + }, + { + "file": "sparse-5000-3-baseline.json", + "runtime": { + "ruby": "4.0.6", + "platform": "x64-mingw-ucrt" + }, + "corpus": { + "files": 5000, + "gems": 2, + "imports_per_file": 0, + "excluded_files": 0, + "dependency_analysis": "imports_and_constants" + }, + "graph_sha256": "124950cdd40283daaad7784b48515256398efb305d91350b4113bb233217c0b3", + "graph_edges": 10001, + "source_files": 5001, + "cache_reused": true, + "cold_seconds": 6.891356, + "discovery_seconds": 3.49325, + "warm_seconds": 0.002475, + "allocated_objects": 5209871, + "heap_growth_bytes": 30283315, + "peak_rss_kb": null + }, + { + "file": "sparse-5000-3-candidate.json", + "runtime": { + "ruby": "4.0.6", + "platform": "x64-mingw-ucrt" + }, + "corpus": { + "files": 5000, + "gems": 2, + "imports_per_file": 0, + "excluded_files": 0, + "dependency_analysis": "imports_and_constants" + }, + "graph_sha256": "124950cdd40283daaad7784b48515256398efb305d91350b4113bb233217c0b3", + "graph_edges": 10001, + "source_files": 5001, + "cache_reused": true, + "cold_seconds": 4.262591, + "discovery_seconds": 1.222559, + "warm_seconds": 0.004828, + "allocated_objects": 2627199, + "heap_growth_bytes": 26715499, + "peak_rss_kb": null + } + ] +} diff --git a/lib/archunit/extraction/enumerate_source_files.rb b/lib/archunit/extraction/enumerate_source_files.rb index d0af7d5..68fcdd2 100644 --- a/lib/archunit/extraction/enumerate_source_files.rb +++ b/lib/archunit/extraction/enumerate_source_files.rb @@ -1,15 +1,14 @@ # frozen_string_literal: true -require 'find' require 'pathname' require_relative '../error/technical_error' require_relative '../error/user_error' require_relative 'arch_ignore' +require_relative 'source_walker' require_relative '../common/fluentapi/check_options' module ArchUnit # Ruby-specific project discovery and source extraction. - # rubocop:disable-next Metrics/ModuleLength -- One source policy keeps graphs and metrics aligned. module Extraction DEFAULT_EXCLUDED_DIRECTORIES = %w[ .bundle @@ -64,16 +63,7 @@ def resolve_project_exclude_patterns(root, value, source_exclusions: []) private_class_method :resolve_project_exclude_patterns def source_files_under(root, exclude_patterns) - Find.find(root.to_s).filter_map do |entry| - stat = File.lstat(entry) - next if stat.symlink? - - if stat.directory? - Find.prune if excluded_path?(entry, root, exclude_patterns) - elsif ruby_source_file?(entry, stat) && !excluded_path?(entry, root, exclude_patterns) - relative_identifier(entry, root).freeze - end - end + SourceWalker.new(root, exclude_patterns).files end private_class_method :source_files_under @@ -110,11 +100,6 @@ def normalize_exclude_pattern(pattern) end private_class_method :normalize_exclude_pattern - def ruby_source_file?(entry, stat) - stat.file? && File.extname(entry) == '.rb' - end - private_class_method :ruby_source_file? - def source_root(value) value = value.to_path if value.respond_to?(:to_path) unless value.is_a?(String) && !value.empty? @@ -129,30 +114,5 @@ def source_root(value) raise TechnicalError, "could not enumerate Ruby source files: #{e.message}" end private_class_method :source_root - - def excluded_path?(entry, root, exclude_patterns) - return false if entry == root.to_s - - relative = relative_identifier(entry, root) - exclude_patterns.any? do |pattern| - exclude_pattern_matches?(pattern, relative, File.basename(entry)) - end - end - private_class_method :excluded_path? - - def exclude_pattern_matches?(pattern, relative, basename) - flags = File::FNM_DOTMATCH | File::FNM_PATHNAME - anchored = pattern.start_with?('/') - candidate = pattern.delete_prefix('/') - target = anchored || candidate.include?('/') ? relative : basename - - File.fnmatch?(candidate, target, flags) - end - private_class_method :exclude_pattern_matches? - - def relative_identifier(entry, root) - Pathname.new(entry).relative_path_from(root).to_s.tr('\\', '/') - end - private_class_method :relative_identifier end end diff --git a/lib/archunit/extraction/source_pattern_matcher.rb b/lib/archunit/extraction/source_pattern_matcher.rb new file mode 100644 index 0000000..f3a6289 --- /dev/null +++ b/lib/archunit/extraction/source_pattern_matcher.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +module ArchUnit + module Extraction + # Compiles discovery-invariant glob decisions once; matching itself is pure. + # @api private + class SourcePatternMatcher + FLAGS = File::FNM_DOTMATCH | File::FNM_PATHNAME + + def initialize(patterns) + @basenames = {} + @globs = [] + patterns.each { |pattern| compile(pattern) } + @basenames.freeze + @globs.freeze + freeze + end + + def excluded?(identifier, basename) + @basenames.key?(basename) || @globs.any? do |pattern, path_pattern| + File.fnmatch?(pattern, path_pattern ? identifier : basename, FLAGS) + end + end + + private + + def compile(pattern) + candidate = pattern.delete_prefix('/') + path_pattern = pattern.start_with?('/') || candidate.include?('/') + if !path_pattern && !candidate.match?(/[*?\[]/) + @basenames[candidate.freeze] = true + else + @globs << [candidate.freeze, path_pattern].freeze + end + end + end + private_constant :SourcePatternMatcher + end +end diff --git a/lib/archunit/extraction/source_walker.rb b/lib/archunit/extraction/source_walker.rb new file mode 100644 index 0000000..dc5cb14 --- /dev/null +++ b/lib/archunit/extraction/source_walker.rb @@ -0,0 +1,70 @@ +# frozen_string_literal: true + +require_relative 'source_pattern_matcher' + +module ArchUnit + module Extraction + # Pruned iterative traversal of an already canonical, validated project root. + # Identifiers come from child components, never unchecked absolute-prefix slicing. + # @api private + class SourceWalker + def initialize(root, patterns) + @root = root.to_s + @encoding = @root.encoding + @encoding = Encoding.find('filesystem') if @encoding == Encoding::US_ASCII + @matcher = SourcePatternMatcher.new(patterns) + end + + def files + files = [] + pending = [[@root, '']] + until pending.empty? + directory, prefix = pending.pop + collect_directory(directory, prefix, pending, files) + end + files + end + + private + + def collect_directory(directory, prefix, pending, files) + Dir.children(directory, encoding: @encoding).each do |name| + collect_child(directory, prefix, name, pending, files) + end + end + + def collect_child(directory, prefix, name, pending, files) + path = File.join(directory, name) + kind = source_kind(path, name) + return unless kind + + identifier = child_identifier(prefix, name) + return if @matcher.excluded?(identifier, name) + + collect_path(kind, path, identifier, pending, files) + end + + def source_kind(path, name) + stat = File.lstat(path) + return if stat.symlink? + return :directory if stat.directory? + + :source if stat.file? && File.extname(name) == '.rb' + end + + def child_identifier(prefix, name) + name = name.tr('\\', '/') + prefix.empty? ? name : "#{prefix}/#{name}" + end + + def collect_path(kind, path, identifier, pending, files) + if kind == :directory + pending << [path, identifier] + else + files << identifier.freeze + end + end + end + private_constant :SourceWalker + end +end diff --git a/lib/archunit/version.rb b/lib/archunit/version.rb index 0652c65..131f835 100644 --- a/lib/archunit/version.rb +++ b/lib/archunit/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module ArchUnit - VERSION = '1.1.0' + VERSION = '1.1.1' end diff --git a/spec/benchmark/extraction_spec.rb b/spec/benchmark/extraction_spec.rb index 9630c3d..ff4b909 100644 --- a/spec/benchmark/extraction_spec.rb +++ b/spec/benchmark/extraction_spec.rb @@ -51,6 +51,21 @@ def corpus_configuration(**overrides) expect(report).to include('graph_cache_reused' => true) expect(report.fetch('corpus')).to include('dependency_analysis' => 'imports_and_constants') expect(report.dig('cold', 'memory', 'ruby_heap_bytes')).to be_positive + expect(report.dig('cold', 'memory_growth', 'allocated_objects')).to be_positive + expect(report.fetch('graph_sha256')).to match(/\A[0-9a-f]{64}\z/) + expect(report.dig('retained', 'ruby_heap_bytes')).to be_positive + expect(report.dig('retained', 'ruby_heap_growth_bytes')).to be_a(Integer) + end + + it 'prunes malformed excluded trees while keeping the exact graph and evidence digest' do + configuration = described_class::DEFAULTS.merge(files: 6, gems: 2) + described_class::Corpus.new(@root, **corpus_configuration).generate + original = described_class::Runner.new(@root, configuration).run + described_class::Corpus.new(@root, **corpus_configuration, excluded_file_count: 20).generate + excluded = described_class::Runner.new(@root, configuration).run + expect(excluded.fetch(:graph_sha256)).to eq(original.fetch(:graph_sha256)) + expect(excluded.dig(:cold, :counters, :source_files)).to eq(7) + expect(excluded.fetch(:graph_cache_reused)).to be(true) end it 'supports an output-free run without threshold limits' do diff --git a/spec/benchmark/performance_study_spec.rb b/spec/benchmark/performance_study_spec.rb index 66a07da..b382a1f 100644 --- a/spec/benchmark/performance_study_spec.rb +++ b/spec/benchmark/performance_study_spec.rb @@ -6,6 +6,13 @@ require_relative '../../benchmark/study/run' RSpec.describe PerformanceStudy::Runner do + it 'keeps the isolated path-gem lock synchronized with the candidate version' do + lock = File.read(File.expand_path('../../benchmark/study/Gemfile.lock', __dir__)) + version = Regexp.escape(ArchUnit::VERSION) + expect(lock).to match(/^ archunit \(#{version}\)$/) + expect(lock).to match(/^ archunit \(#{version}\)$/) + end + it 'checks permitted, forbidden and changed-source policies across all shapes and densities' do results = described_class.run(pairs: 4, trials: 1, compare: false, startup: false) expect(results.length).to eq(8) diff --git a/spec/ci/discovery_study_spec.rb b/spec/ci/discovery_study_spec.rb new file mode 100644 index 0000000..f9b3d13 --- /dev/null +++ b/spec/ci/discovery_study_spec.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +require 'yaml' + +RSpec.describe 'the source-discovery study workflow' do + subject(:workflow) do + YAML.safe_load_file( + File.expand_path('../../.github/workflows/performance-study.yml', __dir__), aliases: true + ) + end + + let(:job) { workflow.fetch('jobs').fetch('discovery') } + + it 'runs only on explicit dispatch with read-only access and a bounded platform matrix' do + triggers = workflow.fetch('on') { workflow.fetch(true) } + expect(triggers.keys).to include('workflow_dispatch') + expect(job.fetch('if')).to eq("github.event_name == 'workflow_dispatch'") + expect(job.fetch('steps')[1].dig('with', 'ref')).to eq('v1.1.0') + expect(workflow.fetch('permissions')).to eq('contents' => 'read') + expect(job.fetch('timeout-minutes')).to eq(20) + expect(job.dig('strategy', 'matrix', 'os')) + .to contain_exactly('ubuntu-latest', 'windows-latest') + end + + it 'pins actions and disables checkout credentials for both production library revisions' do + actions = job.fetch('steps').filter_map { |step| step['uses'] } + expect(actions).to all(match(/@[a-f0-9]{40}\z/)) + checkouts = job.fetch('steps').select do |step| + step.fetch('uses', '').include?('actions/checkout') + end + expect(checkouts.length).to eq(2) + expect(checkouts).to all(include('with' => include('persist-credentials' => false))) + end + + it 'reuses one benchmark harness and checks semantic digests and cache/source counts per pair' do + run = job.fetch('steps').find { |step| step['run'] }.fetch('run') + expect(run).to include('Copy-Item', 'benchmark/extraction.rb', '--excluded-files') + expect(run).to include('graph_sha256', 'graph_cache_reused', 'source_files', '1..3') + expect(run).to include('sparse-5000', 'sparse-10000', 'imports-1000', 'excluded-1000') + expect(run).not_to include('bundle update', 'gem push', 'inputs.baseline') + upload = job.fetch('steps').find { |step| step.fetch('uses', '').include?('upload-artifact') } + expect(upload).to include('if' => 'always()', 'with' => include('path' => 'reports/*.json')) + end +end diff --git a/spec/extraction/enumerate_source_files_spec.rb b/spec/extraction/enumerate_source_files_spec.rb index 08178ac..06ca659 100644 --- a/spec/extraction/enumerate_source_files_spec.rb +++ b/spec/extraction/enumerate_source_files_spec.rb @@ -165,12 +165,41 @@ def create_file(relative_path, contents = '# fixture') end it 'wraps filesystem failures as technical errors' do - allow(Find).to receive(:find).and_raise(Errno::EACCES, @project_root.to_s) + allow(Dir).to receive(:children).and_raise(Errno::EACCES, @project_root.to_s) expect { described_class.enumerate_source_files(@project_root) } .to raise_error(ArchUnit::TechnicalError, /could not enumerate Ruby source files/) end + it 'reads each included entry status once, without a second traversal metadata pass' do + source = create_file('lib/source.rb') + directory = source.dirname.to_s + allow(File).to receive(:lstat).and_call_original + expect(File).to receive(:lstat).with(source.to_s).once.and_call_original + expect(File).to receive(:lstat).with(directory).once.and_call_original + + expect(described_class.enumerate_source_files(@project_root)).to eq(['lib/source.rb']) + end + + it 'constructs child identifiers without repeated relative-path calculations' do + create_file('lib/nested/source.rb') + expect_any_instance_of(Pathname).not_to receive(:relative_path_from) + + expect(described_class.enumerate_source_files(@project_root)).to eq(['lib/nested/source.rb']) + end + + it 'does not open or stat descendants of an excluded tree' do + create_file('vendor/deep/hidden.rb') + create_file('keep.rb') + allow(Dir).to receive(:children).and_call_original + allow(File).to receive(:lstat).and_call_original + expect(Dir).not_to receive(:children).with(@project_root.join('vendor').to_s, any_args) + expect(File).not_to receive(:lstat).with(@project_root.join('vendor/deep').to_s) + expect(File).not_to receive(:lstat).with(@project_root.join('vendor/deep/hidden.rb').to_s) + + expect(described_class.enumerate_source_files(@project_root)).to eq(['keep.rb']) + end + it 'exposes a frozen default exclusion vocabulary' do expect(described_class::DEFAULT_EXCLUDED_DIRECTORIES).to include( '.git', diff --git a/spec/extraction/source_walker_spec.rb b/spec/extraction/source_walker_spec.rb new file mode 100644 index 0000000..4130882 --- /dev/null +++ b/spec/extraction/source_walker_spec.rb @@ -0,0 +1,180 @@ +# frozen_string_literal: true + +require 'fileutils' +require 'find' +require 'tmpdir' + +RSpec.describe 'Pruned source discovery' do + around do |example| + Dir.mktmpdir('archunit-walk-oracle') do |directory| + @root = Pathname.new(directory).realpath + example.run + end + end + + def write_source(name) + file = @root.join(name) + file.dirname.mkpath + file.write("raise 'source must not execute'\n") + end + + def legacy_sources(patterns) + Find.find(@root.to_s).filter_map { |entry| legacy_source(entry, patterns) }.sort + end + + def legacy_source(entry, patterns) + stat = File.lstat(entry) + return if stat.symlink? || entry == @root.to_s + + relative = Pathname.new(entry).relative_path_from(@root).to_s.tr('\\', '/') + excluded = legacy_excluded?(patterns, relative, File.basename(entry)) + Find.prune if stat.directory? && excluded + relative if legacy_source_file?(entry, stat) && !excluded + end + + def legacy_source_file?(entry, stat) + stat.file? && File.extname(entry) == '.rb' + end + + def legacy_excluded?(patterns, relative, name) + patterns.any? do |pattern| + candidate = pattern.delete_prefix('/') + target = pattern.start_with?('/') || candidate.include?('/') ? relative : name + File.fnmatch?(candidate, target, File::FNM_DOTMATCH | File::FNM_PATHNAME) + end + end + + it 'matches the previous Find implementation across mixed tree and exclusion shapes' do + names = %w[root.rb .rb upper.RB .hidden/kept.rb vendor/ignored.rb nested/vendor/ignored.rb + lib/.private.rb lib/generated/source.rb lib/a_model.rb lib/b[1].rb + lib/ümlaut.rb looks.rb/child.rb lib/README] + ['lib/space name.rb'] + names.each { |name| write_source(name) } + @root.join('.archignore').write("lib/generated/\n") + [nil, [], ['/vendor'], ['vendor'], ['**/*_model.rb'], ['lib/[ab]*.rb'], + ['lib\\generated\\', '.hidden'], ['**/.private.rb'], ['looks.rb']].each do |patterns| + discovery = ArchUnit::Extraction.discover_source_files(@root, exclude_patterns: patterns) + expect(discovery.files).to eq(legacy_sources(discovery.patterns)) + end + end + + it 'prunes an excluded large tree even when none of its files are readable Ruby' do + 120.times { |index| write_source("vendor/deep/tree/file_#{index}.rb") } + write_source('lib/kept.rb') + allow(Dir).to receive(:children).and_call_original + expect(Dir).not_to receive(:children).with(@root.join('vendor').to_s, any_args) + expect(ArchUnit::Extraction.enumerate_source_files(@root)).to eq(['lib/kept.rb']) + end + + [Errno::EACCES, Errno::ENOENT, Errno::ENOTDIR].each do |error| + it "fails closed when an included directory listing raises #{error}" do + write_source('lib/source.rb') + allow(Dir).to receive(:children).and_call_original + allow(Dir).to receive(:children).with(@root.join('lib').to_s, any_args).and_raise(error) + expect { ArchUnit::Extraction.enumerate_source_files(@root) } + .to raise_error(ArchUnit::TechnicalError, /could not enumerate Ruby source files/) + end + end + + it 'fails closed if an entry disappears after listing instead of inventing a clean snapshot' do + write_source('source.rb') + allow(File).to receive(:lstat).and_call_original + allow(File).to receive(:lstat).with(@root.join('source.rb').to_s).and_raise(Errno::ENOENT) + expect { ArchUnit::Extraction.enumerate_source_files(@root) } + .to raise_error(ArchUnit::TechnicalError, /could not enumerate Ruby source files/) + end + + it 'never enters symlinked directories, including portable mocked-link controls' do + write_source('alias/hidden.rb') + write_source('keep.rb') + link = @root.join('alias').to_s + status = File.lstat(link) + allow(status).to receive(:symlink?).and_return(true) + allow(File).to receive(:lstat).and_call_original + allow(File).to receive(:lstat).with(link).and_return(status) + allow(Dir).to receive(:children).and_call_original + expect(Dir).not_to receive(:children).with(link, any_args) + expect(ArchUnit::Extraction.enumerate_source_files(@root)).to eq(['keep.rb']) + end + + it 'ignores nonregular entries without treating them as source or directories' do + write_source('pipe.rb') + status = instance_double(File::Stat, symlink?: false, directory?: false, file?: false) + allow(File).to receive(:lstat).and_call_original + allow(File).to receive(:lstat).with(@root.join('pipe.rb').to_s).and_return(status) + expect(ArchUnit::Extraction.enumerate_source_files(@root)).to eq([]) + end + + ['C:/MixedCase/project', '//server/share/project'].each do |root| + it "builds relative identifiers from children under #{root} without prefix slicing" do + directory = instance_double(File::Stat, symlink?: false, directory?: true) + source = instance_double(File::Stat, symlink?: false, directory?: false, file?: true) + library = File.join(root, 'lib') + allow(Dir).to receive(:children).with(root, any_args).and_return(['lib']) + allow(Dir).to receive(:children).with(library, any_args).and_return(['Ünicode.rb']) + allow(File).to receive(:lstat).with(library).and_return(directory) + allow(File).to receive(:lstat).with(File.join(root, 'lib', 'Ünicode.rb')).and_return(source) + walker = ArchUnit::Extraction.const_get(:SourceWalker).new(root, []) + expect(walker.files).to eq(['lib/Ünicode.rb']) + end + end + + it 'uses filesystem encoding for an ASCII-encoded root without losing Unicode child names' do + root = @root.to_s.dup.force_encoding(Encoding::US_ASCII) + source = instance_double(File::Stat, symlink?: false, directory?: false, file?: true) + encoding = Encoding.find('filesystem') + allow(Dir).to receive(:children).with(root, encoding:).and_return(['Ünicode.rb']) + allow(File).to receive(:lstat).with(File.join(root, 'Ünicode.rb')).and_return(source) + expect(ArchUnit::Extraction.const_get(:SourceWalker).new(root, []).files).to eq(['Ünicode.rb']) + end + + it 'walks a deep synthetic tree iteratively without depending on native path-length limits' do + root = @root.to_s + depth = 1200 + directory = instance_double(File::Stat, symlink?: false, directory?: true) + source = instance_double(File::Stat, symlink?: false, directory?: false, file?: true) + allow(Dir).to receive(:children) do |path, **_options| + path.count('/') - root.count('/') < depth ? ['d'] : ['leaf.rb'] + end + allow(File).to receive(:lstat) { |path| path.end_with?('.rb') ? source : directory } + walker = ArchUnit::Extraction.const_get(:SourceWalker).new(root, []) + expect(walker.files).to eq(["#{'d/' * depth}leaf.rb"]) + end + + it 'retains canonical root validation for a symlinked project root' do + write_source('lib/kept.rb') + Dir.mktmpdir('archunit-root-alias') do |directory| + link = File.join(directory, 'alias') + File.symlink(@root, link) + expect(ArchUnit::Extraction.enumerate_source_files(link)).to eq(['lib/kept.rb']) + end + rescue NotImplementedError, Errno::EACCES + skip 'creating symlinks is not available for this user or platform' + end +end + +RSpec.describe 'Compiled source exclusion matching' do + it 'matches legacy literal, anchored, recursive, Unicode and malformed globs' do + patterns = %w[vendor Vendor /vendor **/generated/** *.rb .hidden lib/[ab]?.rb lib/ümlaut.rb + a{b,c} bad[ x] /lib/**/*] + paths = %w[vendor Vendor VENDOR nested/vendor keep.rb .hidden lib/ab.rb lib/ümlaut.rb + lib/Upper.rb a{b,c} bad[ x]] + patterns.each do |pattern| + matcher = ArchUnit::Extraction.const_get(:SourcePatternMatcher).new([pattern]) + paths.each do |path| + candidate = pattern.delete_prefix('/') + target = pattern.start_with?('/') || candidate.include?('/') ? path : File.basename(path) + expected = File.fnmatch?(candidate, target, File::FNM_DOTMATCH | File::FNM_PATHNAME) + expect(matcher.excluded?(path, File.basename(path))).to eq(expected) + end + expect(matcher).to be_frozen + end + end + + it 'does not invoke glob matching for exact default basenames' do + type = ArchUnit::Extraction.const_get(:SourcePatternMatcher) + matcher = type.new(ArchUnit::Extraction::DEFAULT_EXCLUDED_DIRECTORIES) + expect(File).not_to receive(:fnmatch?) + expect(matcher.excluded?('nested/vendor', 'vendor')).to be(true) + expect(matcher.excluded?('lib/source.rb', 'source.rb')).to be(false) + end +end