From 5da0dfe2f3a7d1ca18cbe663c6caa5bbf4730cd1 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 26 Nov 2026 15:10:45 +0000 Subject: [PATCH] Search for member and node refs with a branchless binary search Looking up the node refs of a way or the members of a relation in a sorted vector visits ids in no particular order, so about half of std::binary_search's branches mispredict. These lookups now use a monobound search: it always runs log2(n) steps, and its only data-dependent step is a conditional add, compiled to a conditional move. Changed: binary_search_for_id and binary_search_for_pair_id (every caller looks up refs: way geometry, relation geometry, relation members in bbox and around filters and in member collection) and the has_a_child_with_id variants (way(bn), rel(bn), rel(bw), rel(br) and their role forms). Lookups that come in ascending id order, such as the elements of one tile or the tag index entries of one tag, keep std::binary_search, whose branches predict well there. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01736kJTiuJPoCWZs7RtFYjm --- src/Makefile.am | 1 + src/overpass_api/core/datatypes.h | 40 +++++---------- src/overpass_api/core/monobound_search.h | 57 +++++++++++++++++++++ src/overpass_api/data/abstract_processing.h | 11 ++-- 4 files changed, 76 insertions(+), 33 deletions(-) create mode 100644 src/overpass_api/core/monobound_search.h diff --git a/src/Makefile.am b/src/Makefile.am index b074e52fe..9a826b41a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -224,6 +224,7 @@ nobase_dist_HEADERS = \ overpass_api/core/four_field_index.h\ overpass_api/core/geometry.h\ overpass_api/core/index_computations.h\ + overpass_api/core/monobound_search.h\ overpass_api/core/parsed_query.h\ overpass_api/core/settings.h\ overpass_api/core/type_area.h\ diff --git a/src/overpass_api/core/datatypes.h b/src/overpass_api/core/datatypes.h index 00bdcd82e..5d13b8c81 100644 --- a/src/overpass_api/core/datatypes.h +++ b/src/overpass_api/core/datatypes.h @@ -29,6 +29,7 @@ #include #include "basic_types.h" +#include "monobound_search.h" #include "geometry.h" #include "type_node.h" #include "type_way.h" @@ -102,23 +103,14 @@ struct Pair_Equal_Id { }; +// The callers look up node refs of ways and members of relations, in no particular order. template < class T > const T* binary_search_for_id(const std::vector< T >& vect, typename T::Id_Type id) { - uint32 lower(0); - uint32 upper(vect.size()); - - while (upper > lower) - { - uint32 pos((upper + lower)/2); - if (id < vect[pos].id) - upper = pos; - else if (vect[pos].id == id) - return &(vect[pos]); - else - lower = pos + 1; - } - return 0; + if (vect.empty()) + return 0; + const T& found = vect[monobound_index(vect, id, [](const T& t) -> const typename T::Id_Type& { return t.id; })]; + return found.id == id ? &found : 0; } @@ -547,24 +539,16 @@ struct OSM_Element_Metadata_Skeleton }; +// The callers look up members of relations, in no particular order. template< class TIndex, class TObject > const std::pair< TIndex, const TObject* >* binary_search_for_pair_id (const std::vector< std::pair< TIndex, const TObject* > >& vect, typename TObject::Id_Type id) { - uint32 lower(0); - uint32 upper(vect.size()); - - while (upper > lower) - { - uint32 pos((upper + lower)/2); - if (id < vect[pos].second->id) - upper = pos; - else if (vect[pos].second->id == id) - return &(vect[pos]); - else - lower = pos + 1; - } - return 0; + if (vect.empty()) + return 0; + const std::pair< TIndex, const TObject* >& found = vect[monobound_index(vect, id, + [](const std::pair< TIndex, const TObject* >& p) -> const typename TObject::Id_Type& { return p.second->id; })]; + return found.second->id == id ? &found : 0; } diff --git a/src/overpass_api/core/monobound_search.h b/src/overpass_api/core/monobound_search.h new file mode 100644 index 000000000..c74963540 --- /dev/null +++ b/src/overpass_api/core/monobound_search.h @@ -0,0 +1,57 @@ +/** Copyright Olbricht, Roland + * + * This file is part of Overpass_API. + * + * Overpass_API is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * Overpass_API is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Overpass_API. If not, see . + */ + +#ifndef DE__OSM3S___OVERPASS_API__CORE__MONOBOUND_SEARCH_H +#define DE__OSM3S___OVERPASS_API__CORE__MONOBOUND_SEARCH_H + +#include +#include + + +// Index of the last element of vect whose key(element) isn't greater than id, or 0. vect must be +// sorted by key. The loop always runs log2(size) times and its only data-dependent step is a +// conditional add, which compilers turn into a conditional move: for lookups in no particular +// order, such as the node refs of a way or the members of a relation, there is no branch to +// mispredict. For ascending lookups std::lower_bound's branches predict well; use that there. +template< typename T, typename Key, typename Get_Key > +std::size_t monobound_index(const std::vector< T >& vect, const Key& id, Get_Key key) +{ + std::size_t base = 0; + std::size_t n = vect.size(); + while (n > 1) + { + std::size_t half = n/2; + base += !(id < key(vect[base + half])) ? half : 0; + n -= half; + } + return base; +} + + +// Whether the sorted vector ids contains id, like std::binary_search. +template< typename Id_Type > +bool monobound_contains(const std::vector< Id_Type >& ids, const Id_Type& id) +{ + if (ids.empty()) + return false; + const Id_Type& found = ids[monobound_index(ids, id, [](const Id_Type& i) -> const Id_Type& { return i; })]; + return !(found < id) && !(id < found); +} + + +#endif diff --git a/src/overpass_api/data/abstract_processing.h b/src/overpass_api/data/abstract_processing.h index 143173665..882e09d34 100644 --- a/src/overpass_api/data/abstract_processing.h +++ b/src/overpass_api/data/abstract_processing.h @@ -20,6 +20,7 @@ #define DE__OSM3S___OVERPASS_API__DATA__ABSTRACT_PROCESSING_H #include "../core/datatypes.h" +#include "../core/monobound_search.h" #include "../statements/statement.h" #include "filenames.h" @@ -107,7 +108,7 @@ inline bool has_a_child_with_id it3 != relation.members.end(); ++it3) { if (it3->type == type && - std::binary_search(ids.begin(), ids.end(), it3->ref)) + monobound_contains(ids, it3->ref)) return true; } return false; @@ -121,7 +122,7 @@ inline bool has_a_child_with_id_and_role it3 != relation.members.end(); ++it3) { if (it3->type == type && it3->role == role_id && - std::binary_search(ids.begin(), ids.end(), it3->ref)) + monobound_contains(ids, it3->ref)) return true; } return false; @@ -137,13 +138,13 @@ inline bool has_a_child_with_id for (; it3 != pos->end() && *it3 < 0; ++it3) { if (*it3 + (int)way.nds.size() >= 0 && - std::binary_search(ids.begin(), ids.end(), way.nds[*it3 + way.nds.size()])) + monobound_contains(ids, way.nds[*it3 + way.nds.size()])) return true; } for (; it3 != pos->end(); ++it3) { if (*it3 > 0 && *it3 < (int)way.nds.size()+1 && - std::binary_search(ids.begin(), ids.end(), way.nds[*it3-1])) + monobound_contains(ids, way.nds[*it3-1])) return true; } } @@ -152,7 +153,7 @@ inline bool has_a_child_with_id for (std::vector< Node::Id_Type >::const_iterator it3(way.nds.begin()); it3 != way.nds.end(); ++it3) { - if (std::binary_search(ids.begin(), ids.end(), *it3)) + if (monobound_contains(ids, *it3)) return true; } }