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

perf: eliminate Math.sqrt from force-directed repulsion hot path - #148

Merged
sauravbhattacharya001 merged 1 commit into
masterfrom
perf/quadtree-sqrt-elimination
Apr 1, 2026
Merged

sauravbhattacharya001 merged 1 commit into
masterfrom
perf/quadtree-sqrt-elimination

Conversation

@sauravbhattacharya001

Copy link
Copy Markdown
Owner

Eliminates redundant Math.sqrt() calls from force-directed layout repulsion (both Barnes-Hut and brute-force paths). Replaces sqrt+division with squared-distance arithmetic: force = k²/dist becomes fx = dx * k²/distSq. Barnes-Hut check uses size² < theta² * distSq. Pre-computes k² and theta² outside the per-vertex loop. For 1000 nodes / 300 iterations, saves ~3M sqrt calls in Barnes-Hut mode.

Replace sqrt + division with squared-distance arithmetic in both
QuadTree Barnes-Hut traversal and brute-force repulsion:

- force = k²/dist; f_x = dx/dist * force  →  f_x = dx * k²/distSq
- Barnes-Hut check: size/dist < theta  →  size² < theta² * distSq

This avoids ~V*log(V) sqrt calls per iteration for Barnes-Hut mode
and ~V²/2 calls for brute-force mode. The force direction is preserved
since dx/distSq gives the correct unit vector scaling.

Pre-compute k² and theta² outside the per-vertex loop to avoid
redundant multiplication.
@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 30, 2026
@sauravbhattacharya001
sauravbhattacharya001 merged commit 7519426 into master Apr 1, 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