From 003c43d2dea339e0684c6ff89aabacf0569350f2 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 22 Nov 2026 14:46:36 +0000 Subject: [PATCH 1/2] print: reference id-sorted elements in 8 bytes To print in id order, every out mode except qt puts one reference per element in a vector and sorts it. A reference was a pointer and the element's index, 16 bytes, or 24 when the set has attic elements, which also carried the element's timestamp. For an out of 11.5M nodes at a past date that was 277 MB. Reference each element instead by the number of its index's entry in a table of the set's non-empty vectors, current ones first, and its position in that vector: two 32-bit numbers. The index, the object and the timestamp of an attic element are read from the table and the set. The references are filled in the same order as before and sorted with the same std::sort by id, so elements with equal ids stay in the same order. Output is unchanged. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01736kJTiuJPoCWZs7RtFYjm --- src/overpass_api/statements/print.cc | 180 ++++++++++++++------------- 1 file changed, 94 insertions(+), 86 deletions(-) diff --git a/src/overpass_api/statements/print.cc b/src/overpass_api/statements/print.cc index 1033b3426..392652b65 100644 --- a/src/overpass_api/statements/print.cc +++ b/src/overpass_api/statements/print.cc @@ -413,31 +413,6 @@ void tags_quadtile_attic_ } -template< class TComp > -struct Skeleton_Comparator_By_Id { - bool operator() (const std::pair< const TComp*, uint32 >& a, - const std::pair< const TComp*, uint32 >& b) - { - return (a.first->id < b.first->id); - } -}; - - -template< typename Index, typename Object > -struct Maybe_Attic_Ref -{ -public: - Maybe_Attic_Ref(Index idx_, const Object* obj_, uint64 timestamp_) - : idx(idx_), obj(obj_), timestamp(timestamp_) {} - - Index idx; - const Object* obj; - uint64 timestamp; - - bool operator<(const Maybe_Attic_Ref& rhs) const { return obj->id < rhs.obj->id; } -}; - - // Number of elements in a set, so that the id-sorted references below can be reserved exactly: // grown by doubling, they reach up to about twice their size, plus the old copy while they grow. template< class Index, class Object > @@ -451,52 +426,85 @@ std::size_t count_items(const std::map< Index, std::vector< Object > >& items) } +// The elements of a set in id order, current elements and then attic elements for equal ids as +// std::sort leaves them. Each element is referenced in 8 bytes: the number of its index's entry in a +// table of the set's non-empty vectors, current ones first, and its position in that vector. A +// pointer and an index took 16 bytes per element, and 24 with the timestamp of an attic element. template< class Index, class Object > -std::vector< std::pair< const Object*, uint32 > > collect_items_by_id( - const std::map< Index, std::vector< Object > >& items) +class Items_By_Id { - std::vector< std::pair< const Object*, uint32 > > items_by_id; - items_by_id.reserve(count_items(items)); +public: + Items_By_Id(const std::map< Index, std::vector< Object > >& items) + { + refs.reserve(count_items(items)); + add_entries(current, items); + sort_refs(); + } - for (typename std::map< Index, std::vector< Object > >::const_iterator - it(items.begin()); it != items.end(); ++it) + Items_By_Id(const std::map< Index, std::vector< Object > >& items, + const std::map< Index, std::vector< Attic< Object > > >& attic_items) { - for (typename std::vector< Object >::const_iterator it2(it->second.begin()); - it2 != it->second.end(); ++it2) - items_by_id.push_back(std::make_pair(&(*it2), it->first.val())); + refs.reserve(count_items(items) + count_items(attic_items)); + add_entries(current, items); + add_entries(attic, attic_items); + sort_refs(); } - sort(items_by_id.begin(), items_by_id.end(), - Skeleton_Comparator_By_Id< Object >()); - return items_by_id; -} + std::size_t size() const { return refs.size(); } + const Object& obj(std::size_t i) const { return get(refs[i]); } + Index idx(std::size_t i) const + { + return refs[i].entry < current.size() ? current[refs[i].entry].first + : attic[refs[i].entry - current.size()].first; + } + // NOW for a current element. + uint64 timestamp(std::size_t i) const + { + return refs[i].entry < current.size() ? NOW + : attic[refs[i].entry - current.size()].second[refs[i].pos].timestamp; + } +private: + struct Ref + { + Ref(uint32 entry_, uint32 pos_) : entry(entry_), pos(pos_) {} + uint32 entry; + uint32 pos; + }; -template< class Index, class Object > -std::vector< Maybe_Attic_Ref< Index, Object > > collect_items_by_id( - const std::map< Index, std::vector< Object > >& items, - const std::map< Index, std::vector< Attic< Object > > >& attic_items) -{ - std::vector< Maybe_Attic_Ref< Index, Object > > items_by_id; - items_by_id.reserve(count_items(items) + count_items(attic_items)); - for (typename std::map< Index, std::vector< Object > >::const_iterator - it(items.begin()); it != items.end(); ++it) + std::vector< std::pair< Index, const Object* > > current; + std::vector< std::pair< Index, const Attic< Object >* > > attic; + std::vector< Ref > refs; + + const Object& get(const Ref& ref) const { - for (typename std::vector< Object >::const_iterator it2(it->second.begin()); - it2 != it->second.end(); ++it2) - items_by_id.push_back(Maybe_Attic_Ref< Index, Object >(it->first, &(*it2), NOW)); + return ref.entry < current.size() ? current[ref.entry].second[ref.pos] + : attic[ref.entry - current.size()].second[ref.pos]; } - for (typename std::map< Index, std::vector< Attic< Object > > >::const_iterator - it(attic_items.begin()); it != attic_items.end(); ++it) + + // Appends the references in the order of the set, as before, so that equal ids keep their order. + template< class Elem > + void add_entries(std::vector< std::pair< Index, const Elem* > >& entries, + const std::map< Index, std::vector< Elem > >& items) { - for (typename std::vector< Attic< Object > >::const_iterator it2(it->second.begin()); - it2 != it->second.end(); ++it2) - items_by_id.push_back(Maybe_Attic_Ref< Index, Object >(it->first, &(*it2), it2->timestamp)); + for (typename std::map< Index, std::vector< Elem > >::const_iterator + it(items.begin()); it != items.end(); ++it) + { + if (it->second.empty()) + continue; + uint32 entry = current.size() + attic.size(); + entries.push_back(std::make_pair(it->first, &it->second.front())); + for (uint32 pos = 0; pos < it->second.size(); ++pos) + refs.push_back(Ref(entry, pos)); + } } - sort(items_by_id.begin(), items_by_id.end()); - return items_by_id; -} + void sort_refs() + { + std::sort(refs.begin(), refs.end(), + [this](const Ref& a, const Ref& b) { return get(a).id < get(b).id; }); + } +}; template< class Index, class Object > @@ -504,14 +512,14 @@ void by_id (const std::map< Index, std::vector< Object > >& items, Output_Handler& output, Transaction& transaction, Extra_Data& extra_data, uint32 limit, uint32& element_count) { - std::vector< std::pair< const Object*, uint32 > > items_by_id = collect_items_by_id(items); + Items_By_Id< Index, Object > items_by_id(items); // iterate over the result for (uint32 i(0); i < items_by_id.size(); ++i) { if (++element_count > limit) return; - print_item(extra_data, output, items_by_id[i].second, *(items_by_id[i].first)); + print_item(extra_data, output, items_by_id.idx(i).val(), items_by_id.obj(i)); } } @@ -523,18 +531,18 @@ void by_id Output_Handler& output, Transaction& transaction, Extra_Data& extra_data, uint32 limit, uint32& element_count) { - std::vector< Maybe_Attic_Ref< Index, Object > > items_by_id = collect_items_by_id(items, attic_items); + Items_By_Id< Index, Object > items_by_id(items, attic_items); // iterate over the result for (uint32 i(0); i < items_by_id.size(); ++i) { if (++element_count > limit) return; - if (items_by_id[i].timestamp == NOW) - print_item(extra_data, output, items_by_id[i].idx.val(), *items_by_id[i].obj); + if (items_by_id.timestamp(i) == NOW) + print_item(extra_data, output, items_by_id.idx(i).val(), items_by_id.obj(i)); else - print_item(extra_data, output, items_by_id[i].idx.val(), - Attic< Object >(*items_by_id[i].obj, items_by_id[i].timestamp)); + print_item(extra_data, output, items_by_id.idx(i).val(), + Attic< Object >(items_by_id.obj(i), items_by_id.timestamp(i))); } } @@ -612,7 +620,7 @@ void tags_by_id Resource_Manager& rman, Meta_Collector< Index, typename Object::Id_Type >* meta_printer, Tag_Store< Index, Object >& tag_store, uint32 limit, uint32& element_count) { - std::vector< std::pair< const Object*, uint32 > > items_by_id = collect_items_by_id(items); + Items_By_Id< Index, Object > items_by_id(items); // iterate over the result for (typename Object::Id_Type id_pos; id_pos < items_by_id.size(); id_pos += FLUSH_SIZE) @@ -620,13 +628,13 @@ void tags_by_id // Disable health_check: This ensures that a result will be always printed completely //rman.health_check(*this); - typename Object::Id_Type lower_id_bound(items_by_id[id_pos.val()].first->id); + typename Object::Id_Type lower_id_bound(items_by_id.obj(id_pos.val()).id); typename Object::Id_Type upper_id_bound; if (id_pos + FLUSH_SIZE < items_by_id.size()) - upper_id_bound = items_by_id[(id_pos + FLUSH_SIZE).val()].first->id; + upper_id_bound = items_by_id.obj((id_pos + FLUSH_SIZE).val()).id; else { - upper_id_bound = items_by_id[items_by_id.size()-1].first->id; + upper_id_bound = items_by_id.obj(items_by_id.size()-1).id; ++upper_id_bound; } @@ -648,10 +656,10 @@ void tags_by_id return; typename std::set< OSM_Element_Metadata_Skeleton< typename Object::Id_Type > >::const_iterator meta_it = metadata.lower_bound(OSM_Element_Metadata_Skeleton< typename Object::Id_Type > - (items_by_id[i.val()].first->id)); - print_item(extra_data, output, items_by_id[i.val()].second, *(items_by_id[i.val()].first), - tag_store.get(Index(items_by_id[i.val()].second), *items_by_id[i.val()].first), - (meta_it != metadata.end() && meta_it->ref == items_by_id[i.val()].first->id) ? + (items_by_id.obj(i.val()).id)); + print_item(extra_data, output, items_by_id.idx(i.val()).val(), items_by_id.obj(i.val()), + tag_store.get(items_by_id.idx(i.val()), items_by_id.obj(i.val())), + (meta_it != metadata.end() && meta_it->ref == items_by_id.obj(i.val()).id) ? &*meta_it : 0); } } @@ -666,7 +674,7 @@ void tags_by_id_attic Extra_Data& extra_data, uint32 FLUSH_SIZE, Output_Handler& output, Resource_Manager& rman, Transaction& transaction, uint32 limit, uint32& element_count) { - std::vector< Maybe_Attic_Ref< Index, Object > > items_by_id = collect_items_by_id(current_items, attic_items); + Items_By_Id< Index, Object > items_by_id(current_items, attic_items); Tag_Store< Index, Object > current_tag_store(transaction); Tag_Store< Index, Object > attic_tag_store(transaction); @@ -681,13 +689,13 @@ void tags_by_id_attic // Disable health_check: This ensures that a result will be always printed completely //rman.health_check(*this); - typename Object::Id_Type lower_id_bound(items_by_id[id_pos.val()].obj->id); + typename Object::Id_Type lower_id_bound(items_by_id.obj(id_pos.val()).id); typename Object::Id_Type upper_id_bound; if (id_pos + FLUSH_SIZE < items_by_id.size()) - upper_id_bound = items_by_id[(id_pos + FLUSH_SIZE).val()].obj->id; + upper_id_bound = items_by_id.obj((id_pos + FLUSH_SIZE).val()).id; else { - upper_id_bound = items_by_id[items_by_id.size()-1].obj->id; + upper_id_bound = items_by_id.obj(items_by_id.size()-1).id; ++upper_id_bound; } @@ -710,24 +718,24 @@ void tags_by_id_attic { if (++element_count > limit) return; - if (items_by_id[i.val()].timestamp == NOW) + if (items_by_id.timestamp(i.val()) == NOW) { typename std::set< OSM_Element_Metadata_Skeleton< typename Object::Id_Type > >::const_iterator meta_it = only_current_metadata.lower_bound(OSM_Element_Metadata_Skeleton< typename Object::Id_Type > - (items_by_id[i.val()].obj->id)); - print_item(extra_data, output, items_by_id[i.val()].idx.val(), *items_by_id[i.val()].obj, - current_tag_store.get(items_by_id[i.val()].idx, *items_by_id[i.val()].obj), - (meta_it != only_current_metadata.end() && meta_it->ref == items_by_id[i.val()].obj->id) ? + (items_by_id.obj(i.val()).id)); + print_item(extra_data, output, items_by_id.idx(i.val()).val(), items_by_id.obj(i.val()), + current_tag_store.get(items_by_id.idx(i.val()), items_by_id.obj(i.val())), + (meta_it != only_current_metadata.end() && meta_it->ref == items_by_id.obj(i.val()).id) ? &*meta_it : 0); } else { typename std::set< OSM_Element_Metadata_Skeleton< typename Object::Id_Type > >::const_iterator meta_it = find_matching_metadata(attic_metadata, - items_by_id[i.val()].obj->id, items_by_id[i.val()].timestamp); - print_item(extra_data, output, items_by_id[i.val()].idx.val(), - Attic< Object >(*items_by_id[i.val()].obj, items_by_id[i.val()].timestamp), - attic_tag_store.get(items_by_id[i.val()].idx, *items_by_id[i.val()].obj), + items_by_id.obj(i.val()).id, items_by_id.timestamp(i.val())); + print_item(extra_data, output, items_by_id.idx(i.val()).val(), + Attic< Object >(items_by_id.obj(i.val()), items_by_id.timestamp(i.val())), + attic_tag_store.get(items_by_id.idx(i.val()), items_by_id.obj(i.val())), meta_it != attic_metadata.end() ? &*meta_it : 0); } } From 9bc2992ec2c882f8a085de3aedd5b5380ebd7991 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 22 Nov 2026 17:15:25 +0000 Subject: [PATCH 2/2] print: look up current and attic references the same way The 8-byte references kept current and attic vectors in two tables, so every comparison in the sort branched on the kind of element. Keep one table instead, with each vector's first element (its Object part) and the size of its elements, and find an element by its position times that size. On a whole-province out ids of 12.6M current nodes this takes the sort's extra cost from about 10% of the query's CPU to about 1%, and to about 7% for 11.5M nodes at a past date. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01736kJTiuJPoCWZs7RtFYjm --- src/overpass_api/statements/print.cc | 45 +++++++++++++++++----------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/src/overpass_api/statements/print.cc b/src/overpass_api/statements/print.cc index 392652b65..00f0ab5d7 100644 --- a/src/overpass_api/statements/print.cc +++ b/src/overpass_api/statements/print.cc @@ -437,7 +437,8 @@ class Items_By_Id Items_By_Id(const std::map< Index, std::vector< Object > >& items) { refs.reserve(count_items(items)); - add_entries(current, items); + add_entries(items); + current_count = entries.size(); sort_refs(); } @@ -445,23 +446,20 @@ class Items_By_Id const std::map< Index, std::vector< Attic< Object > > >& attic_items) { refs.reserve(count_items(items) + count_items(attic_items)); - add_entries(current, items); - add_entries(attic, attic_items); + add_entries(items); + current_count = entries.size(); + add_entries(attic_items); sort_refs(); } std::size_t size() const { return refs.size(); } const Object& obj(std::size_t i) const { return get(refs[i]); } - Index idx(std::size_t i) const - { - return refs[i].entry < current.size() ? current[refs[i].entry].first - : attic[refs[i].entry - current.size()].first; - } + Index idx(std::size_t i) const { return entries[refs[i].entry].idx; } // NOW for a current element. uint64 timestamp(std::size_t i) const { - return refs[i].entry < current.size() ? NOW - : attic[refs[i].entry - current.size()].second[refs[i].pos].timestamp; + return refs[i].entry < current_count ? NOW + : static_cast< const Attic< Object >& >(get(refs[i])).timestamp; } private: @@ -472,28 +470,39 @@ class Items_By_Id uint32 pos; }; - std::vector< std::pair< Index, const Object* > > current; - std::vector< std::pair< Index, const Attic< Object >* > > attic; + // The address of the Object part of the vector's first element, and the size of its elements: + // that of Object for current elements, of Attic< Object > for attic elements. Looking up both + // kinds the same way keeps the sort's comparisons free of branches. + struct Entry + { + Entry(const char* first_, uint32 size_, Index idx_) : first(first_), size(size_), idx(idx_) {} + const char* first; + uint32 size; + Index idx; + }; + + std::vector< Entry > entries; + std::size_t current_count; std::vector< Ref > refs; const Object& get(const Ref& ref) const { - return ref.entry < current.size() ? current[ref.entry].second[ref.pos] - : attic[ref.entry - current.size()].second[ref.pos]; + const Entry& entry = entries[ref.entry]; + return *reinterpret_cast< const Object* >(entry.first + std::size_t(ref.pos) * entry.size); } // Appends the references in the order of the set, as before, so that equal ids keep their order. template< class Elem > - void add_entries(std::vector< std::pair< Index, const Elem* > >& entries, - const std::map< Index, std::vector< Elem > >& items) + void add_entries(const std::map< Index, std::vector< Elem > >& items) { for (typename std::map< Index, std::vector< Elem > >::const_iterator it(items.begin()); it != items.end(); ++it) { if (it->second.empty()) continue; - uint32 entry = current.size() + attic.size(); - entries.push_back(std::make_pair(it->first, &it->second.front())); + uint32 entry = entries.size(); + const Object& first = it->second.front(); + entries.push_back(Entry(reinterpret_cast< const char* >(&first), sizeof(Elem), it->first)); for (uint32 pos = 0; pos < it->second.size(); ++pos) refs.push_back(Ref(entry, pos)); }