From 1c6b7637852f0878c503b8e7ea36bf8f96238bd2 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 22 Nov 2026 05:13:17 +0000 Subject: [PATCH] Filter bbox and past-date results in place, without vector slack filter_by_bbox and filter_attic_elements built each index's filtered result in a new vector by push_back and swapped it in. The kept vector carried up to twice its size in slack from doubling, and the input vector, itself grown by push_back, briefly existed alongside it. Compact each index's vector in place instead, keeping the order, then shrink it to its size. Kept elements are moved only when their slot changes, so ways and relations no longer copy their member vectors when nothing is removed. Output is unchanged. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01736kJTiuJPoCWZs7RtFYjm --- src/overpass_api/data/bbox_filter.cc | 14 ++++++++--- src/overpass_api/data/collect_members.h | 33 ++++++++++++++++--------- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/overpass_api/data/bbox_filter.cc b/src/overpass_api/data/bbox_filter.cc index 4b75a5049..a5a83b929 100644 --- a/src/overpass_api/data/bbox_filter.cc +++ b/src/overpass_api/data/bbox_filter.cc @@ -45,8 +45,9 @@ void filter_by_bbox(const Bbox_Double& bbox, std::map< Index, std::vector< Coord for (typename std::map< Index, std::vector< Coord > >::iterator it = nodes.begin(); it != nodes.end(); ++it) { - std::vector< Coord > local_into; - for (typename std::vector< Coord >::const_iterator iit = it->second.begin(); + // Compact in place, keeping the order, and drop the slack left by growing the vector. + typename std::vector< Coord >::iterator out = it->second.begin(); + for (typename std::vector< Coord >::iterator iit = it->second.begin(); iit != it->second.end(); ++iit) { uint32 lat(::ilat(it->first.val(), iit->ll_lower)); @@ -54,9 +55,14 @@ void filter_by_bbox(const Bbox_Double& bbox, std::map< Index, std::vector< Coord if ((lat >= south) && (lat <= north) && (((lon >= west) && (lon <= east)) || ((east < west) && ((lon >= west) || (lon <= east))))) - local_into.push_back(*iit); + { + if (out != iit) + *out = *iit; + ++out; + } } - it->second.swap(local_into); + it->second.erase(out, it->second.end()); + it->second.shrink_to_fit(); } } diff --git a/src/overpass_api/data/collect_members.h b/src/overpass_api/data/collect_members.h index 9abd3b8c0..aaccffd09 100644 --- a/src/overpass_api/data/collect_members.h +++ b/src/overpass_api/data/collect_members.h @@ -355,44 +355,53 @@ void filter_attic_elements for (typename std::map< Index, std::vector< Skeleton > >::iterator it = current.begin(); it != current.end(); ++it) { - std::vector< Skeleton > result; std::map< typename Skeleton::Id_Type, std::pair< uint64, uint64 > >& entry = timestamp_by_id_by_idx[it->first]; - for (typename std::vector< Skeleton >::const_iterator it2 = it->second.begin(); + // Compact in place, keeping the order, and drop the slack left by growing the vector. + typename std::vector< Skeleton >::iterator out = it->second.begin(); + for (typename std::vector< Skeleton >::iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2) { if (entry[it2->id].first > 0) { if (entry[it2->id].second == NOW) - result.push_back(*it2); + { + if (out != it2) + *out = std::move(*it2); + ++out; + } else attic[it->first].push_back(Attic< Skeleton >(*it2, entry[it2->id].second)); } } - - result.swap(it->second); + it->second.erase(out, it->second.end()); + it->second.shrink_to_fit(); } // Filter attic: only keep elements that have already existed at timestamp for (typename std::map< Index, std::vector< Attic< Skeleton > > >::iterator it = attic.begin(); it != attic.end(); ++it) { - std::vector< Attic< Skeleton > > result; std::map< typename Skeleton::Id_Type, std::pair< uint64, uint64 > >& entry = timestamp_by_id_by_idx[it->first]; - for (typename std::vector< Attic< Skeleton > >::const_iterator it2 = it->second.begin(); + // Compact in place, as above. + typename std::vector< Attic< Skeleton > >::iterator out = it->second.begin(); + for (typename std::vector< Attic< Skeleton > >::iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2) { - if (entry[it2->id].first > 0) + const std::pair< uint64, uint64 >& timestamps = entry[it2->id]; + if (timestamps.first > 0) { - result.push_back(*it2); - result.back().timestamp = entry[it2->id].second; + if (out != it2) + *out = std::move(*it2); + out->timestamp = timestamps.second; + ++out; } } - - result.swap(it->second); + it->second.erase(out, it->second.end()); + it->second.shrink_to_fit(); } } }