From 8dba6b955ab0ed219569e369564da0f240293be9 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 21 Nov 2026 00:36:40 +0000 Subject: [PATCH] Collect relation node members and way nodes in one pass Down recursion over relations (>> always, and > when the input has relations) collected the relations' node members and the ways' nodes in two separate passes over the node blocks, then took the union. Blocks holding both kinds of node were read and decompressed twice. Add relation_and_way_node_members, which merges the two sorted id lists and the two range sets and reads the node blocks once. Use it for >> and for > on current data without a range restriction. The history path and localize's restricted-range path are unchanged. The result is the union of the two old results, as before. Nova Scotia extract, CPU time, alternating runs: rel[boundary=administrative][admin_level=4][name="Nova Scotia"]; (._;>>;); 714 ms -> 559 ms; node block reads 2570 -> 1490 (1489 distinct) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01736kJUqyDw9zM3Bkh2W2QT --- src/overpass_api/data/collect_members.cc | 37 ++++++++++++++++++++++++ src/overpass_api/data/collect_members.h | 12 ++++++++ src/overpass_api/statements/recurse.cc | 11 ++----- 3 files changed, 52 insertions(+), 8 deletions(-) diff --git a/src/overpass_api/data/collect_members.cc b/src/overpass_api/data/collect_members.cc index be67d2d7b..8e02c33ce 100644 --- a/src/overpass_api/data/collect_members.cc +++ b/src/overpass_api/data/collect_members.cc @@ -674,6 +674,26 @@ Timeless< Uint32_Index, Node_Skeleton > paired_items_range( } +Timeless< Uint32_Index, Node_Skeleton > relation_and_way_node_members( + Request_Context& context, + const std::map< Uint31_Index, std::vector< Relation_Skeleton > >& relations, + const std::map< Uint31_Index, std::vector< Attic< Relation_Skeleton > > >& attic_relations, + const std::map< Uint31_Index, std::vector< Way_Skeleton > >& ways, + const std::map< Uint31_Index, std::vector< Attic< Way_Skeleton > > >& attic_ways) +{ + // Both id lists are sorted and free of duplicates. + std::vector< Node::Id_Type > rel_ids = relation_node_member_ids(relations, attic_relations); + std::vector< Node::Id_Type > way_ids = way_nd_ids(ways, attic_ways, 0); + context.get_health_guard().check(); + std::vector< Node::Id_Type > ids; + ids.reserve(rel_ids.size() + way_ids.size()); + std::set_union(rel_ids.begin(), rel_ids.end(), way_ids.begin(), way_ids.end(), std::back_inserter(ids)); + + return paired_items_range(context, ids, + relation_node_member_indices(relations, attic_relations).union_(way_nd_indices(ways, attic_ways))); +} + + Timeless< Uint32_Index, Node_Skeleton > relation_node_members (Request_Context& context, const std::map< Uint31_Index, std::vector< Relation_Skeleton > >& relations, @@ -1065,6 +1085,23 @@ void add_nw_member_objects(Request_Context& context, const Set& input_set, Set& { Ranges< Uint32_Index > ranges_32(ranges_32_ ? *ranges_32_ : Ranges< Uint32_Index >::global()); Ranges< Uint31_Index > ranges_31(ranges_31_ ? *ranges_31_ : Ranges< Uint31_Index >::global()); + + if (context.get_desired_timestamp() == NOW && !ranges_32_) + { + // Collect the relations' node members and the ways' nodes in one pass over the node blocks. + relation_way_members(context, input_set.relations, {}, ranges_31, {}, true) + .swap(into.ways, into.attic_ways); + std::map< Uint31_Index, std::vector< Way_Skeleton > > source_ways = input_set.ways; + sort_second(source_ways); + sort_second(into.ways); + indexed_set_union(source_ways, into.ways); + Timeless< Uint32_Index, Node_Skeleton > nodes = relation_and_way_node_members( + context, input_set.relations, input_set.attic_relations, source_ways, {}); + nodes.current.swap(into.nodes); + sort_second(into.nodes); + return; + } + Timeless< Uint32_Index, Node_Skeleton > rel_nodes = relation_node_members(context, input_set.relations, input_set.attic_relations, ranges_32, {}, true); diff --git a/src/overpass_api/data/collect_members.h b/src/overpass_api/data/collect_members.h index 9abd3b8c0..e2e99422a 100644 --- a/src/overpass_api/data/collect_members.h +++ b/src/overpass_api/data/collect_members.h @@ -744,6 +744,18 @@ Ranges< Uint31_Index > relation_way_member_indices Ranges< Uint32_Index > calc_node_children_ranges(const std::vector< uint32 >& way_rel_idxs); +/* The node members of the relations and the nodes of the ways, read in a single pass + over the node blocks. Equivalent to the union of relation_node_members(relations) and + way_members(ways) with global ranges, but blocks holding both kinds of node are read + and decompressed once instead of twice. */ +Timeless< Uint32_Index, Node_Skeleton > relation_and_way_node_members( + Request_Context& context, + const std::map< Uint31_Index, std::vector< Relation_Skeleton > >& relations, + const std::map< Uint31_Index, std::vector< Attic< Relation_Skeleton > > >& attic_relations, + const std::map< Uint31_Index, std::vector< Way_Skeleton > >& ways, + const std::map< Uint31_Index, std::vector< Attic< Way_Skeleton > > >& attic_ways); + + Ranges< Uint32_Index > relation_node_member_indices( const std::map< Uint31_Index, std::vector< Relation_Skeleton > >& current_rels, const std::map< Uint31_Index, std::vector< Attic< Relation_Skeleton > > >& attic_rels); diff --git a/src/overpass_api/statements/recurse.cc b/src/overpass_api/statements/recurse.cc index e5d5de1c3..b42398ec5 100644 --- a/src/overpass_api/statements/recurse.cc +++ b/src/overpass_api/statements/recurse.cc @@ -1404,15 +1404,10 @@ void Recurse_Statement::execute(Resource_Manager& rman) source_ways.set_union(result_ways.swap(into.ways, into.attic_ways).sort()); result_ways.swap(into.ways, into.attic_ways); } - Timeless< Uint32_Index, Node_Skeleton > rel_nodes = relation_node_members( - context, into.relations, into.attic_relations, Ranges< Uint32_Index >::global(), {}, true); + // The relations' node members and the ways' nodes, in one pass over the node blocks. + Timeless< Uint32_Index, Node_Skeleton > rel_nodes = relation_and_way_node_members( + context, into.relations, into.attic_relations, source_ways.current, source_ways.attic); rel_nodes.sort(); - { - Timeless< Uint32_Index, Node_Skeleton > more_nodes - = way_members(context, source_ways.current, source_ways.attic, 0, - Ranges< Uint32_Index >::global(), {}, true); - rel_nodes.set_union(more_nodes.sort()); - } keep_matching_skeletons(rel_nodes.current, rel_nodes.attic, rman.get_desired_timestamp()); rel_nodes.swap(into.nodes, into.attic_nodes); }