Make all std::regex instances static
Per [1] we really shouldn't be using regex. In the cases we do, it's a
HUUUUUGE performance benefit to be compiling the regex ONCE.
The only downside is a slight increase in memory usage.
[1]: https://github.com/openbmc/bmcweb/issues/176
Signed-off-by: Ed Tanous <edtanous@google.com>
Change-Id: I8644b8a07810349fb60bfa0258a13e815912a38e
diff --git a/include/dbus_monitor.hpp b/include/dbus_monitor.hpp
index 46ef25e..4ecc436 100644
--- a/include/dbus_monitor.hpp
+++ b/include/dbus_monitor.hpp
@@ -173,8 +173,8 @@
// These regexes derived on the rules here:
// https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names
- std::regex validPath("^/([A-Za-z0-9_]+/?)*$");
- std::regex validInterface(
+ static std::regex validPath("^/([A-Za-z0-9_]+/?)*$");
+ static std::regex validInterface(
"^[A-Za-z_][A-Za-z0-9_]*(\\.[A-Za-z_][A-Za-z0-9_]*)+$");
for (const auto& thisPath : *paths)
diff --git a/include/dbus_utility.hpp b/include/dbus_utility.hpp
index c863c30..f8e53f4 100644
--- a/include/dbus_utility.hpp
+++ b/include/dbus_utility.hpp
@@ -103,7 +103,7 @@
inline void escapePathForDbus(std::string& path)
{
- const std::regex reg("[^A-Za-z0-9_/]");
+ const static std::regex reg("[^A-Za-z0-9_/]");
std::regex_replace(path.begin(), path.begin(), path.end(), reg, "_");
}
diff --git a/include/openbmc_dbus_rest.hpp b/include/openbmc_dbus_rest.hpp
index 5125764..0fbef04 100644
--- a/include/openbmc_dbus_rest.hpp
+++ b/include/openbmc_dbus_rest.hpp
@@ -101,7 +101,7 @@
inline bool validateFilename(const std::string& filename)
{
- std::regex validFilename(R"(^[\w\- ]+(\.?[\w\- ]*)$)");
+ static std::regex validFilename(R"(^[\w\- ]+(\.?[\w\- ]*)$)");
return std::regex_match(filename, validFilename);
}
@@ -2562,7 +2562,7 @@
// Filename should be in alphanumeric, dot and underscore
// Its based on phosphor-debug-collector application
// dumpfile format
- std::regex dumpFileRegex("[a-zA-Z0-9\\._]+");
+ static std::regex dumpFileRegex("[a-zA-Z0-9\\._]+");
if (!std::regex_match(dumpFileName, dumpFileRegex))
{
BMCWEB_LOG_ERROR << "Invalid dump filename " << dumpFileName;