Skip to content

tags: sort the candidate ids for late tag checks only once - #180

Open
cldellow wants to merge 1 commit into
masterfrom
tags-late-sort-once
Open

cldellow wants to merge 1 commit into
masterfrom
tags-late-sort-once

Conversation

@cldellow

@cldellow cldellow commented Nov 24, 2026 •

Copy link
Copy Markdown
Owner

Fixes #175.

Change

  • generate_ids_by_coarse (core/type_tags.h, both overloads) no longer sorts each coarse index's ids a second time after sorting them and removing duplicates.
  • filter_ids_by_tags (data/filter_ids_by_tags.h) sorts the ids for the first key it checks only if they aren't sorted yet (std::is_sorted, linear). They always are for the first key, which gets them from generate_ids_by_coarse; later keys get ids pushed in tag entry order and still sort them.

Two files, 3 lines added, 7 removed. No change to the database format, the query API or the results.

Impact

Late tag checks run when a statement's strategy is prefer_ranges (bbox under 1 square degree, around under 2 km, areas, input sets). Their candidates are every element in the search area, so large boxes and unions over the same box gain most.

Production-shaped queries. The 20 heaviest query templates on the public servers in June 2026, moved to Boston and New England coordinates, on a US northeast full-history extract (Geofabrik). Current-date queries, warm cache, 4-core VM, compare.py --paired --reps 10 against upstream/master c3a20c0. Identical output on all 20; sum of median CPU −2%. Queries with a clear change:

Query Shape Paired B/A (95% CI) B faster
p04 15 statements in a 0.4 × 0.4 degree box, brand-name regexes (below) 0.856 (0.825–0.889) 10 of 10
p15 35 statements in a 0.004 × 0.006 degree box 0.929 (0.877–0.984) 8 of 10
p20 4 statements, around:25000 0.946 (0.914–0.980) 9 of 10

