From b3bbf8e2e8f4d92e1fdb8fbde13af4e742f3ba32 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Mon, 7 Dec 2020 03:39:17 -0600 Subject: [PATCH 1/4] prep: add move_cons lib --- .gitattributes | 1 + VendorLib/move_const.h | 11 +++++++++++ binding.gyp | 3 ++- 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 VendorLib/move_const.h diff --git a/.gitattributes b/.gitattributes index 2e251d7f..6d27d16d 100644 --- a/.gitattributes +++ b/.gitattributes @@ -6,3 +6,4 @@ spec/filter-options-spec.coffee linguist-vendored spec/filter-spec.coffee linguist-vendored spec/match-spec.coffee linguist-vendored spec/score-spec.coffee linguist-vendored +VendorLib/ diff --git a/VendorLib/move_const.h b/VendorLib/move_const.h new file mode 100644 index 00000000..6a8a119a --- /dev/null +++ b/VendorLib/move_const.h @@ -0,0 +1,11 @@ +// https://raw.githubusercontent.com/aminya/move_const/0.1.0/move_const.h + +#include + +template +typename std::remove_const::type>::type&& move_const(T&& arg) +{ + return const_cast::type>::type&&>( + static_cast::type&&>(arg) + ); +} diff --git a/binding.gyp b/binding.gyp index 956116a2..d5071b0c 100644 --- a/binding.gyp +++ b/binding.gyp @@ -4,7 +4,8 @@ "target_name": "fuzzaldrinplusfast", "sources": [ "src/common.h", "src/fuzzaldrin.cc", "src/scorer.cc", "src/path_scorer.cc", "src/filter.cc", "src/query.cc", "src/matcher.cc", "src/tree.h" ], "include_dirs": [ - " Date: Mon, 7 Dec 2020 03:50:27 -0600 Subject: [PATCH 2/4] prep: make move_const noexcept --- VendorLib/move_const.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VendorLib/move_const.h b/VendorLib/move_const.h index 6a8a119a..8fdcbc84 100644 --- a/VendorLib/move_const.h +++ b/VendorLib/move_const.h @@ -3,7 +3,7 @@ #include template -typename std::remove_const::type>::type&& move_const(T&& arg) +typename std::remove_const::type>::type&& move_const(T&& arg) noexcept { return const_cast::type>::type&&>( static_cast::type&&>(arg) From db4c853c53ea55ac297c8db5ff144d377fccb33f Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Mon, 7 Dec 2020 03:53:13 -0600 Subject: [PATCH 3/4] fix: use move_const in Tree --- src/common.h | 1 + src/tree.h | 25 +++++++++++++------------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/common.h b/src/common.h index abb8ecc9..d6222caf 100644 --- a/src/common.h +++ b/src/common.h @@ -10,6 +10,7 @@ #include #include #include +#include #include diff --git a/src/tree.h b/src/tree.h index be1ff45c..8cd0e755 100644 --- a/src/tree.h +++ b/src/tree.h @@ -5,10 +5,10 @@ #include "common.h" /** Get the children of a jsTree (Napi::Object) */ -inline std::optional getChildren(const Napi::Object &jsTree, const string &childrenKey) { +inline std::optional getChildren(const Napi::Object &&jsTree, const string &&childrenKey) { // determine if it has children if (jsTree.HasOwnProperty(childrenKey)) { - const auto childrenRaw = jsTree.Get(childrenKey); + const auto childrenRaw = jsTree.Get(move_const(childrenKey)); if (childrenRaw.IsArray()) { const auto childrenArray = childrenRaw.As(); if (childrenArray.Length() != 0) { @@ -25,8 +25,8 @@ struct CandidateObject { const size_t level = 0; const size_t index = 0; - explicit CandidateObject(CandidateString &&data_, const size_t level_, const size_t index_) noexcept - : data{ move(data_) }, level{ level_ }, index{ index_ } {} + explicit CandidateObject(const CandidateString &&data_, const size_t level_, const size_t index_) noexcept + : data{ move_const(data_) }, level{ level_ }, index{ index_ } {} }; struct Tree { @@ -37,7 +37,7 @@ struct Tree { /** Recursive function that fills the entriesArray from the given jsTreeArray */ - void makeEntriesArray(const Napi::Array &jsTreeArray, const size_t level) { + void makeEntriesArray(const Napi::Array &&jsTreeArray, const size_t level) { const auto entriesArrayLength = jsTreeArray.Length(); entriesArray.reserve(entriesArrayLength);// reserve enough space for (auto iEntry = 0u; iEntry < entriesArrayLength; iEntry++) { @@ -46,7 +46,7 @@ struct Tree { } /** 1st argument is a single object */ - void makeEntriesArray(const Napi::Object &jsTree, const size_t level, const size_t iEntry) { + void makeEntriesArray(const Napi::Object &&jsTree, const size_t level, const size_t iEntry) { // make the CandidateObject and push it back entriesArray.emplace_back( jsTree.Get(dataKey).ToString().Utf8Value(),// first, get the current data @@ -56,10 +56,10 @@ struct Tree { ); // add children if any - auto mayChildren = getChildren(jsTree, childrenKey); + const auto mayChildren = getChildren(move_const(jsTree), move_const(childrenKey)); if (mayChildren.has_value()) { // recurse - makeEntriesArray(mayChildren.value(), level + 1); + makeEntriesArray(move_const(mayChildren.value()), level + 1); } } @@ -68,11 +68,12 @@ struct Tree { /** create a Tree object and make an entries array */ // NOTE: this is made to only accept Napi::Array because we cannot export templates to JavaScript - explicit Tree(const Napi::Array &jsTreeArrayOrObject_, const string &dataKey_, const string &childrenKey_) - : dataKey{ dataKey_ }, - childrenKey{ childrenKey_ } { - makeEntriesArray(jsTreeArrayOrObject_, 0); + explicit Tree(const Napi::Array &&jsTreeArrayOrObject_, const string &&dataKey_, const string &&childrenKey_) + : dataKey{ move_const(dataKey_) }, + childrenKey{ move_const(childrenKey_) } { + makeEntriesArray(move_const(jsTreeArrayOrObject_), 0); } + }; #endif// Fuzzaldrin_tree_h_ From dc739b013f2ecd85e21764c90f3480f6211950db Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Mon, 7 Dec 2020 04:10:05 -0600 Subject: [PATCH 4/4] fix: use ref in setTreeFiltererCandidates --- src/fuzzaldrin.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/fuzzaldrin.cc b/src/fuzzaldrin.cc index c272b67f..646f0e08 100644 --- a/src/fuzzaldrin.cc +++ b/src/fuzzaldrin.cc @@ -64,11 +64,11 @@ Napi::Value Fuzzaldrin::setTreeFiltererCandidates(const Napi::CallbackInfo &info const string childrenKey = info[2].As(); // create Tree and set candidates - _tree = Tree(jsTreeArray, dataKey, childrenKey); + _tree = Tree(move_const(jsTreeArray), move_const(dataKey), move_const(childrenKey)); - const auto &candidates = _tree.entriesArray; + const auto candidates = ref(_tree.entriesArray); - const auto N = candidates.size();// different + const auto N = candidates.get().size();// different const auto num_chunks = N < 1000 * kMaxThreads ? N / 1000 + 1 : kMaxThreads; candidates_.clear(); candidates_.resize(num_chunks); @@ -80,7 +80,7 @@ Napi::Value Fuzzaldrin::setTreeFiltererCandidates(const Napi::CallbackInfo &info chunk_size++; } for (auto j = cur_start; j < cur_start + chunk_size; j++) { - candidates_[i].emplace_back(candidates[j].data);// different // TODO copy + candidates_[i].emplace_back(candidates.get()[j].data);// different // TODO copy } cur_start += chunk_size; }