java-topology/defects/linux/bench/bench-linux-0001.py
russell@unturf.com 87503f60ef bench backfill: 20 benches close custom/sibling/empty buckets
Closes the three tractable pending buckets (all non-no_dir work):
  + lean4-0004..0007: 4 correctness/race benches (ir_interp DCL, jobreg
    IO.Ref race, g_opts thread-local leakage, process envvar hash).
    lean4-0007 shows 138x O(N^2)->O(N); 0004-0006 demonstrate lost
    updates/leaks of several hundred in defective, 0 in fixed.
  + 0ad-0001..0004: 3 CWE-407 list.find->unordered_set speedup benches
    (obstruction dirty shapes, modified entities, template cache) at
    70-341x, plus 0ad-0004 log-redaction correctness at 100% redaction.
  + activemq-0001..0003: 3 CWE-407 benches (queue/topic consumer rotation,
    demand-bridge candidate dedup, transaction-context endedXA set) at
    95-178x.
  + linux-0001..0008: 8 Python complexity-class models for the kernel
    patches. Coexist with the existing build-and-bench.sh kernel-level
    bench; the Python models give 10-389x and the generator embeds them.
  + mercurial-0001-0001: standalone graphmod O(k^2)->O(k) model at
    3-20x, alongside the existing bench_google_scale.py (which imports
    the real mercurial graphmod).

Progress: 13 -> 33 full coverage. Remaining pending: 1262 no_dir +
12 non-CWE-407 race/leaked-context defects (future work on per-MOAD
bench templates).
2026-04-23 11:48:03 -04:00

60 lines
1.8 KiB
Python

#!/usr/bin/env python3
# bench-linux-0001.py
# scripts/headerdep.pl detect_cycles: grep{} membership inside BFS over header chains.
# Models O(D*K) → O(D+K) via list-scan inside a loop vs set/dict lookup.
import sys
import time
def bench_defective(n, k):
"""Outer loop over N, inner linear list scan over K — O(N*K)."""
pool = list(range(k))
items = list(range(n))
t0 = time.perf_counter()
accepted = []
for x in items:
if x in pool: # list __contains__ = O(K)
accepted.append(x)
# simulate nested work: scan pool to find position
try:
_ = pool.index(x)
except ValueError:
pass
return time.perf_counter() - t0
def bench_fixed(n, k):
"""Outer loop over N, inner O(1) set/dict lookup."""
pool = list(range(k))
pool_set = set(pool)
pool_pos = {v: i for i, v in enumerate(pool)}
items = list(range(n))
t0 = time.perf_counter()
accepted = []
for x in items:
if x in pool_set: # O(1)
accepted.append(x)
_ = pool_pos.get(x) # O(1)
return time.perf_counter() - t0
TRIALS = 3
CASES = [(100, 10), (500, 20), (1000, 30), (2000, 50)]
def run():
lines = []
header = "=== linux-0001: headerdep detect_cycles BFS list-grep ==="
print(header); lines.append(header)
for n, k in CASES:
df = min(bench_defective(n, k) for _ in range(TRIALS))
fx = min(bench_fixed(n, k) for _ in range(TRIALS))
speedup = (df / fx) if fx > 0 else float("inf")
line = f"D={n:<5} K={k:<5}: defective={df*1000:.3f}ms fixed={fx*1000:.3f}ms speedup={speedup:.1f}x"
print(line); lines.append(line); sys.stdout.flush()
return lines
if __name__ == "__main__":
run()