From 8e1b29f5128d4033e1566af7aad18eb5d182a0d6 Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Fri, 27 Jul 2018 09:55:01 -0700 Subject: [PATCH 1/5] don't overallocate over-optimistically --- src/coalesce.cpp | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/coalesce.cpp b/src/coalesce.cpp index 83c8cee..5c08a42 100644 --- a/src/coalesce.cpp +++ b/src/coalesce.cpp @@ -388,7 +388,6 @@ void coalesceSingle(uv_work_t* req) { unsigned long m = grids.size(); double relevMax = 0; std::vector covers; - covers.reserve(m); uint32_t length = 0; uint32_t lastId = 0; @@ -438,7 +437,6 @@ void coalesceSingle(uv_work_t* req) { std::size_t added = 0; std::vector contexts; std::size_t max_contexts = 40; - contexts.reserve(max_contexts); for (auto&& cover : covers) { // Stop at 40 contexts if (added == max_contexts) break; @@ -453,7 +451,6 @@ void coalesceSingle(uv_work_t* req) { uint32_t mask = 0; contexts.emplace_back(std::move(cover), mask, relev); } - coalesceFinalize(baton, std::move(contexts)); } catch (std::exception const& ex) { baton->error = ex.what(); @@ -474,7 +471,8 @@ void coalesceMulti(uv_work_t* req) { std::vector zoomCache; zoomCache.reserve(stackSize); for (auto const& subq : stack) { - intarray zooms; + zoomCache.emplace_back(); + auto & zooms = zoomCache.back(); std::vector zoomUniq(22, false); for (auto const& subqB : stack) { if (subq.idx == subqB.idx) continue; @@ -483,7 +481,6 @@ void coalesceMulti(uv_work_t* req) { zoomUniq[subqB.zoom] = true; zooms.emplace_back(subqB.zoom); } - zoomCache.push_back(std::move(zooms)); } // Coalesce relevs into higher zooms, e.g. @@ -570,11 +567,7 @@ void coalesceMulti(uv_work_t* req) { uint64_t zxy = (z * POW2_28) + (cover.x * POW2_14) + (cover.y); - // Reserve stackSize for the coverList. The vector - // will grow no larger that the size of the input - // subqueries that are being coalesced. std::vector covers; - covers.reserve(stackSize); covers.push_back(cover); uint32_t context_mask = cover.mask; double context_relev = cover.relev; @@ -720,4 +713,4 @@ void coalesceFinalize(CoalesceBaton* baton, std::vector&& contexts) { } } -} // namespace carmen \ No newline at end of file +} // namespace carmen From 9593993741e806e33d58606f85c117ec303a0df1 Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Fri, 27 Jul 2018 10:09:57 -0700 Subject: [PATCH 2/5] limit the amount of contexts we need to sort by filtering in relev --- src/coalesce.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/coalesce.cpp b/src/coalesce.cpp index 5c08a42..9f81290 100644 --- a/src/coalesce.cpp +++ b/src/coalesce.cpp @@ -470,6 +470,7 @@ void coalesceMulti(uv_work_t* req) { // Cache zoom levels to iterate over as coalesce occurs. std::vector zoomCache; zoomCache.reserve(stackSize); + double maxrelev = 0; for (auto const& subq : stack) { zoomCache.emplace_back(); auto & zooms = zoomCache.back(); @@ -605,7 +606,7 @@ void coalesceMulti(uv_work_t* req) { } } } - + maxrelev = std::max(maxrelev,context_relev); if (last) { // Slightly penalize contexts that have no stacking if (covers.size() == 1) { @@ -614,7 +615,9 @@ void coalesceMulti(uv_work_t* req) { } else if (covers[0].mask > covers[1].mask) { context_relev -= 0.01; } - contexts.emplace_back(std::move(covers), context_mask, context_relev); + if (maxrelev - context_relev < .25 ) { + contexts.emplace_back(std::move(covers), context_mask, context_relev); + } } else if (first || covers.size() > 1) { cit = coalesced.find(zxy); if (cit == coalesced.end()) { @@ -633,7 +636,9 @@ void coalesceMulti(uv_work_t* req) { // append coalesced to contexts by moving memory for (auto&& matched : coalesced) { for (auto&& context : matched.second) { - contexts.emplace_back(std::move(context)); + if (maxrelev - context.relev < .25 ) { + contexts.emplace_back(std::move(context)); + } } } From b34e64397cf02e5b87178f113d050aa52ae8c037 Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Fri, 27 Jul 2018 10:29:29 -0700 Subject: [PATCH 3/5] avoid a few copies by more aggresively opting into move semantics --- src/coalesce.cpp | 2 +- src/cpp_util.hpp | 7 +++++++ src/rocksdbcache.cpp | 4 ++-- src/rocksdbcache.hpp | 14 ++++++++++++++ 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/coalesce.cpp b/src/coalesce.cpp index 9f81290..49eddfe 100644 --- a/src/coalesce.cpp +++ b/src/coalesce.cpp @@ -569,7 +569,7 @@ void coalesceMulti(uv_work_t* req) { uint64_t zxy = (z * POW2_28) + (cover.x * POW2_14) + (cover.y); std::vector covers; - covers.push_back(cover); + covers.push_back(std::move(cover)); uint32_t context_mask = cover.mask; double context_relev = cover.relev; diff --git a/src/cpp_util.hpp b/src/cpp_util.hpp index c7f55b6..d96b9b0 100644 --- a/src/cpp_util.hpp +++ b/src/cpp_util.hpp @@ -109,6 +109,13 @@ struct Cover { double distance; double scoredist; bool matches_language; + + Cover() = default; + Cover(Cover const& c) = default; + Cover& operator=(Cover const& c) = delete; + Cover& operator=(Cover&& c) = default; + Cover(Cover&& c) = default; + }; struct Context { diff --git a/src/rocksdbcache.cpp b/src/rocksdbcache.cpp index e726a96..b641fa4 100644 --- a/src/rocksdbcache.cpp +++ b/src/rocksdbcache.cpp @@ -82,11 +82,11 @@ intarray __getmatching(RocksDBCache const* c, std::string phrase, bool match_pre if (vals.first != vals.second) { value_type unadjusted_lastval = *(vals.first); - grids.emplace_back(sortableGrid{ + grids.emplace_back( vals.first, vals.second, unadjusted_lastval, - matches_language}); + matches_language); rh.push(matches_language ? unadjusted_lastval | LANGUAGE_MATCH_BOOST : unadjusted_lastval, grids.size() - 1); } } diff --git a/src/rocksdbcache.hpp b/src/rocksdbcache.hpp index 2a2d924..54d307a 100644 --- a/src/rocksdbcache.hpp +++ b/src/rocksdbcache.hpp @@ -30,10 +30,24 @@ namespace carmen { #define PREFIX_MAX_GRID_LENGTH 500000 struct sortableGrid { + sortableGrid(protozero::const_varint_iterator _it, + protozero::const_varint_iterator _end, + value_type _unadjusted_lastval, + bool _matches_language) + : it(_it), + end(_end), + unadjusted_lastval(_unadjusted_lastval), + matches_language(_matches_language) { + } protozero::const_varint_iterator it; protozero::const_varint_iterator end; value_type unadjusted_lastval; bool matches_language; + sortableGrid() = delete; + sortableGrid(sortableGrid const& c) = delete; + sortableGrid& operator=(sortableGrid const& c) = delete; + sortableGrid& operator=(sortableGrid&& c) = default; + sortableGrid(sortableGrid&& c) = default; }; struct MergeBaton : carmen::noncopyable { From eaa7bc1fb2da0371630ca836600c7546c984213f Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Fri, 27 Jul 2018 10:32:13 -0700 Subject: [PATCH 4/5] format the code --- src/coalesce.cpp | 8 ++++---- src/cpp_util.hpp | 1 - src/rocksdbcache.hpp | 6 +++--- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/coalesce.cpp b/src/coalesce.cpp index 49eddfe..dfed496 100644 --- a/src/coalesce.cpp +++ b/src/coalesce.cpp @@ -473,7 +473,7 @@ void coalesceMulti(uv_work_t* req) { double maxrelev = 0; for (auto const& subq : stack) { zoomCache.emplace_back(); - auto & zooms = zoomCache.back(); + auto& zooms = zoomCache.back(); std::vector zoomUniq(22, false); for (auto const& subqB : stack) { if (subq.idx == subqB.idx) continue; @@ -606,7 +606,7 @@ void coalesceMulti(uv_work_t* req) { } } } - maxrelev = std::max(maxrelev,context_relev); + maxrelev = std::max(maxrelev, context_relev); if (last) { // Slightly penalize contexts that have no stacking if (covers.size() == 1) { @@ -615,7 +615,7 @@ void coalesceMulti(uv_work_t* req) { } else if (covers[0].mask > covers[1].mask) { context_relev -= 0.01; } - if (maxrelev - context_relev < .25 ) { + if (maxrelev - context_relev < .25) { contexts.emplace_back(std::move(covers), context_mask, context_relev); } } else if (first || covers.size() > 1) { @@ -636,7 +636,7 @@ void coalesceMulti(uv_work_t* req) { // append coalesced to contexts by moving memory for (auto&& matched : coalesced) { for (auto&& context : matched.second) { - if (maxrelev - context.relev < .25 ) { + if (maxrelev - context.relev < .25) { contexts.emplace_back(std::move(context)); } } diff --git a/src/cpp_util.hpp b/src/cpp_util.hpp index d96b9b0..18e43cb 100644 --- a/src/cpp_util.hpp +++ b/src/cpp_util.hpp @@ -115,7 +115,6 @@ struct Cover { Cover& operator=(Cover const& c) = delete; Cover& operator=(Cover&& c) = default; Cover(Cover&& c) = default; - }; struct Context { diff --git a/src/rocksdbcache.hpp b/src/rocksdbcache.hpp index 54d307a..a1bfcd6 100644 --- a/src/rocksdbcache.hpp +++ b/src/rocksdbcache.hpp @@ -31,9 +31,9 @@ namespace carmen { struct sortableGrid { sortableGrid(protozero::const_varint_iterator _it, - protozero::const_varint_iterator _end, - value_type _unadjusted_lastval, - bool _matches_language) + protozero::const_varint_iterator _end, + value_type _unadjusted_lastval, + bool _matches_language) : it(_it), end(_end), unadjusted_lastval(_unadjusted_lastval), From 5155c74b42e6189df6d152b14b4ef81689492787 Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Fri, 27 Jul 2018 12:39:40 -0700 Subject: [PATCH 5/5] [publish binary] 0.21.3-optimize --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 88600a9..6ba0eb6 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "url": "git://github.com/mapbox/carmen-cache.git", "type": "git" }, - "version": "0.21.3", + "version": "0.21.3-optimize", "dependencies": { "nan": "~2.10.0", "node-pre-gyp": "~0.10.1"