lg2: reduce string_view for size optimize
There were a few places where string_view's were used for simplicity,
but as a result the compiler ends up instantiating temporary
string_view objects, which makes the generated code larger. Switch
to 'const char*' instead and avoid needless string_view constructions.
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: Ia29470e8db0f1489bf7b5f6a18b1af0877d0ce72
diff --git a/lib/include/phosphor-logging/lg2.hpp b/lib/include/phosphor-logging/lg2.hpp
index 6865a31..e1ac0ab 100644
--- a/lib/include/phosphor-logging/lg2.hpp
+++ b/lib/include/phosphor-logging/lg2.hpp
@@ -10,7 +10,6 @@
#include <phosphor-logging/lg2/header.hpp>
#include <phosphor-logging/lg2/level.hpp>
#include <source_location>
-#include <string_view>
namespace lg2
{
@@ -24,7 +23,7 @@
* @param[in] msg - The message to log.
* @param[in] ts - The rest of the arguments.
*/
- explicit log(const std::source_location& s, const std::string_view& msg,
+ explicit log(const std::source_location& s, const char* msg,
details::header_str_conversion_t<Ts&&>... ts)
{
details::log_conversion::start(
@@ -39,8 +38,7 @@
* @param[in] s - The derived source_location.
*/
explicit log(
- const std::string_view& msg,
- details::header_str_conversion_t<Ts&&>... ts,
+ const char* msg, details::header_str_conversion_t<Ts&&>... ts,
const std::source_location& s = std::source_location::current()) :
log(s, msg, std::forward<details::header_str_conversion_t<Ts&&>>(ts)...)
{
@@ -53,10 +51,10 @@
// Deducation guides to help the compiler out...
template <level S = level::debug, typename... Ts>
-explicit log(const std::string_view&, Ts&&...) -> log<S, Ts...>;
+explicit log(const char*, Ts&&...) -> log<S, Ts...>;
template <level S = level::debug, typename... Ts>
-explicit log(const std::source_location&, const std::string_view&, Ts&&...)
+explicit log(const std::source_location&, const char*, Ts&&...)
-> log<S, Ts...>;
/** Macro to define aliases for lg2::level(...) -> lg2::log<level>(...)
@@ -71,11 +69,10 @@
}; \
\
template <typename... Ts> \
- explicit levelval(const std::string_view&, Ts&&...) -> levelval<Ts...>; \
+ explicit levelval(const char*, Ts&&...) -> levelval<Ts...>; \
\
template <typename... Ts> \
- explicit levelval(const std::source_location&, const std::string_view&, \
- Ts&&...) \
+ explicit levelval(const std::source_location&, const char*, Ts&&...) \
->levelval<Ts...>
// Enumerate the aliases for each log level.
diff --git a/lib/include/phosphor-logging/lg2/conversion.hpp b/lib/include/phosphor-logging/lg2/conversion.hpp
index 6b51e19..fe0d7a1 100644
--- a/lib/include/phosphor-logging/lg2/conversion.hpp
+++ b/lib/include/phosphor-logging/lg2/conversion.hpp
@@ -179,20 +179,23 @@
prohibit(f, signed_val);
prohibit(f, unsigned_val);
+ // Utiilty to handle conversion to a 'const char*' depending on V:
+ // - 'const char*' and similar use static cast.
+ // - 'std::string' and 'std::string_view' use data() function.
+ auto str_data = [](V&& v) {
+ if constexpr (std::is_same_v<const char*, std::decay_t<V>> ||
+ std::is_same_v<char*, std::decay_t<V>>)
+ {
+ return static_cast<const char*>(v);
+ }
+ else
+ {
+ return v.data();
+ }
+ };
+
// Add 'str' flag, force to 'const char*' for variadic passing.
- //
- // It may appear unsafe that we are using a temporary string_view object
- // for conversion, but it is a safe and consise way to do this conversion.
- // - (const char*) goes through string_view, but data() returns the
- // original pointer.
- // - (string_view) makes a duplicate temporary pointing to the same
- // original data pointer.
- // - (string) uses a temporary string_view to return a pointer to the
- // original std::string v.data().
- // Without this pass-through-string_view, we would need some additional
- // template magic to differentiate between types requiring direct pointer
- // passing vs v.data() to obtain the underlying pointer.
- return std::make_tuple(h, (f | str).value, std::string_view{v}.data());
+ return std::make_tuple(h, (f | str).value, str_data(std::forward<V>(v)));
}
/** Logging conversion for pointer-types. */
@@ -248,8 +251,8 @@
/** Conversion and validation is complete. Pass along to the final
* do_log variadic function. */
template <typename... Ts>
- static void done(level l, const std::source_location& s,
- const std::string_view& m, Ts&&... ts)
+ static void done(level l, const std::source_location& s, const char* m,
+ Ts&&... ts)
{
do_log(l, s, m, sizeof...(Ts) / 3, std::forward<Ts>(ts)...);
}
@@ -360,8 +363,8 @@
/** Start processing a sequence of arguments to `lg2::log` using `step` or
* `done`. */
template <typename... Ts>
- static void start(level l, const std::source_location& s,
- const std::string_view& msg, Ts&&... ts)
+ static void start(level l, const std::source_location& s, const char* msg,
+ Ts&&... ts)
{
// If there are no arguments (ie. just a message), then skip processing
// and call `done` directly.
diff --git a/lib/include/phosphor-logging/lg2/logger.hpp b/lib/include/phosphor-logging/lg2/logger.hpp
index 52ee9ed..6dca12c 100644
--- a/lib/include/phosphor-logging/lg2/logger.hpp
+++ b/lib/include/phosphor-logging/lg2/logger.hpp
@@ -3,7 +3,6 @@
#include <cstddef>
#include <phosphor-logging/lg2/level.hpp>
#include <source_location>
-#include <string_view>
namespace lg2::details
{
@@ -16,10 +15,9 @@
* @param[in] level - The logging level to use.
* @param[in] source_location - The original source location of the upper-level
* log call.
- * @param[in] string_view - The primary message to log.
+ * @param[in] char* - The primary message to log.
* @param[in] size_t - The number of sets in the variadic arguments.
*/
-void do_log(level, const std::source_location&, const std::string_view&, size_t,
- ...);
+void do_log(level, const std::source_location&, const char*, size_t, ...);
} // namespace lg2::details
diff --git a/lib/lg2_logger.cpp b/lib/lg2_logger.cpp
index 96e244b..ac7a75c 100644
--- a/lib/lg2_logger.cpp
+++ b/lib/lg2_logger.cpp
@@ -143,8 +143,8 @@
isatty(fileno(stderr)) ? cerr_extra_output : noop_extra_output;
// Do_log implementation.
-void do_log(level l, const std::source_location& s, const std::string_view& m,
- size_t count, ...)
+void do_log(level l, const std::source_location& s, const char* m, size_t count,
+ ...)
{
using namespace std::string_literals;
@@ -154,7 +154,7 @@
std::string message{m};
// Assign all the static fields.
- strings[pos_fmtmsg] = "LOG2_FMTMSG="s + m.data();
+ strings[pos_fmtmsg] = "LOG2_FMTMSG="s + m;
strings[pos_prio] = "PRIORITY="s + std::to_string(static_cast<uint64_t>(l));
strings[pos_file] = "CODE_FILE="s + s.file_name();
strings[pos_line] = "CODE_LINE="s + std::to_string(s.line());