From c22ca093e702812e992c148e0a5016813e5cd3e8 Mon Sep 17 00:00:00 2001 From: Arne Juul Date: Fri, 5 Oct 2018 20:20:44 +0000 Subject: HashedPointMap is only needed PointMapCollection --- .../src/vespa/vespalib/metrics/name_repo.cpp | 5 +-- .../src/vespa/vespalib/metrics/point_map.cpp | 49 +--------------------- .../src/vespa/vespalib/metrics/point_map.h | 13 ------ .../vespalib/metrics/point_map_collection.cpp | 46 ++++++++++++++++++-- .../vespa/vespalib/metrics/point_map_collection.h | 17 +++++++- 5 files changed, 61 insertions(+), 69 deletions(-) (limited to 'staging_vespalib/src') diff --git a/staging_vespalib/src/vespa/vespalib/metrics/name_repo.cpp b/staging_vespalib/src/vespa/vespalib/metrics/name_repo.cpp index 7db14f3af52..9e3683d465f 100644 --- a/staging_vespalib/src/vespa/vespalib/metrics/name_repo.cpp +++ b/staging_vespalib/src/vespa/vespalib/metrics/name_repo.cpp @@ -53,14 +53,13 @@ NameRepo::labelValue(Label l) const const PointMap& NameRepo::pointMap(Point from) const { - const HashedPointMap &map = _pointMaps.lookup(from.id()); - return map.backingMap(); + return _pointMaps.lookup(from.id()); } Point NameRepo::pointFrom(PointMap map) { - size_t id = _pointMaps.resolve(HashedPointMap(std::move(map))); + size_t id = _pointMaps.resolve(std::move(map)); return Point(id); } diff --git a/staging_vespalib/src/vespa/vespalib/metrics/point_map.cpp b/staging_vespalib/src/vespa/vespalib/metrics/point_map.cpp index 134123b2685..6e9ab4d1801 100644 --- a/staging_vespalib/src/vespa/vespalib/metrics/point_map.cpp +++ b/staging_vespalib/src/vespa/vespalib/metrics/point_map.cpp @@ -1,49 +1,2 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -#include "point.h" -#include "metrics_manager.h" - -namespace vespalib { -namespace metrics { - -HashedPointMap::HashedPointMap(PointMap &&from) - : _map(std::move(from)), - _hash(0) -{ - for (const PointMap::value_type &entry : _map) { - _hash = (_hash << 7) + (_hash >> 31) + entry.first.id(); - _hash = (_hash << 7) + (_hash >> 31) + entry.second.id(); - } -} - -bool -HashedPointMap::operator< (const HashedPointMap &other) const -{ - // cheap comparison first - if (_hash != other._hash) { - return _hash < other._hash; - } - if (_map.size() != other._map.size()) { - return _map.size() < other._map.size(); - } - // sizes equal, iterate in parallel - for (auto m = _map.begin(), o = other._map.begin(); - m != _map.end(); - ++m, ++o) - { - const Dimension& d1 = m->first; - const Dimension& d2 = o->first; - if (d1 != d2) { - return d1 < d2; - } - const Label &l1 = m->second; - const Label &l2 = o->second; - if (l1 != l2) { - return l1 < l2; - } - } - // equal - return false; -} - -} // namespace vespalib::metrics -} // namespace vespalib +#include "point_map.h" diff --git a/staging_vespalib/src/vespa/vespalib/metrics/point_map.h b/staging_vespalib/src/vespa/vespalib/metrics/point_map.h index 91c28b292c8..a1063ce944a 100644 --- a/staging_vespalib/src/vespa/vespalib/metrics/point_map.h +++ b/staging_vespalib/src/vespa/vespalib/metrics/point_map.h @@ -10,18 +10,5 @@ namespace metrics { using PointMap = std::map; -// internal -class HashedPointMap { -private: - const PointMap _map; - size_t _hash; -public: - HashedPointMap() : _map(), _hash(0) {} - HashedPointMap(PointMap &&from); - bool operator< (const HashedPointMap &other) const; - - const PointMap &backingMap() const { return _map; } -}; - } // namespace vespalib::metrics } // namespace vespalib diff --git a/staging_vespalib/src/vespa/vespalib/metrics/point_map_collection.cpp b/staging_vespalib/src/vespa/vespalib/metrics/point_map_collection.cpp index 36cb884290e..9909eb04cdc 100644 --- a/staging_vespalib/src/vespa/vespalib/metrics/point_map_collection.cpp +++ b/staging_vespalib/src/vespa/vespalib/metrics/point_map_collection.cpp @@ -5,19 +5,59 @@ namespace vespalib { namespace metrics { +HashedPointMap::HashedPointMap(PointMap &&from) + : _map(std::move(from)), + _hash(0) +{ + for (const PointMap::value_type &entry : _map) { + _hash = (_hash << 7) + (_hash >> 31) + entry.first.id(); + _hash = (_hash << 7) + (_hash >> 31) + entry.second.id(); + } +} + +bool +HashedPointMap::operator< (const HashedPointMap &other) const +{ + // cheap comparison first + if (_hash != other._hash) { + return _hash < other._hash; + } + if (_map.size() != other._map.size()) { + return _map.size() < other._map.size(); + } + // sizes equal, iterate in parallel + for (auto m = _map.begin(), o = other._map.begin(); + m != _map.end(); + ++m, ++o) + { + const Dimension& d1 = m->first; + const Dimension& d2 = o->first; + if (d1 != d2) { + return d1 < d2; + } + const Label &l1 = m->second; + const Label &l2 = o->second; + if (l1 != l2) { + return l1 < l2; + } + } + // equal + return false; +} + using Guard = std::lock_guard; -const HashedPointMap & +const PointMap & PointMapCollection::lookup(size_t id) const { Guard guard(_lock); assert(id < _vec.size()); PointMapMap::const_iterator iter = _vec[id]; - return iter->first; + return iter->first.backingMap(); } size_t -PointMapCollection::resolve(HashedPointMap map) +PointMapCollection::resolve(PointMap map) { Guard guard(_lock); size_t nextId = _vec.size(); diff --git a/staging_vespalib/src/vespa/vespalib/metrics/point_map_collection.h b/staging_vespalib/src/vespa/vespalib/metrics/point_map_collection.h index 91eb907ee74..3474bff6188 100644 --- a/staging_vespalib/src/vespa/vespalib/metrics/point_map_collection.h +++ b/staging_vespalib/src/vespa/vespalib/metrics/point_map_collection.h @@ -9,6 +9,19 @@ namespace vespalib { namespace metrics { +// internal +class HashedPointMap { +private: + const PointMap _map; + size_t _hash; +public: + HashedPointMap() : _map(), _hash(0) {} + HashedPointMap(PointMap &&from); + bool operator< (const HashedPointMap &other) const; + + const PointMap &backingMap() const { return _map; } +}; + // internal class PointMapCollection { private: @@ -18,8 +31,8 @@ private: PointMapMap _map; std::vector _vec; public: - const HashedPointMap &lookup(size_t id) const; - size_t resolve(HashedPointMap map); + const PointMap &lookup(size_t id) const; + size_t resolve(PointMap map); size_t size() const; PointMapCollection() = default; -- cgit v1.2.3