java-topology/defects/v8/unit/V8MaglevKnownMapsMergerTest.java

94 lines
3.2 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<Integer> intersectSlow(List<Integer> requestedMaps, List<Integer> knownMaps) {
List<Integer> 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<Integer> intersectFast(List<Integer> requestedMaps, List<Integer> knownMaps) {
Set<Integer> knownSet = new HashSet<>(knownMaps);
List<Integer> 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<Integer> requestedMaps = new ArrayList<>();
for (int i = 0; i < P; i++) requestedMaps.add(i * 10);
List<Integer> knownMaps = new ArrayList<>();
for (int i = 0; i < R; i++) knownMaps.add(i * 4); // some overlap
// Verify correctness first
List<Integer> slowResult = intersectSlow(requestedMaps, knownMaps);
scanOps = 0; cmpOps = 0;
List<Integer> 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");
}
}