This repository was archived by the owner on Jun 18, 2026. It is now read-only.
perf(GraphDiameterAnalyzer): array-based BFS eliminates per-source allocations - #111
Merged
Merged
Conversation
…tion Replace per-vertex HashMap-based BFS (via GraphUtils.bfsDistances) with a single pre-built int[][] adjacency list and reusable int[] distance/queue arrays across all BFS passes. This eliminates: - V HashMap<String,Integer> allocations (one per source vertex) - V^2 String hashing operations inside BFS - V^2 Integer autoboxing operations - V LinkedList<String> queue allocations The same optimisation pattern is already used in NodeCentralityAnalyzer and PageRankAnalyzer. For a 1000-node component this reduces GC pressure from ~1000 HashMap + LinkedList allocations to 0 per-source allocations.
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
GraphDiameterAnalyzer.analyze()callsGraphUtils.bfsDistances()once per vertex in the largest component. Each call allocates a newHashMap<String, Integer>andLinkedList<String>, and performs String hashing + Integer autoboxing inside the BFS loop.For a component of V vertices and E edges, this means:
Fix
Pre-build an
int[][]adjacency list once from the graph, then reuseint[] distandint[] queuearrays across all V BFS passes — zero per-source allocations.This is the same optimisation pattern already used in
NodeCentralityAnalyzer.computeBetweennessAndCloseness()andPageRankAnalyzer.compute().Impact
For a 1000-node component: eliminates ~1000 HashMap + LinkedList creations and ~1M autoboxing operations per analysis run. The algorithmic complexity remains O(V·(V+E)) but with dramatically lower constant factors and GC pressure.