Skip to content

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
chaimleib:masterfrom
p-ramcharan-dev:perf/120/overlap-one-walk
Open

p-ramcharan-dev wants to merge 1 commit into
chaimleib:masterfrom
p-ramcharan-dev:perf/120/overlap-one-walk

Conversation

@p-ramcharan-dev

Copy link
Copy Markdown

overlap(begin, end) and envelop(begin, end) run one point search for begin and then one more for every
boundary 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() 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. They rely only on what
Node.verify() already checks: intervals in s_center contain x_center, the left branch ends at or
before it, and the right branch begins after it. overlap() and envelop() call them; nothing else
changes. remove_overlap(), remove_envelop(), chop() and tree[a:b] get faster because they use these
two 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.

Tree Query width Hits per query overlap before after envelop before after
12,000 intervals, length <= 2,000 5,000 71 1,372 13.4 102x 1,407 14.5 97x
100,000 intervals, length <= 2,000 5,000 60 1,246 16.0 78x 1,269 15.5 82x
same tree 100,000 1,021 25,703 76.8 335x 25,940 267 97x
same tree 50 10 30.8 10.8 2.9x 31.6 1.4 23x
100,000 intervals, length <= 200,000 5,000 1,027 54,588 510 107x 55,101 1.8 (0.6 hits)
450,000 intervals, length <= 400 5,000 52 779 16.0 49x 795 21.8 36x
12,000 intervals with endpoints in 0..63 (the shape in #120) 8 4,740 26,705 3,971 6.7x 26,903 1,227 22x

The 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

  • Results are the same sets. The order in which a result set iterates can differ from before, since hits
    are found in a different order. remove_overlap(begin, end), remove_envelop() and chop() remove hits
    in that order, so the internal layout of the tree afterwards can differ; its contents do not.
  • A NaN query endpoint used to return whatever the boundary scan happened to reach; it now returns an empty
    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.
  • No overlap with fix(overlaps): scan only query-range boundary points (#127) #158, which changes overlaps() only.

Tests

test/issues/issue120_test.py compares both methods with a plain scan of all intervals: every pair of
points 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

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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant