Repository navigation
perf(overlap, envelop): walk the tree once instead of once per boundary point (#120) - #166
Open
p-ramcharan-dev wants to merge 1 commit into
Open
p-ramcharan-dev wants to merge 1 commit into
p-ramcharan-dev wants to merge 1 commit into
Conversation
overlap() and envelop() ran one point search per boundary point inside the queried range. Add Node.search_range() and Node.search_enveloped(), which visit each node at most once and skip a branch when the range lies entirely on the other side of x_center. Co-Authored-By: Claude <[email protected]>
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.
overlap(begin, end)andenvelop(begin, end)run one point search forbeginand then one more for everyboundary point inside the range, so a query costs O(k log n) with k the number of boundaries in the range,
and every hit is added to the result set once per boundary it spans. This is the slowdown reported in #120.
This change adds
Node.search_range()andNode.search_enveloped(), which visit each node at most once andskip a branch when the range lies entirely on the other side of
x_center. They rely only on whatNode.verify()already checks: intervals ins_centercontainx_center, the left branch ends at orbefore it, and the right branch begins after it.
overlap()andenvelop()call them; nothing elsechanges.
remove_overlap(),remove_envelop(),chop()andtree[a:b]get faster because they use thesetwo methods.
Timings
Microseconds per query, before and after, on the same tree objects. Python 3.13.5 on Windows, one laptop,
best of 5 alternating rounds; the slower rows use small batches (as few as 5 queries), so treat the ratios
as rough.
overlapbeforeenvelopbeforeThe last row gains least because most of what remains is building the result set:
Interval.__hash__ignores
data, so intervals with the same range collide (#126). That is not addressed here.Behaviour
are found in a different order.
remove_overlap(begin, end),remove_envelop()andchop()remove hitsin that order, so the internal layout of the tree afterwards can differ; its contents do not.
set. Trees that contain NaN endpoints were already inconsistent and still are.
Node.search_overlap()is no longer called by the library. I left it in place in case anyone calls it.overlaps()only.Tests
test/issues/issue120_test.pycompares both methods with a plain scan of all intervals: every pair ofpoints on small dense trees (including null and reversed ranges), trees built incrementally and then
pruned, float endpoints with infinities, a deeper tree of 3,330 intervals, the half-open boundaries, and a
check that a range query no longer calls
search_point. The full suite passes locally on Python 3.13(110 tests). I could not run Python 2.7 or 3.5 locally; the new code avoids anything newer than those, and
CI will say.
Disclosure
I wrote this with the help of an AI coding assistant (Claude Code). I have reviewed the change, and the
timings and test results above are from runs on my machine.
🤖 Generated with Claude Code