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" diff --git a/src/coalesce.cpp b/src/coalesce.cpp index 83c8cee..dfed496 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(); @@ -473,8 +470,10 @@ 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) { - 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 +482,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,12 +568,8 @@ 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); + covers.push_back(std::move(cover)); uint32_t context_mask = cover.mask; double context_relev = cover.relev; @@ -612,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) { @@ -621,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()) { @@ -640,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)); + } } } @@ -720,4 +718,4 @@ void coalesceFinalize(CoalesceBaton* baton, std::vector&& contexts) { } } -} // namespace carmen \ No newline at end of file +} // namespace carmen diff --git a/src/cpp_util.hpp b/src/cpp_util.hpp index c7f55b6..18e43cb 100644 --- a/src/cpp_util.hpp +++ b/src/cpp_util.hpp @@ -109,6 +109,12 @@ 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..a1bfcd6 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 {