Fix bisector non-termination on arrays larger than 2³¹ elements - #284
Open
maximilliangrand wants to merge 1 commit into
Open
Fix bisector non-termination on arrays larger than 2³¹ elements#284maximilliangrand wants to merge 1 commit into
maximilliangrand wants to merge 1 commit into
Conversation
The midpoint (lo + hi) >>> 1 coerces its operand to a Uint32, so once lo + hi reaches 2³², the sum wraps and the computed midpoint falls outside [lo, hi). The search then stops converging and loops forever (freezes the tab in Safari, which allows arrays this large). Use Math.trunc((lo + hi) / 2), the form proposed by mbostock in d3#261: it only loses precision once lo + hi reaches 2⁵³, far beyond any array a browser can allocate. Same integer arithmetic, no measurable cost. Closes d3#261. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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
bisectorcomputes the midpoint as(lo + hi) >>> 1.>>>coerces its operand to a Uint32, so oncelo + hireaches 2³² the sum wraps and the midpoint lands outside[lo, hi). The interval stops narrowing and the loop never terminates — it freezes the tab in Safari, which allows arrays this large. This is #261, confirmed there withnew Uint8Array(2147483649); d3.bisectRight(a, 0).Repro (no giant allocation needed — a Proxy stands in for a sorted array of ~2³² zeros; a correct bisect reads ≤ log2(len) ≈ 32 elements):
Fix
Use
Math.trunc((lo + hi) / 2)— the form @mbostock proposed in #261 and @yurivish agreed to. It only loses precision oncelo + hireaches 2⁵³ (≈9 PB of indices), far beyond any array a browser can allocate, so it is strictly more robust than thelo + (hi - lo >>> 1)alternative. Same integer arithmetic, no measurable cost. This is the only>>> 1midpoint insrc(quickselect usesMath.floor).The added regression test passes with the fix and fails without it (the loop reads past 64 elements and throws).
A note on scope
Fil raised an open question in the issue: whether d3 wants to officially support arrays this large, versus documenting a supported cap of ~2³⁰ elements. This PR is the minimal, zero-cost fix — it removes the non-termination without committing the project either way, and the call on documenting a limit is left to the maintainers.
Closes #261.