mirror of
https://github.com/owasp-modsecurity/ModSecurity.git
synced 2025-11-18 10:20:35 +03:00
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:
@@ -86,17 +86,17 @@ void InMemoryPerProcess::del(const std::string& key) {
|
||||
|
||||
|
||||
void InMemoryPerProcess::resolveSingleMatch(const std::string& var,
|
||||
std::vector<const VariableValue *> *l) {
|
||||
std::vector<std::shared_ptr<const VariableValue>> *l) {
|
||||
auto range = this->equal_range(var);
|
||||
|
||||
for (auto it = range.first; it != range.second; ++it) {
|
||||
l->push_back(new VariableValue(&m_name, &it->first, &it->second));
|
||||
l->push_back(std::make_shared<VariableValue>(&m_name, &it->first, &it->second));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void InMemoryPerProcess::resolveMultiMatches(const std::string& var,
|
||||
std::vector<const VariableValue *> *l, variables::KeyExclusions &ke) {
|
||||
std::vector<std::shared_ptr<const VariableValue>> *l, variables::KeyExclusions &ke) {
|
||||
size_t keySize = var.size();
|
||||
l->reserve(15);
|
||||
|
||||
@@ -105,8 +105,7 @@ void InMemoryPerProcess::resolveMultiMatches(const std::string& var,
|
||||
if (ke.toOmit(i.first)) {
|
||||
continue;
|
||||
}
|
||||
l->insert(l->begin(), new VariableValue(&m_name, &i.first,
|
||||
&i.second));
|
||||
l->insert(l->begin(), std::make_shared<VariableValue>(&m_name, &i.first, &i.second));
|
||||
}
|
||||
} else {
|
||||
auto range = this->equal_range(var);
|
||||
@@ -114,15 +113,14 @@ void InMemoryPerProcess::resolveMultiMatches(const std::string& var,
|
||||
if (ke.toOmit(var)) {
|
||||
continue;
|
||||
}
|
||||
l->insert(l->begin(), new VariableValue(&m_name, &var,
|
||||
&it->second));
|
||||
l->insert(l->begin(), std::make_shared<VariableValue>(&m_name, &var, &it->second));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void InMemoryPerProcess::resolveRegularExpression(const std::string& var,
|
||||
std::vector<const VariableValue *> *l, variables::KeyExclusions &ke) {
|
||||
std::vector<std::shared_ptr<const VariableValue>> *l, variables::KeyExclusions &ke) {
|
||||
|
||||
//if (var.find(":") == std::string::npos) {
|
||||
// return;
|
||||
@@ -155,7 +153,7 @@ void InMemoryPerProcess::resolveRegularExpression(const std::string& var,
|
||||
if (ke.toOmit(x.first)) {
|
||||
continue;
|
||||
}
|
||||
l->insert(l->begin(), new VariableValue(&m_name, &x.first, &x.second));
|
||||
l->insert(l->begin(), std::make_shared<VariableValue>(&m_name, &x.first, &x.second));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -87,12 +87,12 @@ class InMemoryPerProcess :
|
||||
std::unique_ptr<std::string> resolveFirst(const std::string& var) override;
|
||||
|
||||
void resolveSingleMatch(const std::string& var,
|
||||
std::vector<const VariableValue *> *l) override;
|
||||
std::vector<std::shared_ptr<const VariableValue>> *l) override;
|
||||
void resolveMultiMatches(const std::string& var,
|
||||
std::vector<const VariableValue *> *l,
|
||||
std::vector<std::shared_ptr<const VariableValue>> *l,
|
||||
variables::KeyExclusions &ke) override;
|
||||
void resolveRegularExpression(const std::string& var,
|
||||
std::vector<const VariableValue *> *l,
|
||||
std::vector<std::shared_ptr<const VariableValue>> *l,
|
||||
variables::KeyExclusions &ke) override;
|
||||
|
||||
private:
|
||||
|
||||
@@ -262,7 +262,7 @@ end_txn:
|
||||
|
||||
|
||||
void LMDB::resolveSingleMatch(const std::string& var,
|
||||
std::vector<const VariableValue *> *l) {
|
||||
std::vector<std::shared_ptr<const VariableValue>> *l) {
|
||||
int rc;
|
||||
MDB_txn *txn;
|
||||
MDB_dbi dbi;
|
||||
@@ -287,11 +287,10 @@ void LMDB::resolveSingleMatch(const std::string& var,
|
||||
mdb_cursor_open(txn, dbi, &cursor);
|
||||
while ((rc = mdb_cursor_get(cursor, &mdb_key,
|
||||
&mdb_value_ret, MDB_NEXT_DUP)) == 0) {
|
||||
std::string *a = new std::string(
|
||||
std::string a(
|
||||
reinterpret_cast<char *>(mdb_value_ret.mv_data),
|
||||
mdb_value_ret.mv_size);
|
||||
VariableValue *v = new VariableValue(&var, a);
|
||||
l->push_back(v);
|
||||
l->emplace_back(&var, &a);
|
||||
}
|
||||
|
||||
mdb_cursor_close(cursor);
|
||||
@@ -466,7 +465,7 @@ end_txn:
|
||||
|
||||
|
||||
void LMDB::resolveMultiMatches(const std::string& var,
|
||||
std::vector<const VariableValue *> *l,
|
||||
std::vector<std::shared_ptr<const VariableValue>> *l,
|
||||
variables::KeyExclusions &ke) {
|
||||
MDB_val key, data;
|
||||
MDB_txn *txn = NULL;
|
||||
@@ -496,7 +495,7 @@ void LMDB::resolveMultiMatches(const std::string& var,
|
||||
|
||||
if (keySize == 0) {
|
||||
while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
|
||||
l->insert(l->begin(), new VariableValue(
|
||||
l->insert(l->begin(), std::make_shared<VariableValue>(
|
||||
&m_name,
|
||||
new std::string(reinterpret_cast<char *>(key.mv_data),
|
||||
key.mv_size),
|
||||
@@ -507,7 +506,7 @@ void LMDB::resolveMultiMatches(const std::string& var,
|
||||
while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
|
||||
char *a = reinterpret_cast<char *>(key.mv_data);
|
||||
if (strncmp(var.c_str(), a, keySize) == 0) {
|
||||
l->insert(l->begin(), new VariableValue(
|
||||
l->insert(l->begin(), std::make_shared<VariableValue>(
|
||||
&m_name,
|
||||
new std::string(reinterpret_cast<char *>(key.mv_data),
|
||||
key.mv_size),
|
||||
@@ -528,7 +527,7 @@ end_txn:
|
||||
|
||||
|
||||
void LMDB::resolveRegularExpression(const std::string& var,
|
||||
std::vector<const VariableValue *> *l,
|
||||
std::vector<std::shared_ptr<const VariableValue>> *l,
|
||||
variables::KeyExclusions &ke) {
|
||||
MDB_val key, data;
|
||||
MDB_txn *txn = NULL;
|
||||
|
||||
@@ -66,12 +66,12 @@ class LMDB :
|
||||
std::unique_ptr<std::string> resolveFirst(const std::string& var) override;
|
||||
|
||||
void resolveSingleMatch(const std::string& var,
|
||||
std::vector<const VariableValue *> *l) override;
|
||||
std::vector<std::shared_ptr<const VariableValue>> *l) override;
|
||||
void resolveMultiMatches(const std::string& var,
|
||||
std::vector<const VariableValue *> *l,
|
||||
std::vector<std::shared_ptr<const VariableValue>> *l,
|
||||
variables::KeyExclusions &ke) override;
|
||||
void resolveRegularExpression(const std::string& var,
|
||||
std::vector<const VariableValue *> *l,
|
||||
std::vector<std::shared_ptr<const VariableValue>> *l,
|
||||
variables::KeyExclusions &ke) override;
|
||||
|
||||
private:
|
||||
|
||||
Reference in New Issue
Block a user