From 77135cac7c69547b642b5e5f8f58cbdb460fc7e9 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 26 Nov 2026 21:56:04 +0000 Subject: [PATCH] query: drop tag-index candidates outside the query's ranges during the lookup With a bbox of 1 square degree or more, a query statement with tag filters looks up ids in the global tag index (collect_ids) and filters by the bbox afterwards. It looked keys up in the order written, and each further key read its whole index range, binary-searching every entry against the candidates so far. For way["maxheight"]["highway"] over open ocean that meant collecting every maxheight way in the database and then reading the whole highway index, though none of them could be in the box. Query_Statement::execute now computes the constraints' node, way and relation ranges before the tag lookup (they don't depend on it) and passes them to progress_1 and collect_ids. At the current date: - keep_ids_inside drops candidates whose cell lies outside those ranges after each lookup; when none are left, the lookup stops. - The key and key-value lookups run smallest first, counted in blocks of the global tag index (Block_Backend::count_blocks, from the block index alone). - When checking the remaining tags late would read fewer local tag blocks than the remaining lookups would read global ones, the lookup stops and the query checks all tags late (check_keys_late = prefer_ranges). The global tag index stores coarse indices, so a candidate is kept if its cell overlaps the ranges, as intersect_ranges does. On a US northeast full-history extract (Geofabrik), warm cache, 4-core VM, median CPU of 5 runs: [out:json][timeout:90]; (way["maxheight"]["highway"](39.0,-70.0,40.0,-69.0); way["bridge"]["maxheight"](39.0,-70.0,40.0,-69.0); way["tunnel"]["maxheight"](39.0,-70.0,40.0,-69.0); node["maxheight"]["highway"](39.0,-70.0,40.0,-69.0); ); out center; 0.24 s -> 0.00 s Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01736kJWpJaGbt8ML6jz9N35 --- src/overpass_api/data/tags_global_reader.h | 150 ++++++++++++++++++++- src/overpass_api/statements/query.cc | 57 ++++---- src/template_db/block_backend.h | 10 ++ 3 files changed, 176 insertions(+), 41 deletions(-) diff --git a/src/overpass_api/data/tags_global_reader.h b/src/overpass_api/data/tags_global_reader.h index 929fc930d..fa56310dc 100644 --- a/src/overpass_api/data/tags_global_reader.h +++ b/src/overpass_api/data/tags_global_reader.h @@ -361,7 +361,64 @@ void filter_id_list( class Statement; -template< typename Skeleton, typename Id_Type > +// Blocks a late tag check of these candidates reads: their cells' skeletons and local tags. +template< typename Skeleton, typename Id_Type, typename Index > +uint64 count_late_check_blocks(const std::vector< std::pair< Id_Type, Uint31_Index > >& ids, Resource_Manager& rman) +{ + std::vector< uint32 > cells; + cells.reserve(ids.size()); + for (auto it = ids.begin(); it != ids.end(); ++it) + cells.push_back(it->second.val() & 0x7fffff00); + std::sort(cells.begin(), cells.end()); + cells.erase(std::unique(cells.begin(), cells.end()), cells.end()); + + Ranges< Tag_Index_Local > tag_ranges; + Ranges< Index > skeleton_ranges; + for (auto it = cells.begin(); it != cells.end(); ++it) + { + tag_ranges.push_back({ *it, "", "" }, { *it + 0x100, "", "" }); + skeleton_ranges.push_back(Index(*it), Index(*it + 0x100)); + } + tag_ranges.sort(); + skeleton_ranges.sort(); + Block_Backend< Tag_Index_Local, Id_Type > local_tags_db + (rman.get_transaction()->data_index(current_local_tags_file_properties< Skeleton >())); + Block_Backend< Index, Skeleton > skeletons_db + (rman.get_transaction()->data_index(current_skeleton_file_properties< Skeleton >())); + return local_tags_db.count_blocks(tag_ranges) + skeletons_db.count_blocks(skeleton_ranges); +} + + +// The global tag index stores coarse indices (multiples of 0x100): keep ids whose cell overlaps ranges. +template< typename Id_Type, typename Index > +void keep_ids_inside(std::vector< std::pair< Id_Type, Uint31_Index > >& ids, const Ranges< Index >& ranges) +{ + std::vector< std::pair< Index, Index > > bounds; + for (auto it = ranges.begin(); it != ranges.end(); ++it) + bounds.push_back(std::make_pair(it.lower_bound(), it.upper_bound())); + std::sort(bounds.begin(), bounds.end()); + std::vector< std::pair< Index, Index > > merged; + for (auto it = bounds.begin(); it != bounds.end(); ++it) + { + if (!merged.empty() && !(merged.back().second < it->first)) + merged.back().second = std::max(merged.back().second, it->second); + else + merged.push_back(*it); + } + std::vector< std::pair< Id_Type, Uint31_Index > > kept; + for (auto it = ids.begin(); it != ids.end(); ++it) + { + Index idx(it->second.val()); + auto b = std::upper_bound(merged.begin(), merged.end(), idx, + [](const Index& x, const std::pair< Index, Index >& r) { return x < r.second; }); + if (b != merged.end() && b->first < Index(idx.val() + 0x100)) + kept.push_back(*it); + } + kept.swap(ids); +} + + +template< typename Skeleton, typename Id_Type, typename Index > std::vector< std::pair< Id_Type, Uint31_Index > > collect_ids( const std::vector< std::string >& keys, const std::vector< std::pair< std::string, std::string > >& key_values, @@ -369,8 +426,10 @@ std::vector< std::pair< Id_Type, Uint31_Index > > collect_ids( const std::vector< std::pair< Regular_Expression*, Regular_Expression* > >& regkey_regexes, const File_Properties& file_prop, const File_Properties& attic_file_prop, Resource_Manager& rman, const Statement& stmt, - uint64 timestamp, Query_Filter_Strategy check_keys_late, bool& result_valid) + uint64 timestamp, Query_Filter_Strategy check_keys_late, bool& result_valid, bool& check_rest_late, + const Ranges< Index >* spatial_ranges) { + check_rest_late = false; if (key_values.empty() && keys.empty() && key_regexes.empty() && regkey_regexes.empty()) return std::vector< std::pair< Id_Type, Uint31_Index > >(); @@ -381,11 +440,86 @@ std::vector< std::pair< Id_Type, Uint31_Index > > collect_ids( new Block_Backend< Tag_Index_Global, Attic< Tag_Object_Global< Id_Type > > > (rman.get_transaction()->data_index(&attic_file_prop))); - // Handle simple Key-Value pairs std::vector< std::pair< Id_Type, Uint31_Index > > new_ids; bool filtered = false; result_valid = false; + if (timestamp == NOW) + { + // Smallest lookups first; stop when checking the remaining tags late reads fewer blocks. + struct Lookup + { + Ranges< Tag_Index_Global > ranges; + Regular_Expression* value_regex; + bool limit_size; + uint64 blocks; + }; + std::vector< Lookup > lookups; + for (auto it = key_values.begin(); it != key_values.end(); ++it) + lookups.push_back(Lookup{ get_kv_req(it->first, it->second), 0, + check_keys_late == prefer_ranges || check_keys_late == ids_useful, 0 }); + if (check_keys_late != prefer_ranges) + { + for (auto it = keys.begin(); it != keys.end(); ++it) + lookups.push_back(Lookup{ get_k_req(*it), 0, check_keys_late == ids_useful, 0 }); + for (auto it = key_regexes.begin(); it != key_regexes.end(); ++it) + lookups.push_back(Lookup{ get_k_req(it->first), it->second, check_keys_late == ids_useful, 0 }); + } + uint64 remaining_blocks = 0; + for (auto it = lookups.begin(); it != lookups.end(); ++it) + { + it->blocks = tags_db.count_blocks(it->ranges); + remaining_blocks += it->blocks; + } + std::stable_sort(lookups.begin(), lookups.end(), + [](const Lookup& lhs, const Lookup& rhs) { return lhs.blocks < rhs.blocks; }); + + for (auto it = lookups.begin(); it != lookups.end(); ++it) + { + if (filtered && count_late_check_blocks< Skeleton, Id_Type, Index >(new_ids, rman) < remaining_blocks) + { + check_rest_late = true; + result_valid = true; + return new_ids; + } + if (it->value_regex) + filter_id_list(new_ids, filtered, tags_db.range_begin(it->ranges), tags_db.range_end(), + Trivial_Regex(), *it->value_regex, it->limit_size); + else + filter_id_list(new_ids, filtered, tags_db.range_begin(it->ranges), tags_db.range_end(), + Trivial_Regex(), Trivial_Regex(), it->limit_size); + if (!filtered) + return new_ids; + if (spatial_ranges) + keep_ids_inside(new_ids, *spatial_ranges); + if (new_ids.empty()) + { + result_valid = true; + return new_ids; + } + remaining_blocks -= it->blocks; + rman.health_check(stmt); + } + + if (check_keys_late != prefer_ranges) + { + for (auto it = regkey_regexes.begin(); it != regkey_regexes.end(); ++it) + { + Ranges< Tag_Index_Global > ranges = get_regk_req< Skeleton >(it->first, rman, stmt); + filter_id_list(new_ids, filtered, tags_db.range_begin(ranges), tags_db.range_end(), + *it->first, *it->second, check_keys_late == ids_useful); + if (!filtered) + return new_ids; + rman.health_check(stmt); + } + } + + result_valid = true; + return new_ids; + } + + // Handle simple Key-Value pairs + for (std::vector< std::pair< std::string, std::string > >::const_iterator kvit = key_values.begin(); kvit != key_values.end(); ++kvit) { @@ -771,7 +905,7 @@ void progress_1( const std::vector< std::pair< Regular_Expression*, Regular_Expression* > >& regkey_nregexes, Id_Constraint< Id_Type >& ids, std::vector< Index >& range_vec, uint64 timestamp, Query_Filter_Strategy& check_keys_late, - Resource_Manager& rman, const Statement& stmt) + Resource_Manager& rman, const Statement& stmt, const Ranges< Index >* spatial_ranges = 0) { File_Properties* file_prop = current_global_tags_file_properties< Skeleton >(); File_Properties* attic_file_prop = attic_global_tags_file_properties< Skeleton >(); @@ -783,11 +917,13 @@ void progress_1( && (!keys.empty() || !key_regexes.empty() || !regkey_regexes.empty()))) { bool result_valid = true; + bool check_rest_late = false; std::vector< std::pair< Id_Type, Uint31_Index > > id_idxs = - collect_ids< Skeleton, Id_Type >( + collect_ids< Skeleton, Id_Type, Index >( keys, key_values, key_regexes, regkey_regexes, - *file_prop, *attic_file_prop, rman, stmt, timestamp, check_keys_late, result_valid); - if (check_keys_late == ids_useful && !result_valid) + *file_prop, *attic_file_prop, rman, stmt, timestamp, check_keys_late, result_valid, check_rest_late, + spatial_ranges); + if ((check_keys_late == ids_useful && !result_valid) || check_rest_late) check_keys_late = prefer_ranges; ids.invert = !result_valid; diff --git a/src/overpass_api/statements/query.cc b/src/overpass_api/statements/query.cc index 76b2d190f..ffd35eef0 100644 --- a/src/overpass_api/statements/query.cc +++ b/src/overpass_api/statements/query.cc @@ -950,11 +950,25 @@ void Query_Statement::execute(Resource_Manager& rman) std::vector< Uint31_Index > way_range_vec_31; std::vector< Uint31_Index > relation_range_vec_31; + // Known before the tag lookup, which drops candidates outside them. + Ranges< Uint32_Index > node_ranges = Ranges< Uint32_Index >::global(); + Ranges< Uint31_Index > way_ranges = Ranges< Uint31_Index >::global(); + Ranges< Uint31_Index > rel_ranges = Ranges< Uint31_Index >::global(); + for (std::vector< Query_Constraint* >::iterator it = constraints.begin(); it != constraints.end(); ++it) + { + if (type & QUERY_NODE) + node_ranges.intersect((*it)->get_node_ranges(rman)).swap(node_ranges); + if (type & QUERY_WAY) + way_ranges.intersect((*it)->get_way_ranges(rman)).swap(way_ranges); + if (type & QUERY_RELATION) + rel_ranges.intersect((*it)->get_relation_ranges(rman)).swap(rel_ranges); + } + if (type & QUERY_NODE) { progress_1< Node_Skeleton, Node::Id_Type, Uint32_Index >( keys, key_values, key_regexes, regkey_regexes, key_nvalues, key_nregexes, regkey_nregexes, - node_ids, range_vec_32, timestamp, check_keys_late, rman, *this); + node_ids, range_vec_32, timestamp, check_keys_late, rman, *this, node_ranges.is_global() ? 0 : &node_ranges); if (node_ids.empty()) node_answer_state = data_collected; collect_nodes(node_ids, node_answer_state, into, rman); @@ -963,7 +977,7 @@ void Query_Statement::execute(Resource_Manager& rman) { progress_1< Way_Skeleton, Way::Id_Type, Uint31_Index >( keys, key_values, key_regexes, regkey_regexes, key_nvalues, key_nregexes, regkey_nregexes, - way_ids, way_range_vec_31, timestamp, check_keys_late, rman, *this); + way_ids, way_range_vec_31, timestamp, check_keys_late, rman, *this, way_ranges.is_global() ? 0 : &way_ranges); if (way_ids.empty()) way_answer_state = data_collected; collect_elems(QUERY_WAY, way_ids, way_answer_state, into, rman); @@ -974,7 +988,7 @@ void Query_Statement::execute(Resource_Manager& rman) { progress_1< Relation_Skeleton, Relation::Id_Type, Uint31_Index >( keys, key_values, key_regexes, regkey_regexes, key_nvalues, key_nregexes, regkey_nregexes, - relation_ids, relation_range_vec_31, timestamp, check_keys_late, rman, *this); + relation_ids, relation_range_vec_31, timestamp, check_keys_late, rman, *this, rel_ranges.is_global() ? 0 : &rel_ranges); if (relation_ids.empty()) relation_answer_state = data_collected; collect_elems(QUERY_RELATION, relation_ids, relation_answer_state, into, rman); @@ -1063,37 +1077,12 @@ void Query_Statement::execute(Resource_Manager& rman) } } - Ranges< Uint32_Index > node_ranges = Ranges< Uint32_Index >::global(); - Ranges< Uint31_Index > way_ranges = Ranges< Uint31_Index >::global(); - Ranges< Uint31_Index > rel_ranges = Ranges< Uint31_Index >::global(); - - if ((type & QUERY_NODE) && node_answer_state < data_collected) - { - for (std::vector< Query_Constraint* >::iterator it = constraints.begin(); - it != constraints.end(); ++it) - node_ranges.intersect((*it)->get_node_ranges(rman)).swap(node_ranges); - - if (!range_vec_32.empty()) - intersect_ranges(node_ranges, range_vec_32).swap(node_ranges); - } - if ((type & QUERY_WAY) && way_answer_state < data_collected) - { - for (std::vector< Query_Constraint* >::iterator it = constraints.begin(); - it != constraints.end(); ++it) - way_ranges.intersect((*it)->get_way_ranges(rman)).swap(way_ranges); - - if (!way_range_vec_31.empty()) - intersect_ranges(way_ranges, way_range_vec_31).swap(way_ranges); - } - if ((type & QUERY_RELATION) && relation_answer_state < data_collected) - { - for (std::vector< Query_Constraint* >::iterator it = constraints.begin(); - it != constraints.end(); ++it) - rel_ranges.intersect((*it)->get_relation_ranges(rman)).swap(rel_ranges); - - if (!relation_range_vec_31.empty()) - intersect_ranges(rel_ranges, relation_range_vec_31).swap(rel_ranges); - } + if ((type & QUERY_NODE) && node_answer_state < data_collected && !range_vec_32.empty()) + intersect_ranges(node_ranges, range_vec_32).swap(node_ranges); + if ((type & QUERY_WAY) && way_answer_state < data_collected && !way_range_vec_31.empty()) + intersect_ranges(way_ranges, way_range_vec_31).swap(way_ranges); + if ((type & QUERY_RELATION) && relation_answer_state < data_collected && !relation_range_vec_31.empty()) + intersect_ranges(rel_ranges, relation_range_vec_31).swap(rel_ranges); set_progress(3); rman.health_check(*this); diff --git a/src/template_db/block_backend.h b/src/template_db/block_backend.h index 910ebbce0..b635120aa 100644 --- a/src/template_db/block_backend.h +++ b/src/template_db/block_backend.h @@ -518,6 +518,16 @@ struct Block_Backend Range_Iterator range_begin(const Ranges< TIndex >& arg) { return Range_Iterator(file_blocks, arg.begin(), arg.end(), block_size); } + // From the block index alone: nothing is read or decompressed. + uint64 count_blocks(const Ranges< TIndex >& arg) + { + uint64 count = 0; + auto end = file_blocks.template range_end< typename Ranges< TIndex >::Iterator >(); + for (auto it = file_blocks.range_begin(arg.begin(), arg.end()); !(it == end); ++it) + ++count; + return count; + } + const Range_Iterator& range_end() const { return *range_end_it; } template< typename Container >