packages feed

canontra-0.4.0.0: BENCHMARKS.md

# Canontra Performance Benchmarks

Empirical Evaluation, Latency Measurements, and Algorithmic Complexity  
**Version**: `v0.4.0.0` (Release `v0.4.0`) Ultra-Low-Latency Pipeline, Accuracy Hardening & `canontra snap`  
**Test Environment**: x86_64, Windows 11 (NTFS), GHC 9.6.6 with `-O2` optimizations  
**Repository**: <https://github.com/symtrace/canontra>  
**Raw CSV Dataset**: [researchBenchmarks/v0.4.0_microbench_results.csv](researchBenchmarks/v0.4.0_microbench_results.csv)

## 1. Overview and Benchmarking Methodology

This document details empirical microbenchmark results for Canontra `v0.4.0` across synthetic modules (`Micro` ~25 LOC to `XL` ~4,005 LOC), polyglot frontends, ByteString/SIMD scanners, unboxed Compressed Sparse Row (CSR) graphs, `CNTR\x06` / `CNTR\x07` incremental binary slab caches, bound $\alpha$-renaming canonicalization, algebraic equivalence normalizers, repository-scale Merkle DAG trees, and `canontra snap` AI harness kernels.

All 159 microbenchmarks (`RQ1`–`RQ19`) were measured using `tasty-bench` (`--timeout 2`) under GHC 9.6.6 with `-O2` optimization. Unlike earlier releases where certain lazy AST structures could stop at WHNF, `v0.4.0` enforces strict full-spine `NFData` (`nf`) evaluation across every benchmark.

Pipeline stages isolated in `bench/Bench.hs`:

* **Stage 1**: 256-bit SIMD/SWAR FastScan (`Canontra.Canonical.SIMDScan`, `Canontra.Canonical.FastScan`) scanning UTF-8 and CRLF line endings.
* **Stage 2**: ByteString/SIMD Direct-to-IR parsing into Flat Linear Arenas (`Canontra.IR.Arena`, `Canontra.Parser.FastPython`, `Python`, `JS`, `Go`, `Rust`).
* **Stage 3**: Scope-aware language-specific bound $\alpha$-canonicalization (`Canontra.Normalize.Alpha`) and purity/IEEE-754 guarded algebraic normal form (`Canontra.Normalize.Algebraic`).
* **Stage 4**: CFG dominator-tree (`computeDominators`) and Reverse Post-Order (`canonicalizeCFGRPO`) canonicalization (`Canontra.Analysis.CFGCanonical`).
* **Stage 5**: ABI-precise structural type contracts (`Canontra.Analysis.TypeContract`) and single-pass `CanonicalProgram` fusion (`Canontra.Normalize.Normalize`).
* **Stage 6**: Unboxed CSR graphs (`Canontra.Analysis.CSRGraph`), per-file `FileGraphSummary` synthesis (`Canontra.Analysis.WholeRepoGraph`), and `CNTR\x07` (`Canontra.Cache.SlabV7`) persistence.
* **Stage 7**: Stateless AI harness delta computation and guard kernels (`Canontra.Harness.SubVersion`, `Canontra.Harness.Guard`).

## 2. Pipeline Stage Latency across File Scales (RQ1)

Measurements across five Python scale tiers:

* **Micro**: ~25 LOC (2 functions)
* **Small**: ~85 LOC (10 functions)
* **Medium**: ~405 LOC (50 functions)
* **Large**: ~1,605 LOC (200 functions)
* **Monolithic (XL)**: ~4,005 LOC (500 functions)

| Pipeline Stage | Micro (~25 LOC) | Small (~85 LOC) | Medium (~405 LOC) | Large (~1,605 LOC) | XL (~4,005 LOC) | Complexity |
| :--- | :---: | :---: | :---: | :---: | :---: | :--- |
| **AST Parsing (`parsePythonSource`)** | 274.7 us | 1.33 ms | 7.52 ms | 55.47 ms | 148.44 ms | $O(N)$ token stream |
| **AST Norm & Serialize** | 80.9 us | 378.4 us | 1.98 ms | 14.65 ms | 28.13 ms | $O(N)$ AST nodes |
| **Scope Analysis (XL)** | — | — | — | — | 11.04 ms | $O(V)$ scope tree |
| **Call Graph Extraction (XL)** | — | — | — | — | 891.1 us | $O(V + E)$ calls |
| **Control-Flow Graph (XL)** | — | — | — | — | 2.81 ms | $O(B + E)$ blocks |
| **Data-Flow Graph (XL)** | — | — | — | — | 25.39 ms | $O(D + U)$ SSA vars |
| **F0 Raw Bytes Hash (XL)** | — | — | — | — | 689.7 us | $O(N)$ SHA-256 |
| **F4 Composite Hash** | — | — | — | — | 119.0 us | $O(1)$ 7-tier fold |
| **Full 8-Tier Pipeline (`computeBundle`)** | **2.17 ms** | **3.96 ms** | **23.83 ms** | **110.94 ms** | **226.56 ms** | $O(N)$ linear |

