onos-0004: ConnectivityIntentCompiler O(R×C) 75x + whitepaper 616

This commit is contained in:
russell@unturf.com 2026-03-28 10:56:56 -04:00
parent 725c91213f
commit ea42ae35c7
6 changed files with 148 additions and 3 deletions

View file

@ -0,0 +1,79 @@
package unit;
import java.util.*;
import java.util.stream.*;
/**
* onos-0004: ConnectivityIntentCompiler resourcesAllocated List.contains O(R×C) → O(C) with Set
* SLOW: List<Resource>.contains() — O(R) per element in filter stream
* FAST: HashSet<Resource>.contains() — O(1) per element
*/
public class ONOS0004ConnectivityResourcesTest {
static long cmpOps = 0;
// Simulate Resource (integer id)
static class Resource {
final int id;
Resource(int id) { this.id = id; }
@Override public boolean equals(Object o) {
cmpOps++;
return o instanceof Resource && ((Resource)o).id == id;
}
@Override public int hashCode() { return id; }
}
// SLOW: List.contains() inside filter — O(R) per candidate
static List<Resource> filterSlow(List<Resource> candidates, List<Resource> allocated) {
return candidates.stream()
.filter(r -> !allocated.contains(r)) // O(R) per candidate
.collect(Collectors.toList());
}
// FAST: HashSet.contains() — O(1) per candidate
static List<Resource> filterFast(List<Resource> candidates, List<Resource> allocated) {
Set<Resource> allocatedSet = new HashSet<>(allocated);
return candidates.stream()
.filter(r -> !allocatedSet.contains(r))
.collect(Collectors.toList());
}
public static void main(String[] args) {
int R = 100; // already-allocated resources
int C = 50; // incoming candidates
int CALLS = 500; // intent recompile events
// Build test data: allocated resources 0..R-1, candidates 50..50+C-1 (overlap at 50..99)
List<Resource> allocated = IntStream.range(0, R)
.mapToObj(Resource::new).collect(Collectors.toList());
List<Resource> candidates = IntStream.range(C, C + C)
.mapToObj(Resource::new).collect(Collectors.toList());
// Verify correctness
List<Resource> slowResult = filterSlow(candidates, allocated);
cmpOps = 0;
List<Resource> fastResult = filterFast(candidates, allocated);
if (slowResult.size() != fastResult.size()) {
System.err.println("FAIL: slow=" + slowResult.size() + " fast=" + fastResult.size());
System.exit(1);
}
// Benchmark SLOW
cmpOps = 0;
for (int i = 0; i < CALLS; i++) filterSlow(candidates, allocated);
long slowCmp = cmpOps;
// Benchmark FAST (count HashSet lookups as C per call)
long fastOps = (long) CALLS * C;
double ratio = (double) slowCmp / Math.max(fastOps, 1);
System.out.printf("onos-0004 ConnectivityResources: SLOW=%d cmpOps, FAST~=%d ops, ratio=%.1fx%n",
slowCmp, fastOps, ratio);
if (ratio < 5.0) {
System.err.println("FAIL: ratio " + ratio + " < 5x");
System.exit(1);
}
System.out.println("PASS");
}
}