From 489a4b6f8ffcb20ad3c9b15b31d15477493ad60b Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 21 Nov 2026 07:10:51 +0000 Subject: [PATCH 1/3] around: take candidate ranges for relation inputs from their members around's geometry for a relation in its input set is that of the relation's direct node and way members (calc_lat_lons). The candidate ranges, though, came from the relation's own index, which for a long route or a boundary is a compound index covering its whole extent: every object in that area became a candidate and had its geometry loaded and tested. calc_ranges now takes the ranges of the current relations' node members and the parent ranges of their way members, expanded by the radius as before. These cover all of the geometry the exact test uses, so results are unchanged. Attic relations keep their own index. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01736kJUqyDw9zM3Bkh2W2QT --- src/overpass_api/statements/around.cc | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/overpass_api/statements/around.cc b/src/overpass_api/statements/around.cc index b1e7c532c..1f391a634 100644 --- a/src/overpass_api/statements/around.cc +++ b/src/overpass_api/statements/around.cc @@ -686,12 +686,27 @@ Ranges< Uint32_Index > Around_Statement::calc_ranges(const Set& input, Resource_ return expand(calc_children(ranges(ways)), radius); } else + { + // The geometry of a relation is that of its direct node and way members (see + // calc_lat_lons), so take the ranges from their indexes. A relation's own index + // covers its whole extent, which for a long route or a boundary is a large area. + Ranges< Uint32_Index > member_node_ranges; + Ranges< Uint31_Index > member_way_ranges; + if (!input.relations.empty()) + { + Request_Context context(this, rman); + member_node_ranges = ranges(relation_node_members( + context, input.relations, {}, Ranges< Uint32_Index >::global(), {}, true).current); + member_way_ranges = ranges(relation_way_members( + context, input.relations, {}, Ranges< Uint31_Index >::global(), {}, true).current); + } return expand( - ranges(input.nodes).union_(ranges(input.attic_nodes)).union_( + ranges(input.nodes).union_(ranges(input.attic_nodes)).union_(member_node_ranges).union_( calc_children( ranges(input.ways).union_(ranges(input.attic_ways)).union_( - ranges(input.relations).union_(ranges(input.attic_relations))))), + member_way_ranges.union_(ranges(input.attic_relations))))), radius); + } } From 4a7c4b7370a03e24a40e81f0f98dcd67623734ea Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 21 Nov 2026 19:59:56 +0000 Subject: [PATCH 2/3] around: take candidate ranges for relations at a past date from their members The previous commit took around's candidate ranges for current relations in the input set from their members' indexes. Relations at a past date ([date:] and [diff:] queries) still contributed their own index, which for a long route or a boundary covers its whole extent. They now contribute the ranges of the members calc_lat_lons uses for their geometry: their node members and their way members as of the date. Results are unchanged. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01736kJUqyDw9zM3Bkh2W2QT --- src/overpass_api/statements/around.cc | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/overpass_api/statements/around.cc b/src/overpass_api/statements/around.cc index 1f391a634..ffe19b197 100644 --- a/src/overpass_api/statements/around.cc +++ b/src/overpass_api/statements/around.cc @@ -700,11 +700,19 @@ Ranges< Uint32_Index > Around_Statement::calc_ranges(const Set& input, Resource_ member_way_ranges = ranges(relation_way_members( context, input.relations, {}, Ranges< Uint31_Index >::global(), {}, true).current); } + // The same for relations at a past date, with the members calc_lat_lons uses for them. + if (!input.attic_relations.empty()) + { + Request_Context context(this, rman); + member_node_ranges = member_node_ranges.union_(ranges(relation_node_members( + context, input.attic_relations, relation_node_member_indices({}, input.attic_relations)))); + member_way_ranges = member_way_ranges.union_(ranges(relation_way_members( + context, input.attic_relations, Ranges< Uint31_Index >::global()))); + } return expand( ranges(input.nodes).union_(ranges(input.attic_nodes)).union_(member_node_ranges).union_( calc_children( - ranges(input.ways).union_(ranges(input.attic_ways)).union_( - member_way_ranges.union_(ranges(input.attic_relations))))), + ranges(input.ways).union_(ranges(input.attic_ways)).union_(member_way_ranges))), radius); } } From fb0945423737c9ed1748d57c6fb4b3a6026e7e55 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 21 Nov 2026 20:23:21 +0000 Subject: [PATCH 3/3] around: look up the input relations' members once per input calc_ranges runs several times per execution of an around filter: for the query's candidate ranges, twice in the cheap filter, and up to four times in the exact filter. Since the previous commits, each run looked up the input relations' node and way members to compute their index ranges. On the query below, that was 6 lookups per execution and about a fifth of its run time. The member ranges depend only on which relations are in the input and on the date, so Around_Statement keeps them with those ids, the relations' timestamps at a past date, and the date, and computes them again only when one of those changes. In a foreach loop, a new input relation triggers a new lookup. On the Nova Scotia extract: [out:csv(::count)]; rel(45.566265,-62.428420,45.586924,-62.388716)->.s0; node(around.s0:0)["footway"]; out count; drops from 0.50 s to 0.44 s, with identical output. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01736kJUqyDw9zM3Bkh2W2QT --- src/overpass_api/statements/around.cc | 52 +++++++++++++++++++++------ src/overpass_api/statements/around.h | 16 +++++++++ 2 files changed, 57 insertions(+), 11 deletions(-) diff --git a/src/overpass_api/statements/around.cc b/src/overpass_api/statements/around.cc index ffe19b197..ae8f5de48 100644 --- a/src/overpass_api/statements/around.cc +++ b/src/overpass_api/statements/around.cc @@ -686,35 +686,65 @@ Ranges< Uint32_Index > Around_Statement::calc_ranges(const Set& input, Resource_ return expand(calc_children(ranges(ways)), radius); } else + { + Ranges< Uint32_Index > relation_node_ranges; + Ranges< Uint31_Index > relation_way_ranges; + member_ranges(input, rman, relation_node_ranges, relation_way_ranges); + return expand( + ranges(input.nodes).union_(ranges(input.attic_nodes)).union_(relation_node_ranges).union_( + calc_children( + ranges(input.ways).union_(ranges(input.attic_ways)).union_(relation_way_ranges))), + radius); + } +} + + +void Around_Statement::member_ranges(const Set& input, Resource_Manager& rman, + Ranges< Uint32_Index >& node_ranges, Ranges< Uint31_Index >& way_ranges) const +{ + std::vector< Relation_Skeleton::Id_Type > relations; + for (auto it = input.relations.begin(); it != input.relations.end(); ++it) + for (auto it2 = it->second.begin(); it2 != it->second.end(); ++it2) + relations.push_back(it2->id); + std::vector< std::pair< Relation_Skeleton::Id_Type, uint64 > > attic_relations; + for (auto it = input.attic_relations.begin(); it != input.attic_relations.end(); ++it) + for (auto it2 = it->second.begin(); it2 != it->second.end(); ++it2) + attic_relations.push_back(std::make_pair(it2->id, it2->timestamp)); + + if (!member_ranges_known || member_ranges_timestamp != rman.get_desired_timestamp() + || member_ranges_relations != relations || member_ranges_attic_relations != attic_relations) { // The geometry of a relation is that of its direct node and way members (see // calc_lat_lons), so take the ranges from their indexes. A relation's own index // covers its whole extent, which for a long route or a boundary is a large area. - Ranges< Uint32_Index > member_node_ranges; - Ranges< Uint31_Index > member_way_ranges; + Ranges< Uint32_Index > new_node_ranges; + Ranges< Uint31_Index > new_way_ranges; if (!input.relations.empty()) { Request_Context context(this, rman); - member_node_ranges = ranges(relation_node_members( + new_node_ranges = ranges(relation_node_members( context, input.relations, {}, Ranges< Uint32_Index >::global(), {}, true).current); - member_way_ranges = ranges(relation_way_members( + new_way_ranges = ranges(relation_way_members( context, input.relations, {}, Ranges< Uint31_Index >::global(), {}, true).current); } // The same for relations at a past date, with the members calc_lat_lons uses for them. if (!input.attic_relations.empty()) { Request_Context context(this, rman); - member_node_ranges = member_node_ranges.union_(ranges(relation_node_members( + new_node_ranges = new_node_ranges.union_(ranges(relation_node_members( context, input.attic_relations, relation_node_member_indices({}, input.attic_relations)))); - member_way_ranges = member_way_ranges.union_(ranges(relation_way_members( + new_way_ranges = new_way_ranges.union_(ranges(relation_way_members( context, input.attic_relations, Ranges< Uint31_Index >::global()))); } - return expand( - ranges(input.nodes).union_(ranges(input.attic_nodes)).union_(member_node_ranges).union_( - calc_children( - ranges(input.ways).union_(ranges(input.attic_ways)).union_(member_way_ranges))), - radius); + member_node_ranges.swap(new_node_ranges); + member_way_ranges.swap(new_way_ranges); + member_ranges_relations.swap(relations); + member_ranges_attic_relations.swap(attic_relations); + member_ranges_timestamp = rman.get_desired_timestamp(); + member_ranges_known = true; } + node_ranges = member_node_ranges; + way_ranges = member_way_ranges; } diff --git a/src/overpass_api/statements/around.h b/src/overpass_api/statements/around.h index d82f24b3c..8c7f22a59 100644 --- a/src/overpass_api/statements/around.h +++ b/src/overpass_api/statements/around.h @@ -84,6 +84,22 @@ class Around_Statement : public Output_Statement Ranges< Uint32_Index > calc_ranges(const Set& input_nodes, Resource_Manager& rman) const; + private: + // The index ranges of the input relations' members, which calc_ranges needs. It runs + // several times per execution, so they are kept, together with what they depend on: + // the date, the ids of the current relations, and the ids and timestamps of the + // relations at a past date. + void member_ranges(const Set& input, Resource_Manager& rman, + Ranges< Uint32_Index >& node_ranges, Ranges< Uint31_Index >& way_ranges) const; + mutable bool member_ranges_known = false; + mutable uint64 member_ranges_timestamp = 0; + mutable std::vector< Relation_Skeleton::Id_Type > member_ranges_relations; + mutable std::vector< std::pair< Relation_Skeleton::Id_Type, uint64 > > member_ranges_attic_relations; + mutable Ranges< Uint32_Index > member_node_ranges; + mutable Ranges< Uint31_Index > member_way_ranges; + + public: + void calc_lat_lons(const Set& input_nodes, Statement& query, Resource_Manager& rman); bool is_inside(double lat, double lon) const;