Skip to content
Snippets Groups Projects
Commit b73a50ba authored by Adrien Béraud's avatar Adrien Béraud
Browse files

pht: prevent unneeded use of shared_ptr

Prefer a const reference instead of shared_ptr if the shared_ptr
is not used and argument type is not changed in the method.

This allows to prevent useless shared_ptr allocations.

In getRealPrefix, use OpState to track state instead of using many
different shared_ptr.
parent 70ccbe4e
No related branches found
No related tags found
No related merge requests found
...@@ -304,13 +304,13 @@ public: ...@@ -304,13 +304,13 @@ public:
/* Specifications of the keys. It defines the number, the length and the /* Specifications of the keys. It defines the number, the length and the
* serialization order of fields. */ * serialization order of fields. */
using KeySpec = std::map<std::string, size_t>; using KeySpec = std::map<std::string, size_t>;
using LookupCallback = std::function<void(std::vector<std::shared_ptr<Value>>& values, Prefix p)>; using LookupCallback = std::function<void(std::vector<std::shared_ptr<Value>>& values, const Prefix& p)>;
typedef void (*LookupCallbackRaw)(std::vector<std::shared_ptr<Value>>* values, Prefix* p, void *user_data); typedef void (*LookupCallbackRaw)(std::vector<std::shared_ptr<Value>>* values, Prefix* p, void *user_data);
static LookupCallback static LookupCallback
bindLookupCb(LookupCallbackRaw raw_cb, void* user_data) { bindLookupCb(LookupCallbackRaw raw_cb, void* user_data) {
if (not raw_cb) return {}; if (not raw_cb) return {};
return [=](std::vector<std::shared_ptr<Value>>& values, Prefix p) { return [=](std::vector<std::shared_ptr<Value>>& values, const Prefix& p) {
raw_cb((std::vector<std::shared_ptr<Value>>*) &values, (Prefix*) &p, user_data); raw_cb((std::vector<std::shared_ptr<Value>>*) &values, (Prefix*) &p, user_data);
}; };
} }
...@@ -373,7 +373,7 @@ private: ...@@ -373,7 +373,7 @@ private:
* @param done_cb : Callback to call when the insert is done * @param done_cb : Callback to call when the insert is done
*/ */
void insert(Prefix kp, IndexEntry entry, std::shared_ptr<int> lo, std::shared_ptr<int> hi, time_point time_p, void insert(const Prefix& kp, IndexEntry entry, std::shared_ptr<int> lo, std::shared_ptr<int> hi, time_point time_p,
bool check_split, DoneCallbackSimple done_cb = {}); bool check_split, DoneCallbackSimple done_cb = {});
class Cache { class Cache {
...@@ -416,8 +416,8 @@ private: ...@@ -416,8 +416,8 @@ private:
}; };
/* Callback used for insert value by using the pht */ /* Callback used for insert value by using the pht */
using RealInsertCallback = std::function<void(std::shared_ptr<Prefix> p, IndexEntry entry)>; using RealInsertCallback = std::function<void(const Prefix& p, IndexEntry entry)>;
using LookupCallbackWrapper = std::function<void(std::vector<std::shared_ptr<IndexEntry>>& values, Prefix p)>; using LookupCallbackWrapper = std::function<void(std::vector<std::shared_ptr<IndexEntry>>& values, const Prefix& p)>;
/** /**
* Performs a step in the lookup operation. Each steps are performed * Performs a step in the lookup operation. Each steps are performed
...@@ -466,7 +466,7 @@ private: ...@@ -466,7 +466,7 @@ private:
* @param entry The entry to put at the prefix p * @param entry The entry to put at the prefix p
* @param end_cb Callback to use at the end of counting * @param end_cb Callback to use at the end of counting
*/ */
void getRealPrefix(std::shared_ptr<Prefix> p, IndexEntry entry, RealInsertCallback end_cb ); void getRealPrefix(const std::shared_ptr<Prefix>& p, IndexEntry entry, RealInsertCallback end_cb );
/** /**
* Looking where to put the data cause if there is free space on the node * Looking where to put the data cause if there is free space on the node
...@@ -485,12 +485,11 @@ private: ...@@ -485,12 +485,11 @@ private:
* @param vals : The vector of values to compare with comapred * @param vals : The vector of values to compare with comapred
* @return position compared diverge from all others * @return position compared diverge from all others
*/ */
size_t foundSplitLocation(Prefix compared, std::shared_ptr<std::vector<std::shared_ptr<IndexEntry>>> vals) { static size_t findSplitLocation(const Prefix& compared, const std::vector<std::shared_ptr<IndexEntry>>& vals) {
for ( size_t i = 0; i < compared.content_.size() * 8 - 1; i++ ) for ( size_t i = 0; i < compared.content_.size() * 8 - 1; i++ )
for ( auto const& v : *vals) for ( auto const& v : vals)
if ( Prefix(v->prefix).isContentBitActive(i) != compared.isContentBitActive(i) ) if ( Prefix(v->prefix).isContentBitActive(i) != compared.isContentBitActive(i) )
return i + 1; return i + 1;
return compared.content_.size() * 8 - 1; return compared.content_.size() * 8 - 1;
} }
...@@ -502,7 +501,7 @@ private: ...@@ -502,7 +501,7 @@ private:
* @param entry : Entry to put on the pht * @param entry : Entry to put on the pht
* @param end_cb : Callback to apply to the insert prefi (here does the insert) * @param end_cb : Callback to apply to the insert prefi (here does the insert)
*/ */
void split(Prefix insert, std::shared_ptr<std::vector<std::shared_ptr<IndexEntry>>> vals, IndexEntry entry, RealInsertCallback end_cb); void split(const Prefix& insert, const std::vector<std::shared_ptr<IndexEntry>>& vals, IndexEntry entry, RealInsertCallback end_cb);
/** /**
* Tells if the key is valid according to the key spec. * Tells if the key is valid according to the key spec.
......
...@@ -296,7 +296,7 @@ void Pht::lookup(Key k, Pht::LookupCallback cb, DoneCallbackSimple done_cb, bool ...@@ -296,7 +296,7 @@ void Pht::lookup(Key k, Pht::LookupCallback cb, DoneCallbackSimple done_cb, bool
std::shared_ptr<unsigned> max_common_prefix_len = not exact_match ? std::make_shared<unsigned>(0) : nullptr; std::shared_ptr<unsigned> max_common_prefix_len = not exact_match ? std::make_shared<unsigned>(0) : nullptr;
lookupStep(prefix, lo, hi, values, lookupStep(prefix, lo, hi, values,
[=](std::vector<std::shared_ptr<IndexEntry>>& entries, Prefix p) { [=](std::vector<std::shared_ptr<IndexEntry>>& entries, const Prefix& p) {
std::vector<std::shared_ptr<Value>> vals(entries.size()); std::vector<std::shared_ptr<Value>> vals(entries.size());
std::transform(entries.begin(), entries.end(), vals.begin(), std::transform(entries.begin(), entries.end(), vals.begin(),
...@@ -329,7 +329,7 @@ void Pht::updateCanary(Prefix p) { ...@@ -329,7 +329,7 @@ void Pht::updateCanary(Prefix p) {
} }
} }
void Pht::insert(Prefix kp, IndexEntry entry, std::shared_ptr<int> lo, std::shared_ptr<int> hi, time_point time_p, void Pht::insert(const Prefix& kp, IndexEntry entry, std::shared_ptr<int> lo, std::shared_ptr<int> hi, time_point time_p,
bool check_split, DoneCallbackSimple done_cb) { bool check_split, DoneCallbackSimple done_cb) {
if (time_p + ValueType::USER_DATA.expiration < clock::now()) return; if (time_p + ValueType::USER_DATA.expiration < clock::now()) return;
...@@ -347,26 +347,25 @@ void Pht::insert(Prefix kp, IndexEntry entry, std::shared_ptr<int> lo, std::shar ...@@ -347,26 +347,25 @@ void Pht::insert(Prefix kp, IndexEntry entry, std::shared_ptr<int> lo, std::shar
done_cb(false); done_cb(false);
} else { } else {
RealInsertCallback real_insert = [=]( std::shared_ptr<Prefix> p, IndexEntry entry) { RealInsertCallback real_insert = [=](const Prefix& p, IndexEntry entry) {
updateCanary(*p); updateCanary(p);
checkPhtUpdate(*p, entry, time_p); checkPhtUpdate(p, entry, time_p);
cache_.insert(*p); cache_.insert(p);
dht_->put(p->hash(), std::move(entry), done_cb , time_p); dht_->put(p.hash(), std::move(entry), done_cb , time_p);
}; };
if ( not check_split or final_prefix->size_ == kp.size_ ) { if ( not check_split or final_prefix->size_ == kp.size_ ) {
real_insert(final_prefix, std::move(entry)); real_insert(*final_prefix, std::move(entry));
} else { } else {
if ( vals->size() < MAX_NODE_ENTRY_COUNT ) { if ( vals->size() < MAX_NODE_ENTRY_COUNT ) {
getRealPrefix(final_prefix, std::move(entry), real_insert); getRealPrefix(final_prefix, std::move(entry), real_insert);
} }
else { else {
split(*final_prefix, vals, entry, real_insert); split(*final_prefix, *vals, entry, real_insert);
} }
} }
} }
}, nullptr, cache_.lookup(kp), true }, nullptr, cache_.lookup(kp), true);
);
} }
Prefix Pht::zcurve(const std::vector<Prefix>& all_prefix) const { Prefix Pht::zcurve(const std::vector<Prefix>& all_prefix) const {
...@@ -440,43 +439,44 @@ Prefix Pht::linearize(Key k) const { ...@@ -440,43 +439,44 @@ Prefix Pht::linearize(Key k) const {
return zcurve(all_prefix); return zcurve(all_prefix);
} }
void Pht::getRealPrefix(std::shared_ptr<Prefix> p, IndexEntry entry, RealInsertCallback end_cb ) { void Pht::getRealPrefix(const std::shared_ptr<Prefix>& p, IndexEntry entry, RealInsertCallback end_cb )
{
if ( p->size_ == 0 ) { if ( p->size_ == 0 ) {
end_cb(p, std::move(entry)); end_cb(*p, std::move(entry));
return; return;
} }
auto total = std::make_shared<unsigned int>(0); /* Will contains the total number of data on 3 nodes */ struct OpState {
auto ended = std::make_shared<unsigned int>(0); /* Just indicate how many have end */ unsigned entry_count {0}; /* Total number of data on 3 nodes */
unsigned ended {0}; /* How many ops have ended */
auto parent = std::make_shared<Prefix>(p->getPrefix(-1)); Prefix parent;
auto sibling = std::make_shared<Prefix>(p->getSibling()); OpState(Prefix p) : parent(p) {}
};
auto op_state = std::make_shared<OpState>(p->getPrefix(-1));
auto pht_filter = [&](const dht::Value& v) { auto pht_filter = [&](const dht::Value& v) {
return v.user_type.compare(0, name_.size(), name_) == 0; return v.user_type.compare(0, name_.size(), name_) == 0;
}; };
/* Lambda will count total number of data node */ /* Lambda will count total number of data node */
auto count = [=]( const std::shared_ptr<dht::Value> value ) { auto count = [=]( const std::shared_ptr<dht::Value>& value ) {
if (value->user_type != canary_) if (value->user_type != canary_)
(*total)++; op_state->entry_count++;
return true; return true;
}; };
auto on_done = [=] ( bool ) { auto on_done = [=] ( bool ) {
(*ended)++; op_state->ended++;
/* Only the last one do the CallBack*/ /* Only the last one do the CallBack*/
if ( *ended == 3 ) { if (op_state->ended == 3) {
if ( *total < MAX_NODE_ENTRY_COUNT ) if (op_state->entry_count < MAX_NODE_ENTRY_COUNT)
end_cb(parent, std::move(entry)); end_cb(op_state->parent, std::move(entry));
else else
end_cb(p, std::move(entry)); end_cb(*p, std::move(entry));
} }
}; };
dht_->get(parent->hash(), dht_->get(op_state->parent.hash(),
count, count,
on_done, on_done,
pht_filter pht_filter
...@@ -488,7 +488,7 @@ void Pht::getRealPrefix(std::shared_ptr<Prefix> p, IndexEntry entry, RealInsertC ...@@ -488,7 +488,7 @@ void Pht::getRealPrefix(std::shared_ptr<Prefix> p, IndexEntry entry, RealInsertC
pht_filter pht_filter
); );
dht_->get(sibling->hash(), dht_->get(p->getSibling().hash(),
count, count,
on_done, on_done,
pht_filter pht_filter
...@@ -520,11 +520,11 @@ void Pht::checkPhtUpdate(Prefix p, IndexEntry entry, time_point time_p) { ...@@ -520,11 +520,11 @@ void Pht::checkPhtUpdate(Prefix p, IndexEntry entry, time_point time_p) {
); );
} }
void Pht::split(Prefix insert, std::shared_ptr<std::vector<std::shared_ptr<IndexEntry>>> vals, IndexEntry entry, RealInsertCallback end_cb ) { void Pht::split(const Prefix& insert, const std::vector<std::shared_ptr<IndexEntry>>& vals, IndexEntry entry, RealInsertCallback end_cb ) {
auto full = Prefix(entry.prefix); const auto full = Prefix(entry.prefix);
auto loc = foundSplitLocation(full, vals); auto loc = findSplitLocation(full, vals);
auto prefix_to_insert = std::make_shared<Prefix>(full.getPrefix(loc)); const auto prefix_to_insert = full.getPrefix(loc);
for(;loc != insert.size_ - 1; loc--) { for(;loc != insert.size_ - 1; loc--) {
updateCanary(full.getPrefix(loc)); updateCanary(full.getPrefix(loc));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment