From d4cc14a807427266d0d452fdc18c85eb419b3bba Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 26 Nov 2026 16:16:43 +0000 Subject: [PATCH] query: build the late tag check's ids one cell at a time Query_Statement::filter_by_tags checks tags late for candidates collected by area: generate_ids_by_coarse copied the id of every candidate into a map of per-cell id lists, and filter_ids_by_tags then narrowed each list to the matching ids. All candidate ids were held at once, alongside the candidates themselves, although only one cell's are needed at a time. filter_by_tags now creates the map's entries (one per cell, still needed for the range query and filter_by_ids) without ids, and fill_cell_ids fills a cell's ids from the items, and attic items at a past date, just before that cell is filtered. Each list then shrinks to its matches, so the map ends up holding only those. On a US northeast full-history extract (Geofabrik), warm cache, 4-core VM, median of 3 alternating runs: node["amenity"](42.2,-71.3,42.5,-70.9); out meta; peak RSS 174 MB -> 137 MB, CPU 1.46 s -> 1.29 s Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01736kJWpJaGbt8ML6jz9N35 --- src/overpass_api/statements/query.cc | 38 +++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/src/overpass_api/statements/query.cc b/src/overpass_api/statements/query.cc index 76b2d190f..0f96a2800 100644 --- a/src/overpass_api/statements/query.cc +++ b/src/overpass_api/statements/query.cc @@ -450,6 +450,23 @@ void filter_by_ids( } +template< class TIndex, class TObject > +void fill_cell_ids(std::vector< typename TObject::Id_Type >& ids, + typename std::map< TIndex, std::vector< TObject > >::const_iterator& item_it, + const std::map< TIndex, std::vector< TObject > >& items, uint32 coarse_index) +{ + while (item_it != items.end() && (item_it->first.val() & 0x7fffff00) == coarse_index) + { + for (typename std::vector< TObject >::const_iterator eit = item_it->second.begin(); + eit != item_it->second.end(); ++eit) + ids.push_back(eit->id); + ++item_it; + } + std::sort(ids.begin(), ids.end()); + ids.erase(std::unique(ids.begin(), ids.end()), ids.end()); +} + + template< class TIndex, class TObject > void Query_Statement::filter_by_tags (std::map< TIndex, std::vector< TObject > >& items, @@ -461,11 +478,23 @@ void Query_Statement::filter_by_tags && key_nregexes.empty() && key_nvalues.empty()) return; - // generate set of relevant coarse indices + // One entry per coarse index, but its ids are filled in only when that cell is filtered, so the + // candidates' ids are never all held at once. std::map< uint32, std::vector< typename TObject::Id_Type > > ids_by_coarse; - generate_ids_by_coarse(ids_by_coarse, items); + for (typename std::map< TIndex, std::vector< TObject > >::const_iterator it = items.begin(); it != items.end(); ++it) + ids_by_coarse.insert(ids_by_coarse.end(), std::make_pair(it->first.val() & 0x7fffff00, + std::vector< typename TObject::Id_Type >())); if (timestamp != NOW) - generate_ids_by_coarse(ids_by_coarse, *attic_items); + { + for (typename std::map< TIndex, std::vector< Attic< TObject > > >::const_iterator it = attic_items->begin(); + it != attic_items->end(); ++it) + ids_by_coarse.insert(std::make_pair(it->first.val() & 0x7fffff00, + std::vector< typename TObject::Id_Type >())); + } + typename std::map< TIndex, std::vector< TObject > >::const_iterator cell_item_it = items.begin(); + typename std::map< TIndex, std::vector< Attic< TObject > > >::const_iterator cell_attic_it; + if (attic_items) + cell_attic_it = attic_items->begin(); // formulate range query Ranges< Tag_Index_Local > ranges = formulate_range_query(ids_by_coarse); @@ -501,6 +530,7 @@ void Query_Statement::filter_by_tags coarse_count = 0; rman.health_check(*this); } + fill_cell_ids(it->second, cell_item_it, items, it->first); filter_ids_by_tags(key_union, regkey_regexes, items_db, tag_it, it->first, it->second); } } @@ -518,6 +548,8 @@ void Query_Statement::filter_by_tags coarse_count = 0; rman.health_check(*this); } + fill_cell_ids(it->second, cell_item_it, items, it->first); + fill_cell_ids(it->second, cell_attic_it, *attic_items, it->first); filter_ids_by_tags(key_union, regkey_regexes, timestamp, items_db, tag_it, attic_items_db, attic_tag_it, it->first, it->second); }