Use std::shared_ptr for variable resolution

AnchoredSetVariable::resolve is called for every rule
(see RuleWithOperator::evaluate). The previous implementation allocated
a new copy of every variable, which quickly added up. In my tests,
AnchoredSetVariable::resolve function consumed 7.8% of run time.

AnchoredSetVariable (which is a multimap) values are never changed,
only added. This means it's safe to store them in std::shared_ptr,
and make resolve return shared_ptr pointing to the same object.

Other resolve implementation could also use this optimization by not
allocating new objects, however, they are not hot spots, so this
optimization was not implemented there.

In my benchmark, this raises performance from 117 requests per second to
131 RPS, and overhead is lowered from 7.8% to 2.4%.

As a bonus, replacing plain pointer with smart pointers make code
cleaner, since using smart pointers makes manual deletes no longer necessary.

Additionally, VariableOrigin is now stored in plain std::vector,
since it's wasteful to store structure containing just two integer
values using std::list<std::unique_ptr<T>>.
This commit is contained in:
WGH
2020-07-28 18:46:03 +03:00
committed by Felipe Zimmerle
parent 7d4cb31b72
commit 1b13e041ce
55 changed files with 264 additions and 376 deletions

View File

@@ -265,14 +265,16 @@ bool RuleWithOperator::evaluate(Transaction *trans) const {
getFinalVars(&vars, &exclusion, trans);
std::vector<std::shared_ptr<const VariableValue>> e;
for (auto &var : vars) {
std::vector<const VariableValue *> e;
if (!var) {
continue;
}
e.clear();
var->evaluate(trans, &e);
for (const VariableValue *v : e) {
for (const auto &vv : e) {
TransformationsResults transformationsResults;
const VariableValue *v = vv.get();
const std::string &value = v->getValue();
const std::string &key = v->getKeyWithCollection();
@@ -283,8 +285,6 @@ bool RuleWithOperator::evaluate(Transaction *trans) const {
return m.first == getId() && m.second == v->getKeyWithCollection();
}) != trans->m_ruleRemoveTargetById.end()
) {
delete v;
v = NULL;
continue;
}
if (exclusion.contains(v) ||
@@ -294,8 +294,6 @@ bool RuleWithOperator::evaluate(Transaction *trans) const {
return containsTag(m.first, trans) && m.second == v->getKeyWithCollection();
}) != trans->m_ruleRemoveTargetByTag.end()
) {
delete v;
v = NULL;
continue;
}
@@ -317,8 +315,8 @@ bool RuleWithOperator::evaluate(Transaction *trans) const {
trans->messageGetLast()->m_match = m_operator->resolveMatchMessage(trans,
key, value);
for (auto &i : v->getOrigin()) {
trans->messageGetLast()->m_reference.append(i->toText());
for (const auto &i : v->getOrigin()) {
trans->messageGetLast()->m_reference.append(i.toText());
}
auto iter2 = transformationsResults.begin();
@@ -344,11 +342,7 @@ bool RuleWithOperator::evaluate(Transaction *trans) const {
iter++;
}
delete v;
v = NULL;
}
e.clear();
e.reserve(4);
}
if (globalRet == false) {