Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move const #47

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -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/
11 changes: 11 additions & 0 deletions VendorLib/move_const.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// https://raw.githubusercontent.com/aminya/move_const/0.1.0/move_const.h

#include <type_traits>

template <typename T>
typename std::remove_const<typename std::remove_reference<T>::type>::type&& move_const(T&& arg) noexcept
{
return const_cast<typename std::remove_const<typename std::remove_reference<T>::type>::type&&>(
static_cast<typename std::remove_reference<T>::type&&>(arg)
);
}
3 changes: 2 additions & 1 deletion binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
"<!@(node -p \"require('node-addon-api').include\")"
"<!@(node -p \"require('node-addon-api').include\")",
"VendorLib"
],
# Compiler flags:
'default_configuration': 'Release',
Expand Down
1 change: 1 addition & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <iostream>
#include <utility>
#include <cassert>
#include <move_const.h>

#include <napi.h>

Expand Down
8 changes: 4 additions & 4 deletions src/fuzzaldrin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ Napi::Value Fuzzaldrin::setTreeFiltererCandidates(const Napi::CallbackInfo &info
const string childrenKey = info[2].As<Napi::String>();

// 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);
Expand All @@ -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;
}
Expand Down
25 changes: 13 additions & 12 deletions src/tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
#include "common.h"

/** Get the children of a jsTree (Napi::Object) */
inline std::optional<Napi::Array> getChildren(const Napi::Object &jsTree, const string &childrenKey) {
inline std::optional<Napi::Array> 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<Napi::Array>();
if (childrenArray.Length() != 0) {
Expand All @@ -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 {
Expand All @@ -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++) {
Expand All @@ -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
Expand All @@ -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);
}
}

Expand All @@ -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_