Skip to content
This repository was archived by the owner on Jun 18, 2026. It is now read-only.

Commit 100a6e1

Browse files
perf(PlanarGraphAnalyzer): eliminate HashSet allocation in contraction strategies 2/3
In tryContractionStrategy cases 2 and 3 (common-neighbor heuristic), replaced per-edge HashSet copy + retainAll with an allocation-free counting loop that iterates the smaller neighbor set and probes the larger one. Eliminates O(|E|) temporary HashSet allocations in the inner loop of the minor-detection contraction phase.
1 parent 640aa34 commit 100a6e1

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

Gvisual/src/gvisual/PlanarGraphAnalyzer.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -624,12 +624,20 @@ private static boolean tryContractionStrategy(Map<String, Set<String>> adj,
624624
case 2: case 3: {
625625
int bestCommon = (strategy == 2) ? -1 : Integer.MAX_VALUE;
626626
for (String v : g.keySet()) {
627-
for (String n : g.get(v)) {
628-
Set<String> common = new HashSet<String>(g.get(v));
629-
common.retainAll(g.get(n));
630-
boolean better = (strategy == 2) ? common.size() > bestCommon : common.size() < bestCommon;
627+
Set<String> vNbrs = g.get(v);
628+
for (String n : vNbrs) {
629+
// Count common neighbors without allocating a temporary HashSet.
630+
// Previously created new HashSet<>(g.get(v)) + retainAll per pair.
631+
Set<String> nNbrs = g.get(n);
632+
Set<String> smaller = vNbrs.size() <= nNbrs.size() ? vNbrs : nNbrs;
633+
Set<String> larger = smaller == vNbrs ? nNbrs : vNbrs;
634+
int commonCount = 0;
635+
for (String s : smaller) {
636+
if (larger.contains(s)) commonCount++;
637+
}
638+
boolean better = (strategy == 2) ? commonCount > bestCommon : commonCount < bestCommon;
631639
if (better) {
632-
bestCommon = common.size();
640+
bestCommon = commonCount;
633641
toContract = v;
634642
bestNeighbor = n;
635643
}

0 commit comments

Comments
 (0)