java-topology/defects/rubocop/bench/bench-rubocop-0002.py
russell@unturf.com 99be1fbed9 bench backfill: close 36 orphan-project dirs + fix rubocop MOAD misclass
Previously 52 registered defects had no project dir locally because their
patches live under a sibling project (e.g. ans-* under defects/ansible/,
geth-0001 under defects/go-ethereum/, cfe-* under defects/cfengine/).

Created defects/{stem}/bench/ for each orphan stem (ans, argo, cel, cfe,
element, geth, go-stdlib, hv, igraph, nats, nx, openscad, otel, pre, r,
rmq, simplex, sm, solargraph, tf, tf-aws) and wrote the standard list-vs-set
benches against each defect. Patches stay where they are; bench_status
looks up by defect-id prefix, not patch location.

Also added defects/rubocop/ with benches for rubocop-0001 and rubocop-0002
(Array -> Set with compare_by_identity). Both were misclassified as
MOAD-0011 ReDoS in generate_undf.py; the tickets show they're CWE-407
Sedimentary (O(N*S) -> O(N+S) via identity Set).

Coverage: 1243 -> 1281 (96.0% -> 98.9%). The 14 remaining are truly
missing — no patch anywhere in the tree: dragonflybsd-0001..0005,
jami-daemon, jitsi-videobridge, netbsd-0001..0004, regamedll-cs-0001,
supertuxkart-0002, hadoop-rpc-0001.
2026-04-23 12:37:51 -04:00

54 lines
1.7 KiB
Python

#!/usr/bin/env python3
# bench-rubocop-0002.py
# Style::RedundantSelf @allowed_send_nodes Array: `allowed_send_node?`
# calls `.include?(node)` — O(S) per send_node check. With N send_node
# checks and S allowed entries accumulated during one file, total cost is
# O(N*S). Fix: swap Array for Set for O(1) include?.
import sys
import time
def bench_defective(n_checks, s_allowed):
allowed = list(range(s_allowed)) # Ruby Array @allowed_send_nodes
queries = [i % (s_allowed + 100) for i in range(n_checks)]
t0 = time.perf_counter()
hits = 0
for q in queries:
if q in allowed: # list __contains__ = O(S)
hits += 1
return time.perf_counter() - t0
def bench_fixed(n_checks, s_allowed):
allowed = set(range(s_allowed)) # Ruby Set @allowed_send_nodes
queries = [i % (s_allowed + 100) for i in range(n_checks)]
t0 = time.perf_counter()
hits = 0
for q in queries:
if q in allowed: # O(1)
hits += 1
return time.perf_counter() - t0
TRIALS = 3
CASES = [(500, 50), (1000, 100), (2000, 200), (5000, 500)]
def run():
lines = []
header = "=== rubocop-0002: RedundantSelf Array#include? vs Set#include? ==="
print(header); lines.append(header)
for n, s in CASES:
df = min(bench_defective(n, s) for _ in range(TRIALS))
fx = min(bench_fixed(n, s) for _ in range(TRIALS))
speedup = (df / fx) if fx > 0 else float("inf")
line = f"N={n:<5} S={s:<4}: 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()