Skip to content

around's reach grid tests input segments repeatedly, and its boxes are too wide #36

Open

Description

@cldellow

The reach grid that around uses to narrow down its exact tests (added in #10) tests some input segments many times per lookup, and its boxes are wider than needed. That makes around against long, rural input geometry much slower than its result size suggests.

Query

Found by the differential fuzzer. On the Nova Scotia history database (current data only; the database is described in #2):

[out:csv(::count)];
wr(newer:"2016-12-19T18:03:25Z")(45.574002,-62.284014,45.619842,-62.234392)->.s0;
way(around.s0:20)["material"!~"^conc"];
out count;

.s0 holds 188 ways and 14 relations: province-long road routes (the Trans-Canada Highway) and rural boundaries, with 1,700 member ways and 22,600 nodes. The result is 12,809 ways, which takes 17.9 s on the all-changes branch (#12). A dense urban query of similar shape (all ways within 20 m of the 31,769 highways in the box 44.60,-63.70,44.75,-63.45) returns 108,198 ways in 11.2 s. So the first query costs about 13 times more per result.

Causes in the grid

  1. Entries are tested once per shared cell. Each input segment is filed under every cell its reach box covers, up to 64. A lookup box that spans several of those cells passes the segment to the exact test once per cell.
  2. Large lookup boxes scan every cell's list. When a candidate segment's box covers more than 64 cells, the lookup checks every entry. It does that by walking the cell map, so each entry is tested once per cell it's filed under, up to 64 times. Long, straight rural segments hit this path.
  3. Reach boxes are about three times too wide. A segment's box is widened by its own length plus the radius. That length term guards against a great-circle arc bulging out of its endpoints' box. But for a segment of a few km the bulge is a few metres (about L² tan(latitude) / 8R), so the box covers about nine times the area it needs.

The exact tests (intersect, great_circle_line_dist) took about half the query's time.

Fix

The grid part of #10 is revised to:

  • test each entry once per lookup
  • scan a list of entries once when a box is too large
  • widen segments by the radius plus twice the arc bulge plus 1 m, keeping the old bound beyond 50 km or 80° latitude

On top of the all-changes branch, the query above drops from 17.9 s to 11.2 s. The rest of its cost comes from how many candidates around reads for relation inputs, which is #37.


Generated by Claude Code

Activity

  1. added
    query-perf
    on Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    query-perf

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions