From 268129b9c17cb9489b1e7024cb12c684b56f485f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Wed, 1 Apr 2015 15:30:57 +0200 Subject: [PATCH] use cumulative hash instead of string for linedash lookup --- src/mbgl/geometry/line_atlas.cpp | 29 ++++++++++++++++------------- src/mbgl/geometry/line_atlas.hpp | 2 +- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/mbgl/geometry/line_atlas.cpp b/src/mbgl/geometry/line_atlas.cpp index 507cc7b08..3816b929e 100644 --- a/src/mbgl/geometry/line_atlas.cpp +++ b/src/mbgl/geometry/line_atlas.cpp @@ -4,6 +4,8 @@ #include #include +#include + #include #include @@ -26,21 +28,22 @@ LineAtlas::~LineAtlas() { } LinePatternPos LineAtlas::getDashPosition(const std::vector &dasharray, bool round) { + size_t key = round ? std::numeric_limits::min() : std::numeric_limits::max(); + for (const float part : dasharray) { + boost::hash_combine(key, part); + } + + // Note: We're not handling hash collisions here. + std::lock_guard lock(mtx); - - std::ostringstream sskey; - - for (const float &part : dasharray) { - sskey << part << "-"; + const auto it = positions.find(key); + if (it == positions.end()) { + auto inserted = positions.emplace(key, addDash(dasharray, round)); + assert(inserted.second); + return inserted.first->second; + } else { + return it->second; } - sskey << round; - std::string key = sskey.str(); - - if (positions.find(key) == positions.end()) { - positions[key] = addDash(dasharray, round); - } - - return positions[key]; } LinePatternPos LineAtlas::addDash(const std::vector &dasharray, bool round) { diff --git a/src/mbgl/geometry/line_atlas.hpp b/src/mbgl/geometry/line_atlas.hpp index 191dc0c4d..df60a2dec 100644 --- a/src/mbgl/geometry/line_atlas.hpp +++ b/src/mbgl/geometry/line_atlas.hpp @@ -33,7 +33,7 @@ private: std::atomic dirty; uint32_t texture = 0; int nextRow = 0; - std::map positions; + std::map positions; }; };