## 3. Polyglot Ingestion & ByteString/SIMD Parsers (RQ2 & RQ7)

### Complete End-to-End Pipeline (`computeBundle`, ~100 LOC per file)

| Language | Standard / Conformance | Latency (Mean) | `2*Stdev` | AST Memory Model |
| :--- | :--- | :---: | :---: | :--- |
| **Python** | 3.8–3.12 (PEP 701, PEP 695, PEP 572) | 3.86 ms | ± 2.42 ms | Direct-to-IR + Normalized Arena |
| **TypeScript / JS** | ES2024 / TS 5.2 (`using`, JSX, regex lookahead) | 1.76 ms | ± 772.1 us | Direct-to-IR |
| **Go** | Go 1.18–1.21+ (Generics, Tilde Sets, Receivers) | 1.83 ms | ± 881.3 us | Direct-to-IR |
| **Rust** | Rust 2021 (GATs, Raw Identifiers, Macros) | 1.40 ms | ± 986.6 us | Direct-to-IR |

### ByteString-Native & SIMD Parser Kernels (~85 LOC `Small` Tier)

| Parser Kernel | Mean Latency | `2*Stdev` | Notes |
| :--- | :---: | :---: | :--- |
| **`parseGoByteString` (`Go BS`)** | 95.4 us | ± 61.7 us | ByteString UTF-8 parser (`Go Parser Small`: `88.5 us`) |
| **`parseJSByteString` (`JS/TS BS`)** | 137.3 us | ± 21.0 us | ByteString UTF-8 lexer (`TS Parser Small`: `126.6 us`) |
| **`parseRustByteString` (`Rust Small BS`)** | 132.8 us | ± 130.1 us | ByteString UTF-8 lexer (`Rust Small`: `99.2 us`) |
| **`parseFastPythonByteString` (`FastPython BS`)** | 463.9 us | ± 438.7 us | Hybrid indent-stack SWAR parser (`Python Parser Small`: `1.68 ms`) |
| **`parsePolyglotByteString` (Py / TS)** | 628.7 us / 152.6 us | ± 875.4 / ± 89.2 us | Unified polyglot dispatch |

## 4. Local Slab Cache (`CNTR\x07` & `CNTR\x06`) Benchmarks (RQ14 & RQ18)

Canontra `v0.4.0` introduces `CNTR\x07` (`Canontra.Cache.SlabV7`), storing full 9-tier bundles and compact binary `FileGraphSummary` records in 4KB CRC-32C protected slab pages:

### RQ18: `v0.4.0` `CNTR\x07` SlabV7 & Summary Graph Synthesis (10 Modules)

| Operation | Mean Latency | `2*Stdev` | Benefit vs Full Cold Re-Parse |
| :--- | :---: | :---: | :--- |
| **`encodeSlabV7Binary` (10 modules)** | 671.4 us | ± 902.7 us | Encodes bundles + `FileGraphSummary` |
| **`decodeSlabV7Binary` (10 modules)** | 216.7 us | ± 205.7 us | Restores full cache + graph summaries |
| **`lookupSlabV7BinaryBS` (Warm Hit)** | **6.0 us** | ± 3.4 us | Direct ByteString Radix lookup |
| **`buildWholeRepoCallGraphFromSummaries` (10 mods)** | **112.9 us** | ± 34.6 us | **87.3× faster** than full-AST `buildCSRCallGraph` (`9.86 ms`) |
| **`buildWholeRepoDataFlowFromSummaries` (10 mods)** | **65.6 us** | ± 20.7 us | **180.2× faster** than full-AST `buildCSRDataFlow` (`11.82 ms`) |

### RQ14: `CNTR\x06` Slab & CSR Binary Reference Kernels (1,000 Files / 500 Edges)

