Skip to content
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
sauravbhattacharya001 merged 1 commit into
masterfrom
perf/array-based-diameter-bfs
Mar 28, 2026
Merged

sauravbhattacharya001 merged 1 commit into
masterfrom
perf/array-based-diameter-bfs

Conversation

@sauravbhattacharya001

Copy link
Copy Markdown
Owner

Problem

GraphDiameterAnalyzer.analyze() calls GraphUtils.bfsDistances() once per vertex in the largest component. Each call allocates a new HashMap<String, Integer> and LinkedList<String>, and performs String hashing + Integer autoboxing inside the BFS loop.

For a component of V vertices and E edges, this means:

  • V HashMap allocations (each with V entries)
  • V² String.hashCode() calls inside BFS
  • V² Integer autoboxing operations
  • V LinkedList allocations for the BFS queue

Fix

Pre-build an int[][] adjacency list once from the graph, then reuse int[] dist and int[] queue arrays across all V BFS passes — zero per-source allocations.

This is the same optimisation pattern already used in NodeCentralityAnalyzer.computeBetweennessAndCloseness() and PageRankAnalyzer.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.

…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.
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@github-actions github-actions Bot added visualization Graph visualization and UI size/m labels Mar 22, 2026
@sauravbhattacharya001
sauravbhattacharya001 merged commit 1f09107 into master Mar 28, 2026
4 of 8 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

size/m visualization Graph visualization and UI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant