From 70f18bd9c3a92331da59b366ec66cacc5ba941e1 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 20 Nov 2026 20:46:30 +0000 Subject: [PATCH] Reject non-matching ids with a bit filter before binary search Id_Predicate checks every object it is shown with std::binary_search over the sorted id list. Most objects it sees are not in the list: a tag query such as node[amenity=restaurant] finds the matching ids in the tag index, then reads every node block of each quadtile that holds one of them and tests each node against the ids. Add a small hashed bit filter (about 16 bits per id, at most 16 MiB) that rejects most absent ids with one bit test; the binary search still decides every id the filter lets through, so results do not change. Collect_Items now builds its predicate once instead of once per chunk. On a Nova Scotia extract (12.6M nodes), median CPU time per query with warm caches: nwr[amenity=restaurant] 160 ms -> 124 ms node[natural=peak] 183 ms -> 129 ms rel boundary with (._;>>;) 682 ms -> 571 ms Outputs are byte-identical. No change to the database format or the API. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01736kJUqyDw9zM3Bkh2W2QT --- src/overpass_api/data/abstract_processing.h | 53 +++++++++++++++++++-- src/overpass_api/data/collect_members.h | 8 ++-- 2 files changed, 54 insertions(+), 7 deletions(-) diff --git a/src/overpass_api/data/abstract_processing.h b/src/overpass_api/data/abstract_processing.h index 143173665..396b83bcc 100644 --- a/src/overpass_api/data/abstract_processing.h +++ b/src/overpass_api/data/abstract_processing.h @@ -22,6 +22,9 @@ #include "../core/datatypes.h" #include "../statements/statement.h" #include "filenames.h" +#include +#include +#include //----------------------------------------------------------------------------- @@ -83,19 +86,61 @@ class Trivial_Predicate //----------------------------------------------------------------------------- +/* A one-bit-per-hash-slot filter over a set of ids. It answers "definitely + not in the set" with a single bit test, and "maybe in the set" otherwise. + It uses about 16 bits per id (at most 16 MiB), so a false "maybe" happens + for roughly 6% of absent ids while the cap is not reached. */ +class Id_Bit_Filter +{ +public: + template< typename Id_Type > + explicit Id_Bit_Filter(const std::vector< Id_Type >& ids) + { + unsigned log_bits = 6; + while (log_bits < 27 && (uint64(1) << log_bits) < uint64(ids.size())*16) + ++log_bits; + shift = 64 - log_bits; + bits.assign((uint64(1) << log_bits)/64, 0); + for (typename std::vector< Id_Type >::const_iterator it = ids.begin(); it != ids.end(); ++it) + { + uint64 h = slot(it->val()); + bits[h>>6] |= uint64(1) << (h & 63); + } + } + + bool maybe_contains(uint64 id) const + { + uint64 h = slot(id); + return (bits[h>>6] >> (h & 63)) & 1; + } + +private: + uint64 slot(uint64 id) const { return (id * 0x9E3779B97F4A7C15ull) >> shift; } + + std::vector< uint64 > bits; + unsigned shift; +}; + + template < class Object > class Id_Predicate { public: Id_Predicate(const std::vector< typename Object::Id_Type >& ids_) - : ids(ids_) {} - bool match(const Object& obj) const { return std::binary_search(ids.begin(), ids.end(), obj.id); } - bool match(const Handle< Object >& h) const { return std::binary_search(ids.begin(), ids.end(), h.id()); } - bool match(const Handle< Attic< Object > >& h) const { return std::binary_search(ids.begin(), ids.end(), h.id()); } + : ids(ids_), filter(std::make_shared< const Id_Bit_Filter >(ids_)) {} + bool match(const Object& obj) const { return contains(obj.id); } + bool match(const Handle< Object >& h) const { return contains(h.id()); } + bool match(const Handle< Attic< Object > >& h) const { return contains(h.id()); } bool possible() const { return !ids.empty(); } private: + // Most candidates are not in ids, so the filter rejects them before the + // binary search; the binary search keeps the answer exact. + bool contains(const typename Object::Id_Type& id) const + { return filter->maybe_contains(id.val()) && std::binary_search(ids.begin(), ids.end(), id); } + const std::vector< typename Object::Id_Type >& ids; + std::shared_ptr< const Id_Bit_Filter > filter; }; //----------------------------------------------------------------------------- diff --git a/src/overpass_api/data/collect_members.h b/src/overpass_api/data/collect_members.h index 9abd3b8c0..ab3c5aa52 100644 --- a/src/overpass_api/data/collect_members.h +++ b/src/overpass_api/data/collect_members.h @@ -176,7 +176,7 @@ class Collect_Items const Ranges< Index >& ranges_, const Statement& query_, Resource_Manager& rman_) : ids(&ids_), invert_ids(invert_ids_), ranges(ranges_), query(&query_), rman(&rman_), - min_idx(ranges_.empty() ? Index() : ranges_.begin().lower_bound()) {} + min_idx(ranges_.empty() ? Index() : ranges_.begin().lower_bound()), id_pred(ids_) {} bool get_chunk( std::map< Index, std::vector< Object > >& elements, @@ -189,6 +189,8 @@ class Collect_Items const Statement* query; Resource_Manager* rman; Index min_idx; + // Built once and reused for every chunk. + Id_Predicate< Object > id_pred; }; @@ -221,11 +223,11 @@ bool Collect_Items< Index, Object >::get_chunk( elements, attic_elements, Trivial_Predicate< Object >(), ranges, min_idx, *query, *rman); else return get_elements_by_id_from_db_generic( - elements, attic_elements, Not_Predicate< Object, Id_Predicate< Object > >(Id_Predicate< Object >(*ids)), + elements, attic_elements, Not_Predicate< Object, Id_Predicate< Object > >(id_pred), ranges, min_idx, *query, *rman); } return get_elements_by_id_from_db_generic( - elements, attic_elements, Id_Predicate< Object >(*ids), ranges, min_idx, *query, *rman); + elements, attic_elements, id_pred, ranges, min_idx, *query, *rman); }