From e0bbfaa2d585ce84549900c5f5b38f0a161bb44e Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 26 Nov 2026 23:50:54 +0000 Subject: [PATCH] query: collect few tag-index candidates by their exact indices The global tag index stores each element's coarse index (a multiple of 0x100), so Query_Statement::execute intersected the constraints' ranges with whole coarse cells (intersect_ranges) and read every element of each candidate's cell. In dense areas that is thousands of elements per candidate: for maxheight ways in a 1 x 1 degree box around Worcester, 2,185 distinct way blocks for 1,025 results. Each element type's id map (the skeleton file's Random_File) gives exact indices. exact_indices_pay_off compares the map blocks the candidate ids fall in (the map holds a fixed number of ids per block and decompresses one block at a time) with the skeleton blocks the coarse ranges touch (Block_Backend::count_blocks); when the map blocks are fewer than half, the ranges are further intersected with the candidates' exact indices (get_indexes_, exact_ranges). At a past date nothing changes. On a US northeast full-history extract (Geofabrik), warm cache, 4-core VM, median CPU of 3 alternating runs: [out:json][timeout:90]; (way["maxheight"]["highway"](42.0,-72.0,43.0,-71.0); way["bridge"]["maxheight"](42.0,-72.0,43.0,-71.0); way["tunnel"]["maxheight"](42.0,-72.0,43.0,-71.0); node["maxheight"]["highway"](42.0,-72.0,43.0,-71.0); ); out center; 1.24 s -> 0.88 s, way blocks read 3,389 -> 391 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01736kJWpJaGbt8ML6jz9N35 --- src/overpass_api/statements/query.cc | 50 ++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/overpass_api/statements/query.cc b/src/overpass_api/statements/query.cc index ffd35eef0..5419e95dd 100644 --- a/src/overpass_api/statements/query.cc +++ b/src/overpass_api/statements/query.cc @@ -826,6 +826,42 @@ void Query_Statement::collect_elems(Answer_State& answer_state, Set& into, Resou } +// The id map holds a fixed number of ids per block and decompresses one block at a time, so +// looking ids up costs about one block per distinct block they fall in. +template< typename Index, typename Skeleton > +bool exact_indices_pay_off(const std::vector< typename Skeleton::Id_Type >& ids, + const Ranges< Index >& coarse_ranges, Resource_Manager& rman) +{ + if (ids.empty() || coarse_ranges.is_global()) + return false; + Random_File_Index* map = rman.get_transaction()->random_index(current_skeleton_file_properties< Skeleton >()); + uint64 ids_per_block = map->get_block_size() * map->get_compression_factor() / Index::max_size_of(); + uint64 map_blocks = 0; + uint64 last_block = ~uint64(0); + for (auto it = ids.begin(); it != ids.end(); ++it) + { + uint64 block = it->val() / ids_per_block; + if (block != last_block) + ++map_blocks; + last_block = block; + } + Block_Backend< Index, Skeleton > skeletons_db + (rman.get_transaction()->data_index(current_skeleton_file_properties< Skeleton >())); + return 2*map_blocks < skeletons_db.count_blocks(coarse_ranges); +} + + +template< typename Index > +Ranges< Index > exact_ranges(const std::vector< Index >& idxs) +{ + Ranges< Index > result; + for (auto it = idxs.begin(); it != idxs.end(); ++it) + result.push_back(*it, inc(*it)); + result.sort(); + return result; +} + + template< typename Index > Ranges< Index > intersect_ranges(const Ranges< Index >& range_a, std::vector< Index >& range_vec) { @@ -1077,12 +1113,26 @@ void Query_Statement::execute(Resource_Manager& rman) } } + // The global tag index gives only each candidate's coarse cell. Their exact indices from the + // id map narrow the blocks to read further, when the map blocks read are fewer than those saved. if ((type & QUERY_NODE) && node_answer_state < data_collected && !range_vec_32.empty()) + { intersect_ranges(node_ranges, range_vec_32).swap(node_ranges); + if (timestamp == NOW && !node_ids.invert && exact_indices_pay_off< Uint32_Index, Node_Skeleton >(node_ids.ids, node_ranges, rman)) + node_ranges.intersect(exact_ranges(get_indexes_< Uint32_Index, Node_Skeleton >(node_ids.ids, rman))).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 (timestamp == NOW && !way_ids.invert && exact_indices_pay_off< Uint31_Index, Way_Skeleton >(way_ids.ids, way_ranges, rman)) + way_ranges.intersect(exact_ranges(get_indexes_< Uint31_Index, Way_Skeleton >(way_ids.ids, rman))).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); + if (timestamp == NOW && !relation_ids.invert && exact_indices_pay_off< Uint31_Index, Relation_Skeleton >(relation_ids.ids, rel_ranges, rman)) + rel_ranges.intersect(exact_ranges(get_indexes_< Uint31_Index, Relation_Skeleton >(relation_ids.ids, rman))).swap(rel_ranges); + } set_progress(3); rman.health_check(*this);