From ce107a7407df181f9278144036673898925808cc Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 21 Nov 2026 08:25:31 +0000 Subject: [PATCH 1/2] user: compute the ids of touched objects once per query For uid_touched and user_touched, User_Constraint scanned all of the users' current and attic metadata (touched_ids_by_users) in get_node_ids, get_way_ids and get_relation_ids, and again in filter. When the query engine used both, as it does when these filters choose the candidates, every metadata block was decompressed twice. Inside a loop, both scans repeated in every iteration. The touched ids depend only on the users, so they are the same for the whole query. The constraint now keeps them after first use, and both callers share them. A list longer than 2M ids (16 MB) is not kept: it is handed over or dropped after use, as before, so a prolific user's ids never stay in memory longer than they did. calc_ranges_both, which computed the same ranges as calc_ranges_32 and calc_ranges_31 together, is no longer needed. On the Nova Scotia history database, with N a user id: [out:csv(::count)]; nwr(uid_touched:N)(44.60,-63.66,44.70,-63.52); out count; drops from 2.02 s to 1.20 s, and a foreach over 76 cafes running nwr(around.it:100)(uid_touched:N) from 126 s to 6 s, with identical output. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01736kJUqyDw9zM3Bkh2W2QT --- src/overpass_api/statements/user.cc | 151 +++++++++++++++++++--------- 1 file changed, 102 insertions(+), 49 deletions(-) diff --git a/src/overpass_api/statements/user.cc b/src/overpass_api/statements/user.cc index a4fea93b6..5f7805f84 100644 --- a/src/overpass_api/statements/user.cc +++ b/src/overpass_api/statements/user.cc @@ -52,6 +52,34 @@ class User_Constraint : public Query_Constraint private: User_Statement* user; + + // The ids of the objects the users touched depend only on the users, so they are + // the same for the whole query. They are kept after first use and shared by + // get_*_ids and filter, which otherwise each read all of the users' metadata. + // Lists longer than max_kept_ids are not kept, so a prolific user's list never + // stays in memory longer than before. + static const std::size_t max_kept_ids = 2*1024*1024; + bool node_ids_known = false; + bool way_ids_known = false; + bool relation_ids_known = false; + std::vector< Node_Skeleton::Id_Type > touched_node_ids; + std::vector< Way_Skeleton::Id_Type > touched_way_ids; + std::vector< Relation_Skeleton::Id_Type > touched_relation_ids; + + const std::vector< Node_Skeleton::Id_Type >& touched_nodes(Resource_Manager& rman); + const std::vector< Way_Skeleton::Id_Type >& touched_ways(Resource_Manager& rman); + const std::vector< Relation_Skeleton::Id_Type >& touched_relations(Resource_Manager& rman); + + // Drops a list that is too long to keep, so that it is computed again when needed. + template< typename Id_Type > + static void release_if_large(std::vector< Id_Type >& ids, bool& known) + { + if (ids.size() > max_kept_ids) + { + std::vector< Id_Type >().swap(ids); + known = false; + } + } }; @@ -186,31 +214,6 @@ void calc_ranges_31 } -void calc_ranges_both - (Ranges< Uint32_Index >& node_req, Ranges< Uint31_Index >& other_req, - const std::set< Uint32_Index >& user_ids, Transaction& transaction) -{ - - Block_Backend< Uint32_Index, Uint31_Index, std::set< Uint32_Index >::const_iterator > user_db - (transaction.data_index(meta_settings().USER_INDICES)); - for (auto user_it = user_db.discrete_begin(user_ids.begin(), user_ids.end()); - !(user_it == user_db.discrete_end()); ++user_it) - { - if ((user_it.object().val() & 0x80000000) == 0) - { - node_req.push_back(Uint32_Index(user_it.object().val()), Uint32_Index(user_it.object().val() + 0x100)); - other_req.push_back(Uint31_Index(user_it.object().val()), Uint31_Index(user_it.object().val() + 0x100)); - } - else if ((user_it.object().val() & 0xff) == 0) - other_req.push_back(Uint31_Index(user_it.object().val()), Uint31_Index(user_it.object().val() + 0x100)); - else - other_req.push_back(Uint31_Index(user_it.object().val()), Uint31_Index(user_it.object().val() + 1)); - } - node_req.sort(); - other_req.sort(); -} - - void User_Constraint::filter(const Statement& query, Resource_Manager& rman, Set& into) { std::set< Uint32_Index > user_ids = user->get_ids(*rman.get_transaction()); @@ -235,25 +238,21 @@ void User_Constraint::filter(const Statement& query, Resource_Manager& rman, Set } else { - Ranges< Uint32_Index > node_ranges; - Ranges< Uint31_Index > other_ranges; - calc_ranges_both(node_ranges, other_ranges, user->get_ids(*rman.get_transaction()), *rman.get_transaction()); - if (!into.nodes.empty() || !into.attic_nodes.empty()) Timeless< Uint32_Index, Node_Skeleton >{ into.nodes, into.attic_nodes }.filter_by_id( - touched_ids_by_users< Uint32_Index, Node_Skeleton >( - rman, node_ranges, user->get_ids(*rman.get_transaction()))) + touched_nodes(rman)) .swap(into.nodes, into.attic_nodes); + release_if_large(touched_node_ids, node_ids_known); if (!into.ways.empty() || !into.attic_ways.empty()) Timeless< Uint31_Index, Way_Skeleton >{ into.ways, into.attic_ways }.filter_by_id( - touched_ids_by_users< Uint31_Index, Way_Skeleton >( - rman, other_ranges, user->get_ids(*rman.get_transaction()))) + touched_ways(rman)) .swap(into.ways, into.attic_ways); + release_if_large(touched_way_ids, way_ids_known); if (!into.relations.empty() || !into.attic_relations.empty()) Timeless< Uint31_Index, Relation_Skeleton >{ into.relations, into.attic_relations }.filter_by_id( - touched_ids_by_users< Uint31_Index, Relation_Skeleton >( - rman, other_ranges, user->get_ids(*rman.get_transaction()))) + touched_relations(rman)) .swap(into.relations, into.attic_relations); + release_if_large(touched_relation_ids, relation_ids_known); } into.areas.clear(); @@ -434,15 +433,61 @@ bool User_Constraint::get_ranges(Resource_Manager& rman, Ranges< Uint31_Index >& } -bool User_Constraint::get_node_ids(Resource_Manager& rman, std::vector< Node_Skeleton::Id_Type >& ids) +const std::vector< Node_Skeleton::Id_Type >& User_Constraint::touched_nodes(Resource_Manager& rman) { - if (user->get_criterion() == User_Statement::touched) + if (!node_ids_known) { Ranges< Uint32_Index > node_ranges; calc_ranges_32(node_ranges, user->get_ids(*rman.get_transaction()), *rman.get_transaction()); + touched_node_ids = touched_ids_by_users< Uint32_Index, Node_Skeleton >( + rman, node_ranges, user->get_ids(*rman.get_transaction())); + node_ids_known = true; + } + return touched_node_ids; +} - touched_ids_by_users< Uint32_Index, Node_Skeleton >( - rman, node_ranges, user->get_ids(*rman.get_transaction())).swap(ids); + +const std::vector< Way_Skeleton::Id_Type >& User_Constraint::touched_ways(Resource_Manager& rman) +{ + if (!way_ids_known) + { + Ranges< Uint31_Index > other_ranges; + calc_ranges_31(other_ranges, user->get_ids(*rman.get_transaction()), *rman.get_transaction()); + touched_way_ids = touched_ids_by_users< Uint31_Index, Way_Skeleton >( + rman, other_ranges, user->get_ids(*rman.get_transaction())); + way_ids_known = true; + } + return touched_way_ids; +} + + +const std::vector< Relation_Skeleton::Id_Type >& User_Constraint::touched_relations(Resource_Manager& rman) +{ + if (!relation_ids_known) + { + Ranges< Uint31_Index > other_ranges; + calc_ranges_31(other_ranges, user->get_ids(*rman.get_transaction()), *rman.get_transaction()); + touched_relation_ids = touched_ids_by_users< Uint31_Index, Relation_Skeleton >( + rman, other_ranges, user->get_ids(*rman.get_transaction())); + relation_ids_known = true; + } + return touched_relation_ids; +} + + +bool User_Constraint::get_node_ids(Resource_Manager& rman, std::vector< Node_Skeleton::Id_Type >& ids) +{ + if (user->get_criterion() == User_Statement::touched) + { + touched_nodes(rman); + if (touched_node_ids.size() > max_kept_ids) + { + // Too long to keep: hand it over, as before this list was kept. + ids.swap(touched_node_ids); + node_ids_known = false; + } + else + ids = touched_node_ids; return true; } return false; @@ -453,11 +498,15 @@ bool User_Constraint::get_way_ids(Resource_Manager& rman, std::vector< Way_Skele { if (user->get_criterion() == User_Statement::touched) { - Ranges< Uint31_Index > other_ranges; - calc_ranges_31(other_ranges, user->get_ids(*rman.get_transaction()), *rman.get_transaction()); - - touched_ids_by_users< Uint31_Index, Way_Skeleton >( - rman, other_ranges, user->get_ids(*rman.get_transaction())).swap(ids); + touched_ways(rman); + if (touched_way_ids.size() > max_kept_ids) + { + // Too long to keep: hand it over, as before this list was kept. + ids.swap(touched_way_ids); + way_ids_known = false; + } + else + ids = touched_way_ids; return true; } return false; @@ -468,11 +517,15 @@ bool User_Constraint::get_relation_ids(Resource_Manager& rman, std::vector< Rela { if (user->get_criterion() == User_Statement::touched) { - Ranges< Uint31_Index > other_ranges; - calc_ranges_31(other_ranges, user->get_ids(*rman.get_transaction()), *rman.get_transaction()); - - touched_ids_by_users< Uint31_Index, Relation_Skeleton >( - rman, other_ranges, user->get_ids(*rman.get_transaction())).swap(ids); + touched_relations(rman); + if (touched_relation_ids.size() > max_kept_ids) + { + // Too long to keep: hand it over, as before this list was kept. + ids.swap(touched_relation_ids); + relation_ids_known = false; + } + else + ids = touched_relation_ids; return true; } return false; From a95d1a59517d05cbfd2a662d80417d522de1415b Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 21 Nov 2026 19:45:24 +0000 Subject: [PATCH 2/2] user: keep touched ids for the whole query, whatever their number The previous commit kept the touched ids only for lists of up to 2M ids, so the most prolific users, likely the ones queried most, still had their metadata read twice. Upstream already holds a full id list from get_node_ids while filter builds a second one, so keeping one list costs no more than that for nodes. The way and relation lists are smaller, and the bound is removed. On the Nova Scotia history database, for the user with the most edits in the extract (N their user id): [out:csv(::count)]; nwr(uid_touched:N)(44.60,-63.66,44.70,-63.52); out count; drops from 1.99 s to 1.22 s (it was 2.20 s with the bound). Peak RSS goes from 75.9 to 85.1 MB. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01736kJUqyDw9zM3Bkh2W2QT --- src/overpass_api/statements/user.cc | 47 ++--------------------------- 1 file changed, 3 insertions(+), 44 deletions(-) diff --git a/src/overpass_api/statements/user.cc b/src/overpass_api/statements/user.cc index 5f7805f84..2e61061bb 100644 --- a/src/overpass_api/statements/user.cc +++ b/src/overpass_api/statements/user.cc @@ -56,9 +56,6 @@ class User_Constraint : public Query_Constraint // The ids of the objects the users touched depend only on the users, so they are // the same for the whole query. They are kept after first use and shared by // get_*_ids and filter, which otherwise each read all of the users' metadata. - // Lists longer than max_kept_ids are not kept, so a prolific user's list never - // stays in memory longer than before. - static const std::size_t max_kept_ids = 2*1024*1024; bool node_ids_known = false; bool way_ids_known = false; bool relation_ids_known = false; @@ -69,17 +66,6 @@ class User_Constraint : public Query_Constraint const std::vector< Node_Skeleton::Id_Type >& touched_nodes(Resource_Manager& rman); const std::vector< Way_Skeleton::Id_Type >& touched_ways(Resource_Manager& rman); const std::vector< Relation_Skeleton::Id_Type >& touched_relations(Resource_Manager& rman); - - // Drops a list that is too long to keep, so that it is computed again when needed. - template< typename Id_Type > - static void release_if_large(std::vector< Id_Type >& ids, bool& known) - { - if (ids.size() > max_kept_ids) - { - std::vector< Id_Type >().swap(ids); - known = false; - } - } }; @@ -242,17 +228,14 @@ void User_Constraint::filter(const Statement& query, Resource_Manager& rman, Set Timeless< Uint32_Index, Node_Skeleton >{ into.nodes, into.attic_nodes }.filter_by_id( touched_nodes(rman)) .swap(into.nodes, into.attic_nodes); - release_if_large(touched_node_ids, node_ids_known); if (!into.ways.empty() || !into.attic_ways.empty()) Timeless< Uint31_Index, Way_Skeleton >{ into.ways, into.attic_ways }.filter_by_id( touched_ways(rman)) .swap(into.ways, into.attic_ways); - release_if_large(touched_way_ids, way_ids_known); if (!into.relations.empty() || !into.attic_relations.empty()) Timeless< Uint31_Index, Relation_Skeleton >{ into.relations, into.attic_relations }.filter_by_id( touched_relations(rman)) .swap(into.relations, into.attic_relations); - release_if_large(touched_relation_ids, relation_ids_known); } into.areas.clear(); @@ -479,15 +462,7 @@ bool User_Constraint::get_node_ids(Resource_Manager& rman, std::vector< Node_Ske { if (user->get_criterion() == User_Statement::touched) { - touched_nodes(rman); - if (touched_node_ids.size() > max_kept_ids) - { - // Too long to keep: hand it over, as before this list was kept. - ids.swap(touched_node_ids); - node_ids_known = false; - } - else - ids = touched_node_ids; + ids = touched_nodes(rman); return true; } return false; @@ -498,15 +473,7 @@ bool User_Constraint::get_way_ids(Resource_Manager& rman, std::vector< Way_Skele { if (user->get_criterion() == User_Statement::touched) { - touched_ways(rman); - if (touched_way_ids.size() > max_kept_ids) - { - // Too long to keep: hand it over, as before this list was kept. - ids.swap(touched_way_ids); - way_ids_known = false; - } - else - ids = touched_way_ids; + ids = touched_ways(rman); return true; } return false; @@ -517,15 +484,7 @@ bool User_Constraint::get_relation_ids(Resource_Manager& rman, std::vector< Rela { if (user->get_criterion() == User_Statement::touched) { - touched_relations(rman); - if (touched_relation_ids.size() > max_kept_ids) - { - // Too long to keep: hand it over, as before this list was kept. - ids.swap(touched_relation_ids); - relation_ids_known = false; - } - else - ids = touched_relation_ids; + ids = touched_relations(rman); return true; } return false;