Skip to content

Commit 1333652

Browse files
Alex Snastfacebook-github-bot
authored andcommitted
optimize simpleEscapeString with switch statement for better performance
Summary: Replace unordered_map lookup with switch statement in simpleEscapeString function. This eliminates hash table overhead and provides better branch prediction for character escaping operations in MySQL query building. Reviewed By: jkedgar Differential Revision: D79863225 fbshipit-source-id: c2fc8f18bb4b10b3c6fdb914c81b66cb03edb9b3
1 parent 608b318 commit 1333652

File tree

1 file changed

+29
-15
lines changed
  • third-party/squangle/src/squangle/mysql_client

1 file changed

+29
-15
lines changed

third-party/squangle/src/squangle/mysql_client/Query.cpp

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -358,21 +358,35 @@ void noEscapeString(folly::fbstring* dest, const folly::fbstring& value) {
358358
// Simple escaping of default characters
359359
void simpleEscapeString(folly::fbstring* dest, const folly::fbstring& value) {
360360
folly::grow_capacity_by(*dest, value.size());
361-
for (char ch : value) {
362-
static const std::unordered_map<char, folly::fbstring> replacements = {
363-
{'\\', "\\\\"},
364-
{'\'', "\\'"},
365-
{'\"', "\\\""},
366-
{'\0', "\\0"},
367-
{'\b', "\\b"},
368-
{'\n', "\\n"},
369-
{'\r', "\\r"},
370-
{'\t', "\\t"},
371-
};
372-
if (auto it = replacements.find(ch); it != replacements.end()) {
373-
dest->append(it->second);
374-
} else {
375-
dest->push_back(ch);
361+
for (const char ch : value) {
362+
switch (ch) {
363+
case '\\':
364+
dest->append("\\\\");
365+
break;
366+
case '\'':
367+
dest->append("\\'");
368+
break;
369+
case '\"':
370+
dest->append("\\\"");
371+
break;
372+
case '\0':
373+
dest->append("\\0");
374+
break;
375+
case '\b':
376+
dest->append("\\b");
377+
break;
378+
case '\n':
379+
dest->append("\\n");
380+
break;
381+
case '\r':
382+
dest->append("\\r");
383+
break;
384+
case '\t':
385+
dest->append("\\t");
386+
break;
387+
default:
388+
dest->push_back(ch);
389+
break;
376390
}
377391
}
378392
}

0 commit comments

Comments
 (0)