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.
This commit is contained in:
parent
b5b9cce0a1
commit
99be1fbed9
125 changed files with 3950 additions and 0 deletions
6
defects/rubocop/Makefile
Normal file
6
defects/rubocop/Makefile
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
.PHONY: all bench clean
|
||||
all: bench
|
||||
bench:
|
||||
python3 bench/run_all.py
|
||||
clean:
|
||||
rm -rf bench/__pycache__ __pycache__
|
||||
76
defects/rubocop/bench/bench-rubocop-0001.py
Normal file
76
defects/rubocop/bench/bench-rubocop-0001.py
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
#!/usr/bin/env python3
|
||||
# bench-rubocop-0001.py
|
||||
# IgnoredNode @ignored_nodes Array: `ignored_node?(node)` runs
|
||||
# `ignored_nodes.any? { |n| n.equal?(node) }` — O(S) per call.
|
||||
# `part_of_ignored_node?` maps over the full array each call — O(S).
|
||||
# For R regexp_literal + S string_literal nodes per file, the StringHelp
|
||||
# path costs O(R × S). Fix: `Set.new.compare_by_identity` — O(1) via
|
||||
# object hash/identity.
|
||||
|
||||
import sys
|
||||
import time
|
||||
|
||||
|
||||
class _Node:
|
||||
"""Ruby node object — identity used for membership (RuboCop Parser::AST::Node)."""
|
||||
__slots__ = ("id",)
|
||||
def __init__(self, i):
|
||||
self.id = i
|
||||
|
||||
|
||||
def bench_defective(r, s):
|
||||
"""Array membership via identity scan — O(R*S)."""
|
||||
ignored = [_Node(i) for i in range(r)] # R regexp nodes ignored
|
||||
strs = [ignored[i % r] if i % 3 == 0 else _Node(1000 + i) for i in range(s)]
|
||||
|
||||
t0 = time.perf_counter()
|
||||
hits = 0
|
||||
for node in strs:
|
||||
# Array#any? { |n| n.equal?(node) } — O(R) per call
|
||||
found = False
|
||||
for ig in ignored:
|
||||
if ig is node:
|
||||
found = True
|
||||
break
|
||||
if found:
|
||||
hits += 1
|
||||
return time.perf_counter() - t0
|
||||
|
||||
|
||||
def bench_fixed(r, s):
|
||||
"""Set.compare_by_identity — O(1) membership via object id."""
|
||||
ignored_set = set()
|
||||
ignored_list = []
|
||||
for i in range(r):
|
||||
n = _Node(i)
|
||||
ignored_set.add(id(n)) # identity hash
|
||||
ignored_list.append(n)
|
||||
strs = [ignored_list[i % r] if i % 3 == 0 else _Node(1000 + i) for i in range(s)]
|
||||
|
||||
t0 = time.perf_counter()
|
||||
hits = 0
|
||||
for node in strs:
|
||||
if id(node) in ignored_set: # O(1)
|
||||
hits += 1
|
||||
return time.perf_counter() - t0
|
||||
|
||||
|
||||
TRIALS = 3
|
||||
CASES = [(50, 500), (100, 1000), (200, 2000), (500, 5000)]
|
||||
|
||||
|
||||
def run():
|
||||
lines = []
|
||||
header = "=== rubocop-0001: IgnoredNode Array#any?(equal?) vs Set.compare_by_identity ==="
|
||||
print(header); lines.append(header)
|
||||
for r, s in CASES:
|
||||
df = min(bench_defective(r, s) for _ in range(TRIALS))
|
||||
fx = min(bench_fixed(r, s) for _ in range(TRIALS))
|
||||
speedup = (df / fx) if fx > 0 else float("inf")
|
||||
line = f"R={r:<4} S={s:<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()
|
||||
54
defects/rubocop/bench/bench-rubocop-0002.py
Normal file
54
defects/rubocop/bench/bench-rubocop-0002.py
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
#!/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()
|
||||
12
defects/rubocop/bench/results.txt
Normal file
12
defects/rubocop/bench/results.txt
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
=== rubocop-0001: IgnoredNode Array#any?(equal?) vs Set.compare_by_identity ===
|
||||
R=50 S=500 : defective=0.534ms fixed=0.075ms speedup=7.1x
|
||||
R=100 S=1000 : defective=2.005ms fixed=0.150ms speedup=13.4x
|
||||
R=200 S=2000 : defective=7.898ms fixed=0.288ms speedup=27.4x
|
||||
R=500 S=5000 : defective=47.680ms fixed=0.731ms speedup=65.2x
|
||||
|
||||
=== rubocop-0002: RedundantSelf Array#include? vs Set#include? ===
|
||||
N=500 S=50 : defective=0.380ms fixed=0.023ms speedup=16.8x
|
||||
N=1000 S=100 : defective=1.422ms fixed=0.048ms speedup=29.7x
|
||||
N=2000 S=200 : defective=5.185ms fixed=0.132ms speedup=39.3x
|
||||
N=5000 S=500 : defective=33.127ms fixed=0.423ms speedup=78.4x
|
||||
|
||||
22
defects/rubocop/bench/run_all.py
Normal file
22
defects/rubocop/bench/run_all.py
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#!/usr/bin/env python3
|
||||
import importlib.util, os, sys
|
||||
BENCH_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
def load_module(filename):
|
||||
path = os.path.join(BENCH_DIR, filename)
|
||||
spec = importlib.util.spec_from_file_location("mod", path)
|
||||
mod = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(mod)
|
||||
return mod
|
||||
|
||||
all_lines = []
|
||||
for fname in ["bench-rubocop-0001.py", "bench-rubocop-0002.py"]:
|
||||
mod = load_module(fname)
|
||||
lines = mod.run()
|
||||
all_lines.extend(lines); all_lines.append("")
|
||||
print(); sys.stdout.flush()
|
||||
|
||||
out_path = os.path.join(BENCH_DIR, "results.txt")
|
||||
with open(out_path, "w") as f:
|
||||
f.write("\n".join(all_lines) + "\n")
|
||||
print(f"results written to {out_path}"); sys.stdout.flush()
|
||||
Loading…
Add table
Add a link
Reference in a new issue