* **`encodeSlabV6Binary` (1,000 files)**: `16.60 ms`
* **`decodeSlabV6Binary` (1,000 files)**: `4.39 ms`
* **`lookupSlabBinaryBS` (Zero-Copy Warm Hit)**: `2.0 us`
* **`encodeCSRGraph` / `decodeCSRGraph`**: `25.0 us` / `232 ps`

## 5. Unboxed CSR Graph Engine & Chase-Lev Parallelism (RQ13 & RQ15)

| Operation | Mean Latency | `2*Stdev` | Algorithmic Complexity |
| :--- | :---: | :---: | :--- |
| **`csrHasEdge` Binary Search (Hit)** | **21.0 ns** | ± 3.4 ns | $O(\log(\text{deg}(u)))$ |
| **`forwardReachabilityCone` (100 nodes)** | 4.2 us | ± 1.5 us | $O(V + E)$ |
| **`tarjanSCC` Cycle Collapse** | 27.1 us | ± 27.4 us | $O(V + E)$ |
| **`transposeCSR` Matrix Inversion** | 50.7 us | ± 71.8 us | $O(V + E)$ |
| **`condenseSCC` Canonical DAG** | 57.2 us | ± 44.6 us | $O(V + E)$ |
| **`buildCSRGraph` (100 nodes, 500 edges)** | 108.3 us | ± 116.3 us | $O(E \log E)$ |
| **`Chase-Lev Deque` Push/Pop/Steal** | 13.5 us | ± 6.0 us | Lock-free `MV.IOVector` circular buffer |
| **`parProcessWorkStealing` (1,000 tasks)** | 1.14 ms | ± 386.1 us | Work-stealing multi-core scheduler |
| **`spliceCSREdges` Localized Invalidation** | 55.7 us | ± 27.4 us | Localized CSR matrix delta |

## 6. Semantic Normalizers & Batch CIA 2.0 (RQ16 & RQ17, Strict `NFData`)

| Operation | Scale | Mean Latency | `2*Stdev` | Notes |
| :--- | :--- | :---: | :---: | :--- |
| **`canonicalizeAlphaProgram`** | Medium (~405 LOC) | 518.8 us | ± 356.8 us | Language-specific de Bruijn local renaming |
| **`canonicalizeAlphaProgram`** | XL (~4,005 LOC) | 11.33 ms | ± 2.65 ms | Full deep `NFData` spine evaluation |
| **`canonicalizeCFGRPO`** | XL (~4,005 LOC) | 20.02 ms | ± 23.18 ms | Dominator + RPO basic-block relabeling |
| **`normalizeAlgebraicProgram`** | Medium (~405 LOC) | 903.3 us | ± 1.10 ms | Pure commutative + IEEE-754 guarded AST rewrite |
| **`normalizeAlgebraicProgram`** | XL (~4,005 LOC) | 9.38 ms | ± 4.84 ms | Full deep `NFData` spine evaluation |
| **`computeBatchImpactSliceCSR`** | PR Batch Changeset | **164.8 us** | ± 130.1 us | Multi-source CSR reachability slice |
| **`computeImpactSliceCSR`** | 10 Modules | **241.1 us** | ± 82.9 us | Exact impacted test runner synthesis |

## 7. Stateless AI Harness Kernels (`canontra snap` - Alpha, RQ19)

| Harness Kernel | Mean Latency | `2*Stdev` | Role |
| :--- | :---: | :---: | :--- |
| **`diffFileBundles` (9-tier bundle comparison)** | **733.1 ns** | ± 588.4 ns | Computes mutated/invariant tiers & severity |
| **`computeFileDeltas` (1,000 files, 1 dirty)** | **1.73 ms** | ± 1.21 ms | Diffs two 1,000-file snapshot manifests |
| **`classifySnapDeltas` (dirty delta triage)** | **19.9 ns** | ± 3.4 ns | Classifies turn as `no_change`/`semantic_noop`/`internal_logic`/`contract_change` |
| **`parseGuardTiers` (`F2,F3,F_T`)** | **733.1 ns** | ± 241.8 ns | Parses `--forbid` policy flags |
| **`matchesGlob` (`**/*.py`)** | **5.9 us** | ± 7.9 us | Pure POSIX glob matching for `--allow-paths` |

For end-to-end multi-tool wall-clock comparisons across all 15 open-source repositories, see [benchmarkReport.md](benchmarkReport.md).