Docs navigation

Guides / design / graph-metrics-design · Edit on GitHub

Graph Centrality & Network Metrics — Engineering Design

This document specifies the Macro Graph Centrality, Network Metrics, and Git Churn Hotspot subsystem in rgctl (crates/rgctl-analysis/src/centrality.rs, centrality_approx.rs, git_churn.rs, results.rs, and src/cli/metrics.rs). It covers pull-based CSR FastPageRank, exact and degree-stratified SampledBetweenness ($K = 512$), exact-bitset and HyperLogLog HyperBallHarmonic ($p \in {14, 12, 10}$), NodeRole filtering, and columnar .rgctl/analysis_results.bin persistence.

Functions tab — centrality columns (gbuilder)

Figure 1: Functions tab — sortable PageRank (PR), betweenness (BC), harmonic (Harm), and blast columns over the function inventory.


1. System Overview & End-to-End Data Flow

┌──────────────────────────────────────────────────────────────────────────────┐
│ 1. ZERO-COPY CSR PROJECTION (`FlatGraphIndex` / `CodeGraphCsr`)              │
│                                                                              │
│  `graph.snapshot.bin` (`RGCL` v2)                                            │
│    ├─► Call-only CSR (`EdgeType::Calls`)                                     │
│    ├─► Structural CSR (`Calls`, `Imports`, `Inherits`, `Implements`,         │
│    │     `References`, `Uses`)                                               │
│    └─► `NodeRole` table (`Production`, `Test`, `Generated`, `NonCallable`)   │
└───────────────────────────────────────┬──────────────────────────────────────┘
                                        │
                                        ▼
┌──────────────────────────────────────────────────────────────────────────────┐
│ 2. PARALLEL CENTRALITY & CHURN PASSES (`crates/rgctl-analysis`)              │
│                                                                              │
│  • `FastPageRank`:                                                           │
│      - Pull-based CSR power iteration (`d = 0.85`, `PARALLEL_THRESHOLD=4096`)│
│      - `V <= 500,000`: 20 iterations, `tol = 1e-6`                           │
│      - `V >  500,000`:  8 iterations, `tol = 1e-4`                           │
│  • `Betweenness`:                                                            │
│      - `V <= 500` (`DEFAULT_EXACT_CENTRALITY_LIMIT`): exact Brandes O(V*E)   │
│      - `V >  500`: `SampledBetweenness` (`DEFAULT_SAMPLE_PIVOTS = 512`)      │
│  • `HyperBallHarmonic` (opt-in via `discover --with-harmonic`):              │
│      - `V <= 8,192` (`HYPERBALL_EXACT_THRESHOLD`): exact 64-bit word BitSet  │
│      - `V >  8,192`: parallel HyperLogLog (`p = 14 / 12 / 10`, 16/8 rounds)  │
│  • `GitChurn` (opt-in via `discover --with-git`):                            │
│      - Exponential recency decay + `CoChangesWith` edges + hotspot score     │
└───────────────────────────────────────┬──────────────────────────────────────┘
                                        │
                                        ▼
┌──────────────────────────────────────────────────────────────────────────────┐
│ 3. COLUMNAR PERSISTENCE & CONSUMERS                                          │
│                                                                              │
│  • `.rgctl/analysis_results.bin` (`AnalysisResults::CentralityTable`)        │
│  • `.rgctl/analysis/git_churn.json` (`--with-git`)                           │
│  • CLI: `rgctl metrics --pagerank --betweenness --communities --churn`       │
│  • Dashboard: `function_metrics.json` (`FunctionsView.tsx`, `MigrationView`) │
└──────────────────────────────────────────────────────────────────────────────┘

2. Theoretical Foundations Mapped to rgctl

