Skip to content

Commit 0b5493d

Browse files
committed
Minor performance improvements setting up intervention's log
- Initialize `log` temporary value on construction instead of doing default initialization and then calling `append`. - Leverage `std::string_view` to replace `const std::string&` parameters in `utils::string::replaceAll` to avoid creating a `std::string` object (and associated allocation and copy) for the string literal`%d`
1 parent c947f5e commit 0b5493d

File tree

3 files changed

+8
-9
lines changed

3 files changed

+8
-9
lines changed

src/transaction.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,9 +1492,8 @@ bool Transaction::intervention(ModSecurityIntervention *it) {
14921492
it->status = m_it.status;
14931493

14941494
if (m_it.log != NULL) {
1495-
std::string log("");
1496-
log.append(m_it.log);
1497-
utils::string::replaceAll(&log, std::string("%d"),
1495+
std::string log(m_it.log);
1496+
utils::string::replaceAll(log, "%d",
14981497
std::to_string(it->status));
14991498
it->log = strdup(log.c_str());
15001499
} else {

src/utils/string.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,11 @@ unsigned char *c2x(unsigned what, unsigned char *where) {
254254
}
255255

256256

257-
void replaceAll(std::string *str, const std::string& from,
258-
const std::string& to) {
257+
void replaceAll(std::string &str, std::string_view from,
258+
std::string_view to) {
259259
size_t start_pos = 0;
260-
while ((start_pos = str->find(from, start_pos)) != std::string::npos) {
261-
str->replace(start_pos, from.length(), to);
260+
while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
261+
str.replace(start_pos, from.length(), to);
262262
start_pos += to.length();
263263
}
264264
}

src/utils/string.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ std::vector<std::string> ssplit(std::string str, char delimiter);
6868
std::pair<std::string, std::string> ssplit_pair(const std::string& str, char delimiter);
6969
std::vector<std::string> split(std::string str, char delimiter);
7070
void chomp(std::string *str);
71-
void replaceAll(std::string *str, const std::string& from,
72-
const std::string& to);
71+
void replaceAll(std::string &str, std::string_view from,
72+
std::string_view to);
7373
std::string removeWhiteSpacesIfNeeded(std::string a);
7474
std::string parserSanitizer(std::string a);
7575

0 commit comments

Comments
 (0)