p01 (4 statements in a 1 × 1 degree box, which doesn't reach the changed code) first came out at 1.046 while other work ran on the machine; re-measured with 20 pairs on a quiet machine it is 0.995 (0.970–1.021).

A shorter query of the p04 kind, single runs (2.54, 2.52, 2.61 s on upstream/master; 2.30, 2.20, 2.36 s with this PR):

[out:json]; (node["shop"](42.16,-71.26,42.56,-70.86); node["amenity"](42.16,-71.26,42.56,-70.86); node["tourism"](42.16,-71.26,42.56,-70.86); ); out count;
p04
[out:json][timeout:25]; (node["amenity"~"fuel|truck_stop"]["brand"~"travelcenters of america|petro stopping center|love's travel stop|ta travelcenters|pilot flying j|sapp brothers|pilot travel|seven eleven|road ranger|sapp bros|race trac|flying j|ta petro|buc-ee's|racetrac|speedway|circle k|7-eleven|7 eleven|flyingj|buc ees|am best|circlek|7eleven|love's|bucees|ambest|ambest|loves|pilot|petro|7-11|7 11|wawa|ta",i](42.16,-71.26,42.56,-70.86); node["amenity"~"fuel|truck_stop"]["name"~"travelcenters of america|petro stopping center|love's travel stop|ta travelcenters|pilot flying j|sapp brothers|pilot travel|seven eleven|road ranger|sapp bros|race trac|flying j|ta petro|buc-ee's|racetrac|speedway|circle k|7-eleven|7 eleven|flyingj|buc ees|am best|circlek|7eleven|love's|bucees|ambest|ambest|loves|pilot|petro|7-11|7 11|wawa|ta",i](42.16,-71.26,42.56,-70.86); way["amenity"~"fuel|truck_stop"]["brand"~"travelcenters of america|petro stopping center|love's travel stop|ta travelcenters|pilot flying j|sapp brothers|pilot travel|seven eleven|road ranger|sapp bros|race trac|flying j|ta petro|buc-ee's|racetrac|speedway|circle k|7-eleven|7 eleven|flyingj|buc ees|am best|circlek|7eleven|love's|bucees|ambest|ambest|loves|pilot|petro|7-11|7 11|wawa|ta",i](42.16,-71.26,42.56,-70.86); way["amenity"~"fuel|truck_stop"]["name"~"travelcenters of america|petro stopping center|love's travel stop|ta travelcenters|pilot flying j|sapp brothers|pilot travel|seven eleven|road ranger|sapp bros|race trac|flying j|ta petro|buc-ee's|racetrac|speedway|circle k|7-eleven|7 eleven|flyingj|buc ees|am best|circlek|7eleven|love's|bucees|ambest|ambest|loves|pilot|petro|7-11|7 11|wawa|ta",i](42.16,-71.26,42.56,-70.86); node["shop"~"convenience|supermarket"]["brand"~"travelcenters of america|petro stopping center|love's travel stop|ta travelcenters|pilot flying j|sapp brothers|pilot travel|seven eleven|road ranger|sapp bros|race trac|flying j|ta petro|buc-ee's|racetrac|speedway|circle k|7-eleven|7 eleven|flyingj|buc ees|am best|circlek|7eleven|love's|bucees|ambest|ambest|loves|pilot|petro|7-11|7 11|wawa|ta",i](42.16,-71.26,42.56,-70.86); node["shop"~"convenience|supermarket"]["name"~"travelcenters of america|petro stopping center|love's travel stop|ta travelcenters|pilot flying j|sapp brothers|pilot travel|seven eleven|road ranger|sapp bros|race trac|flying j|ta petro|buc-ee's|racetrac|speedway|circle k|7-eleven|7 eleven|flyingj|buc ees|am best|circlek|7eleven|love's|bucees|ambest|ambest|loves|pilot|petro|7-11|7 11|wawa|ta",i](42.16,-71.26,42.56,-70.86); node["leisure"~"fitness_centre|sports_centre"]["brand"~"planet fitness|planet-fitness|crunch fitness|crunch-fitness|planetfitness|crunch gym",i](42.16,-71.26,42.56,-70.86); node["leisure"~"fitness_centre|sports_centre"]["name"~"planet fitness|planet-fitness|crunch fitness|crunch-fitness|planetfitness|crunch gym",i](42.16,-71.26,42.56,-70.86); way["leisure"~"fitness_centre|sports_centre"]["brand"~"planet fitness|planet-fitness|crunch fitness|crunch-fitness|planetfitness|crunch gym",i](42.16,-71.26,42.56,-70.86); way["leisure"~"fitness_centre|sports_centre"]["name"~"planet fitness|planet-fitness|crunch fitness|crunch-fitness|planetfitness|crunch gym",i](42.16,-71.26,42.56,-70.86); node["amenity"="gym"]["brand"~"planet fitness|planet-fitness|crunch fitness|crunch-fitness|planetfitness|crunch gym",i](42.16,-71.26,42.56,-70.86); node["amenity"="gym"]["name"~"planet fitness|planet-fitness|crunch fitness|crunch-fitness|planetfitness|crunch gym",i](42.16,-71.26,42.56,-70.86); way["amenity"="gym"]["name"~"planet fitness|planet-fitness|crunch fitness|crunch-fitness|planetfitness|crunch gym",i](42.16,-71.26,42.56,-70.86); ); out center tags;

Benchmark queries: the timing workflow runs them on this PR.

Testing

  • The 20 production-shaped queries: identical output to upstream/master.
  • 106 targeted queries on a Rhode Island full-history database (areas, input sets, bboxes, global filters; key=value, key-only and key-regex filters; three past dates; out meta): identical output.
  • CI: fuzz, history and props run on this PR.

Limitations

  • The ids are still sorted once per statement; a union over the same area repeats it for each statement.

Generated by Claude Code

@cldellow cldellow added the query-perf Query performance change; collected on all-changes label Nov 24, 2026 — with Claude

Copy link
Copy Markdown
Owner Author

CI on this PR, against upstream/master c3a20c0:

Workflow Run Result
fuzz (plain, areas,complete, extended sets) 45349360526 5,986 same, 14 skipped, 0 differences
history (history,users,complete) 45349360306 1,999 same, 1 skipped, 0 differences
props 45349360299 passed
timing (benchmark queries, Nova Scotia, 5 runners) 45349360376 sum of medians 0.996; identical outputs

Beyond the noise floor: q06_around −7.3%, t03_turbo_wizard_bbox −4.9%, t06_josm_wizard_bbox −3.9%, q08_out_meta −2.8%; t01_turbo_default +0.6% (floor ±0.5%) and a3_buildings_hrm +0.3% (floor ±0.2%), which this change doesn't plausibly slow.


Generated by Claude Code

Copy link
Copy Markdown
Owner Author

Re-measured p01 on a quieter machine (compare.py --paired --reps 20, same database, against upstream/master c3a20c0), with p11 as a second query that doesn't reach the changed code:

Query Paired B/A (95% CI) B faster
p01 0.995 (0.970–1.021) 13 of 20
p11 1.004 (0.989–1.019) 8 of 20

So the 1.046 in the description was noise from other work running at the time; no slowdown.


Generated by Claude Code

Remove two redundant sorts, and add a guard to avoid a redundant sort in
some cases:

- generate_ids_by_coarse (both overloads) sorted each coarse index's ids
  a second time, after already sorting them and removing duplicates.
- filter_ids_by_tags sorts the ids before checking each required key.
  The first key's ids are always already sorted, so it now sorts only if
  std::is_sorted says they aren't.

Geofabrik Nova Scotia extract, warm cache, 25 alternating pairs:

  [out:json]; (node["shop"](44.45,-63.80,44.85,-63.40); node["amenity"](44.45,-63.80,44.85,-63.40); node["tourism"](44.45,-63.80,44.85,-63.40); ); out count;
      638 ms -> 561 ms (-13%)

Co-Authored-By: Colin Dellow <cldellow@gmail.com>
@cldellow
cldellow force-pushed the tags-late-sort-once branch from dcac9f3 to 2f3159c Compare November 25, 2026 20:04
@cldellow

Copy link
Copy Markdown
Owner Author

Offered upstream in fossgis#15

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

human-reviewed Colin has reviewed this. query-perf Query performance change; collected on all-changes upstreamed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Late tag checks sort the same candidate ids up to three times

2 participants