ConceptTheoretical OriginHow rgctl Applies It in Rust
Pull-Based CSR PageRankPage, Brin, Motwani & Winograd (1998); Gargiulo et al. (2015), Componentwise PageRankFastPageRank::compute_flat precomputes inv_out_deg[u] and pulls incoming neighbor contributions across contiguous CSR slices (rev_offsets, rev_sources), redistributing dangling-node mass uniformly each round and filtering hotspots by NodeRole::Production unless --include-tests is passed.
Brandes Betweenness & Degree-Stratified Pivot SamplingBrandes (2001), A Faster Algorithm for Betweenness Centrality; Geisberger, Sanders & Schultes (2008), Better Approximation of Betweenness CentralityCentralityCalculator runs exact Brandes for $V \le 500$ (DEFAULT_EXACT_CENTRALITY_LIMIT = 500) and SampledBetweenness with $K = 512$ (DEFAULT_SAMPLE_PIVOTS = 512) degree-stratified pivots (50% highest total-degree hubs + 50% deterministic stride) scaled by $N/K$ for $V > 500$.
HyperBall Harmonic CentralityBoldi & Vigna (2013), In-Core Computation of Geometric Centralities with HyperBall: A Hundred Billion Nodes and BeyondHyperBallHarmonic evaluates $H(v) = \sum_{d=1}^{R} \frac{
Temporal Change Coupling & Churn HotspotsTornhill (2015), Your Code as a Crime Scene; Graves et al. (2000), Predicting Fault Incidence Using Software Change Historygit_churn.rs streams git log over --git-since (default 90.days), weights commit frequency by exponential recency decay $\times \log_2(1 + \text{cyclomatic_complexity})$, and emits EdgeType::CoChangesWith edges between files modified together.

3. Binary Layout, Data Structures & Memory Discipline

3.1 Verified Centrality Constants (centrality.rs & centrality_approx.rs)

ConstantValueMeaning
PARALLEL_THRESHOLD4_096Minimum graph size $V$ before Rayon parallel iterators activate (par_iter).
LARGE_GRAPH_PAGERANK_NODE_LIMIT500_000Threshold above which FastPageRank switches from (max_iter = 20, tol = 1e-6) to (max_iter = 8, tol = 1e-4).
DEFAULT_EXACT_CENTRALITY_LIMIT500Maximum node count $V$ for exact $O(V \cdot E)$ Brandes betweenness before switching to SampledBetweenness.
DEFAULT_SAMPLE_PIVOTS ($K$)512Number of pivot sources sampled by SampledBetweenness when $V > 500$.
HYPERBALL_EXACT_THRESHOLD8_192Maximum node count $V$ for exact 64-bit word BitSet harmonic ball propagation (hyperball_exact).
HYPERLOGLOG_PRECISION ($p$)14 (12 / 10 adaptive)HyperLogLog register precision ($m = 2^p$ bytes per node) when $V > 8,192$.
DEFAULT_HYPERBALL_ROUNDS16 (8 when $V > 500,000$)Ball expansion rounds (LARGE_GRAPH_HYPERBALL_ROUNDS = 8 above LARGE_GRAPH_HYPERBALL_NODE_LIMIT = 500_000).

3.2 Columnar Persistence (.rgctl/analysis_results.bin)

Defined in crates/rgctl-analysis/src/results.rs:

  • Stores flat Vec<f64> / Vec<f32> columns indexed by dense node index (0..V) inside CentralityTable (pagerank, betweenness, harmonic, degree) rather than HashMap<Uuid, f64>, eliminating per-node hash-table overhead on 1M+ node repositories.

4. CLI Contract, JSON Schema & Implementation Map

4.1 CLI Commands

Verified against rgctl metrics --help and src/cli/metrics.rs:

# Discover with optional harmonic centrality and git churn / co-change edges
rgctl discover . --with-harmonic --with-git --git-since 90.days

# Query all core graph metrics (PageRank, betweenness, communities)
rgctl metrics -f json

# Query PageRank with custom iteration count or including test/generated symbols
rgctl metrics --pagerank --iterations 50 -f json
rgctl metrics --pagerank --include-tests -f json

# Query betweenness centrality or Fast-Leiden community modularity
rgctl metrics --betweenness -f json
rgctl metrics --communities -f json

# Query git churn hotspots and co-changing files (requires `discover --with-git`)
rgctl metrics --churn -f json

4.2 JSON Schema (rgctl metrics --pagerank --betweenness -f json)

{
  "schema_version": "1.0",
  "pagerank": {
    "top_nodes": [
      {
        "node": "4f8a12c0-3b11-5e99-8100-aabbccddeeff",
        "name": "OrderRepository::save",
        "kind": "method",
        "qualified_name": "com.example.OrderRepository::save",
        "file_path": "src/OrderRepository.java",
        "pagerank": 0.014821
      }
    ]
  },
  "betweenness": {
    "top_nodes": [
      {
        "node": "9c2100a1-1122-5334-9900-112233445566",
        "name": "OrderService::processOrder",
        "kind": "method",
        "file_path": "src/OrderService.java",
        "betweenness": 142.5
      }
    ]
  }
}

4.3 Implementation Map

ComponentSource FileKey Types & Functions
PageRank & Exact Centralitycrates/rgctl-analysis/src/centrality.rsFastPageRank, PageRankConfig, NodeRole, CentralityCalculator
Approximate Betweenness & Harmoniccrates/rgctl-analysis/src/centrality_approx.rsSampledBetweenness, HyperBallHarmonic, ApproximateCentrality
Git Churn & Co-Change Analysiscrates/rgctl-analysis/src/git_churn.rsGitChurnAnalyzer, FileChurnMetrics
Columnar Results Tablecrates/rgctl-analysis/src/results.rsAnalysisResults, CentralityTable, CommunityTable
CLI Metrics Commandsrc/cli/metrics.rs, src/cli/metrics_output.rsMetricsArgs, run, collect_churn_hotspots

5. Performance Guardrails & Verification Checklist

  1. Harmonic Centrality Is Opt-In (--with-harmonic): Because HyperBallHarmonic allocates HLL register banks across $V$ vertices, it is off on default cold discover and enabled via --with-harmonic or --full.
  2. Large-Repo Dashboard Sparse Mode: When $V \ge 50,000$, function_metrics.json writes sparse_mode: "community_only" so the dashboard loads metrics via WASM + package metagraph aggregates without writing multi-hundred-megabyte JSON arrays.

Verification Commands

# Run centrality unit and audit integration tests
cargo test -p rgctl-analysis centrality
cargo test --test centrality_audit

# Run Criterion centrality microbenchmarks
cargo bench -p rgctl-analysis --bench centrality_benchmarks

6. Known Limitations & By-Design Boundaries

  1. Active-Pivot Sampling on Heterogeneous Graphs (#187): SampledBetweenness currently draws $K = 512$ pivots from $0..V$ before filtering out 0-out-degree nodes (File, Import, Field, leaf functions); restricting the candidate pool to active $out_deg > 0$ vertices is tracked in OpenSpec change sampled-betweenness-active-pivots.
  2. Git Churn Requires Local Git History: rgctl discover --with-git requires a non-shallow .git commit history covering the --git-since window; shallow CI clones (--depth 1) yield single-commit churn scores.