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
Merged
Conversation
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.
|
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.
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.