From d15ddd70c693b42b902c14cc2e4e93dd5a21867a Mon Sep 17 00:00:00 2001 From: Yuri Gorshenin Date: Mon, 6 Jun 2016 13:42:35 +0300 Subject: [PATCH] [search] Reduced geometry cache size. --- search/nested_rects_cache.cpp | 45 +++++++++++++++++++++++------------ search/nested_rects_cache.hpp | 11 ++++++--- 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/search/nested_rects_cache.cpp b/search/nested_rects_cache.cpp index 07c5ae68ce..3e9f37abcc 100644 --- a/search/nested_rects_cache.cpp +++ b/search/nested_rects_cache.cpp @@ -39,21 +39,26 @@ double NestedRectsCache::GetDistanceToFeatureMeters(FeatureID const & id) const if (!m_valid) return RankingInfo::kMaxDistMeters; - size_t bucket = 0; - for (; bucket != RECT_SCALE_COUNT; ++bucket) + int scale = 0; + for (; scale != RECT_SCALE_COUNT; ++scale) { - if (binary_search(m_features[bucket].begin(), m_features[bucket].end(), id)) + auto const & bucket = m_buckets[scale]; + auto const it = bucket.find(id.m_mwmId); + if (it == bucket.end()) + continue; + auto const & features = it->second; + if (binary_search(features.begin(), features.end(), id.m_index)) break; } - auto const scale = static_cast(bucket); if (scale != RECT_SCALE_COUNT) - return GetRadiusMeters(scale); + return GetRadiusMeters(static_cast(scale)); if (auto const & info = id.m_mwmId.GetInfo()) { auto const & rect = info->m_limitRect; - return max(MercatorBounds::DistanceOnEarth(rect.Center(), m_position), GetRadiusMeters(scale)); + return max(MercatorBounds::DistanceOnEarth(rect.Center(), m_position), + GetRadiusMeters(static_cast(scale))); } return RankingInfo::kMaxDistMeters; @@ -62,10 +67,7 @@ double NestedRectsCache::GetDistanceToFeatureMeters(FeatureID const & id) const void NestedRectsCache::Clear() { for (int scale = 0; scale != RECT_SCALE_COUNT; ++scale) - { - m_features[scale].clear(); - m_features[scale].shrink_to_fit(); - } + m_buckets[scale].clear(); m_valid = false; } @@ -86,17 +88,30 @@ void NestedRectsCache::Update() { for (int scale = 0; scale != RECT_SCALE_COUNT; ++scale) { - auto & features = m_features[scale]; + auto & bucket = m_buckets[scale]; + bucket.clear(); - features.clear(); m2::RectD const rect = MercatorBounds::RectByCenterXYAndSizeInMeters( m_position, GetRadiusMeters(static_cast(scale))); - auto addId = MakeBackInsertFunctor(features); + + MwmSet::MwmId lastId; + TFeatures * lastFeatures = nullptr; + auto addId = [&lastId, &lastFeatures, &bucket](FeatureID const & id) + { + if (!id.IsValid()) + return; + if (id.m_mwmId != lastId) + { + lastId = id.m_mwmId; + lastFeatures = &bucket[lastId]; + } + lastFeatures->push_back(id.m_index); + }; m_index.ForEachFeatureIDInRect(addId, rect, m_scale); - sort(features.begin(), features.end()); + for (auto & kv : bucket) + sort(kv.second.begin(), kv.second.end()); } m_valid = true; } - } // namespace search diff --git a/search/nested_rects_cache.hpp b/search/nested_rects_cache.hpp index 919ed29a74..077c37b34c 100644 --- a/search/nested_rects_cache.hpp +++ b/search/nested_rects_cache.hpp @@ -4,6 +4,9 @@ #include "geometry/point2d.hpp" +#include "std/map.hpp" +#include "std/vector.hpp" + class Index; namespace search @@ -39,8 +42,10 @@ private: m2::PointD m_position; bool m_valid; - // Sorted lists of features. - vector m_features[RECT_SCALE_COUNT]; -}; + using TFeatures = vector; + using TBucket = map; + // Sorted lists of features. + TBucket m_buckets[RECT_SCALE_COUNT]; +}; } // namespace search