package unit; import java.util.*; /** * v8-0004: KnownMapsMerger::IntersectWithKnownNodeAspects * std::find O(P×R) → sorted merge O(P+R) * * SLOW: simulates std::find over known_maps for each requested map — O(P×R) * FAST: uses HashSet.contains() — O(P) with O(R) setup */ public class V8MaglevKnownMapsMergerTest { static long scanOps = 0; static long cmpOps = 0; // SLOW: O(P×R) — std::find per requested map static List intersectSlow(List requestedMaps, List knownMaps) { List result = new ArrayList<>(); for (int map : requestedMaps) { scanOps++; for (int known : knownMaps) { // O(R) per map cmpOps++; if (known == map) { result.add(map); break; } } } return result; } // FAST: O(P + R) — build HashSet from knownMaps, then O(1) lookup static List intersectFast(List requestedMaps, List knownMaps) { Set knownSet = new HashSet<>(knownMaps); List result = new ArrayList<>(); for (int map : requestedMaps) { if (knownSet.contains(map)) result.add(map); } return result; } public static void main(String[] args) { // Simulate P=8 requested maps, R=50 predecessor known maps int P = 8; int R = 50; int NODES = 1000; // number of CheckMaps nodes compiled // Build test data: half of requested maps appear in known List requestedMaps = new ArrayList<>(); for (int i = 0; i < P; i++) requestedMaps.add(i * 10); List knownMaps = new ArrayList<>(); for (int i = 0; i < R; i++) knownMaps.add(i * 4); // some overlap // Verify correctness first List slowResult = intersectSlow(requestedMaps, knownMaps); scanOps = 0; cmpOps = 0; List fastResult = intersectFast(requestedMaps, knownMaps); Collections.sort(slowResult); Collections.sort(fastResult); if (!slowResult.equals(fastResult)) { System.err.println("FAIL: slow=" + slowResult + " fast=" + fastResult); System.exit(1); } // Benchmark SLOW across NODES CheckMaps compilations scanOps = 0; cmpOps = 0; for (int n = 0; n < NODES; n++) { intersectSlow(requestedMaps, knownMaps); } long slowCmp = cmpOps; // Benchmark FAST across NODES compilations scanOps = 0; cmpOps = 0; long fastOps = 0; for (int n = 0; n < NODES; n++) { intersectFast(requestedMaps, knownMaps); fastOps += P; // O(P) lookups per call } double ratio = (double) slowCmp / Math.max(fastOps, 1); System.out.printf("v8-0004 KnownMapsMerger: SLOW=%d cmpOps, FAST~=%d ops, ratio=%.1fx%n", slowCmp, fastOps, ratio); if (ratio < 5.0) { System.err.println("FAIL: speedup ratio " + ratio + " < 5x"); System.exit(1); } System.out.println("PASS"); } }