From 6f45662d623257f6ed3b323da90149c427c14f11 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 22 Nov 2026 14:16:06 +0000 Subject: [PATCH] Tag_Store: keep only a chunk's ids when printing in chunks Chunked output (tags_by_id, tags_by_id_attic) prints 1M nodes or 512k ways or relations at a time, and prefetch_chunk loads only that chunk's tags. It still kept the ids of the whole set, per coarse index, and filtered them by the chunk's id bounds while collecting. On a whole- province `out` of 12.6M nodes these lists were 145 MB of the peak. Generate them per chunk from the elements within the bounds instead. They then hold at most one chunk's ids, and the tag range query covers only the coarse indices that have elements in the chunk. Output is unchanged. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01736kJTiuJPoCWZs7RtFYjm --- src/overpass_api/core/type_tags.h | 55 +++++++++++++++++++++++++++++++ src/overpass_api/data/tag_store.h | 14 ++++---- 2 files changed, 61 insertions(+), 8 deletions(-) diff --git a/src/overpass_api/core/type_tags.h b/src/overpass_api/core/type_tags.h index bc3cdf0f6..e3e03be2e 100644 --- a/src/overpass_api/core/type_tags.h +++ b/src/overpass_api/core/type_tags.h @@ -325,6 +325,61 @@ void generate_ids_by_coarse } +// As generate_ids_by_coarse, but only for the elements with ids in [lower_id_bound, upper_id_bound), +// so that a chunk of output needs the ids of that chunk only. +template< class TIndex, class TObject > +void generate_ids_by_coarse + (std::map< uint32, std::vector< typename TObject::Id_Type > >& ids_by_coarse, + const std::map< TIndex, std::vector< TObject > >& items, + typename TObject::Id_Type lower_id_bound, typename TObject::Id_Type upper_id_bound) +{ + for (typename std::map< TIndex, std::vector< TObject > >::const_iterator + it(items.begin()); it != items.end(); ++it) + { + for (typename std::vector< TObject >::const_iterator it2(it->second.begin()); + it2 != it->second.end(); ++it2) + { + if (!(it2->id < lower_id_bound) && it2->id < upper_id_bound) + ids_by_coarse[it->first.val() & 0x7fffff00].push_back(it2->id); + } + } + + for (typename std::map< uint32, std::vector< typename TObject::Id_Type > >::iterator + it = ids_by_coarse.begin(); it != ids_by_coarse.end(); ++it) + { + std::sort(it->second.begin(), it->second.end()); + it->second.erase(std::unique(it->second.begin(), it->second.end()), it->second.end()); + } +} + + +template< class TIndex, class TObject > +void generate_ids_by_coarse + (std::map< uint32, std::vector< Attic< typename TObject::Id_Type > > >& ids_by_coarse, + const std::map< TIndex, std::vector< TObject > >& items, + typename TObject::Id_Type lower_id_bound, typename TObject::Id_Type upper_id_bound) +{ + for (typename std::map< TIndex, std::vector< TObject > >::const_iterator + it(items.begin()); it != items.end(); ++it) + { + for (typename std::vector< TObject >::const_iterator it2(it->second.begin()); + it2 != it->second.end(); ++it2) + { + if (!(it2->id < lower_id_bound) && it2->id < upper_id_bound) + ids_by_coarse[it->first.val() & 0x7fffff00].push_back + (Attic< typename TObject::Id_Type >(it2->id, it2->timestamp)); + } + } + + for (typename std::map< uint32, std::vector< Attic< typename TObject::Id_Type > > >::iterator + it = ids_by_coarse.begin(); it != ids_by_coarse.end(); ++it) + { + std::sort(it->second.begin(), it->second.end()); + it->second.erase(std::unique(it->second.begin(), it->second.end()), it->second.end()); + } +} + + struct Tag_Index_Global_Until756 { std::string key; diff --git a/src/overpass_api/data/tag_store.h b/src/overpass_api/data/tag_store.h index 296229cb9..2110c1a48 100644 --- a/src/overpass_api/data/tag_store.h +++ b/src/overpass_api/data/tag_store.h @@ -303,11 +303,9 @@ void Tag_Store< Index, Object >::prefetch_chunk(const std::map< Index, std::vect { tags_by_id.clear(); - // The coarse indices and ids of the whole set, needed for every chunk. elems is the same set on - // every call, so they are generated once: generating them again appended every id a second - // time before deduplicating, which cost time and twice their memory on each chunk. - if (ids_by_coarse.empty()) - generate_ids_by_coarse(ids_by_coarse, elems); + // The coarse indices and ids of this chunk's elements only: no other tags are collected. + ids_by_coarse.clear(); + generate_ids_by_coarse(ids_by_coarse, elems, lower_id_bound, upper_id_bound); Block_Backend< Tag_Index_Local, typename Object::Id_Type > items_db (transaction->data_index(current_local_tags_file_properties< Object >())); @@ -356,10 +354,10 @@ template< typename Index, typename Object > void Tag_Store< Index, Object >::prefetch_chunk(const std::map< Index, std::vector< Attic< Object > > >& attic_items, typename Object::Id_Type lower_id_bound, typename Object::Id_Type upper_id_bound) { - // As for current elements: only this chunk's tags are needed, and the ids once. + // As for current elements: only this chunk's tags and ids are needed. tags_by_id.clear(); - if (attic_ids_by_coarse.empty()) - generate_ids_by_coarse(attic_ids_by_coarse, attic_items); + attic_ids_by_coarse.clear(); + generate_ids_by_coarse(attic_ids_by_coarse, attic_items, lower_id_bound, upper_id_bound); Block_Backend< Tag_Index_Local, typename Object::Id_Type > current_tags_db (transaction->data_index(current_local_tags_file_properties< Object >()));