Skip to content

Commit 8d825f0

Browse files
committed
Optimization
1 parent 87e1ff2 commit 8d825f0

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

include/boost/redis/impl/log_utils.hpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <cstddef>
1616
#include <string>
1717
#include <string_view>
18+
#include <type_traits>
1819

1920
namespace boost::redis::detail {
2021

@@ -47,25 +48,31 @@ struct log_traits<system::error_code> {
4748
}
4849
};
4950

51+
template <class... Args>
52+
void format_log_args(std::string& to, const Args&... args)
53+
{
54+
auto dummy = {(log_traits<Args>::log(to, args), 0)...};
55+
ignore_unused(dummy);
56+
}
57+
5058
// Logs a message with the specified severity to the logger.
5159
// Formatting won't be performed if the logger's level is inferior to lvl.
5260
// args are stringized using log_traits, and concatenated.
53-
template <class... Args>
54-
void log(buffered_logger& to, logger::level lvl, const Args&... args)
61+
template <class Arg0, class... Rest>
62+
void log(buffered_logger& to, logger::level lvl, const Arg0& arg0, const Rest&... arg_rest)
5563
{
5664
// Severity check
5765
if (to.lgr.lvl < lvl)
5866
return;
5967

60-
// Clear the buffer
61-
to.buffer.clear();
62-
63-
// Format all arguments
64-
auto dummy = {(log_traits<Args>::log(to.buffer, args), 0)...};
65-
ignore_unused(dummy);
66-
67-
// Invoke the function
68-
to.lgr.fn(lvl, to.buffer);
68+
// Optimization: if we get passed a single string, don't copy it to the buffer
69+
if constexpr (sizeof...(Rest) == 0u && std::is_convertible_v<Arg0, std::string_view>) {
70+
to.lgr.fn(lvl, arg0);
71+
} else {
72+
to.buffer.clear();
73+
format_log_args(to.buffer, arg0, arg_rest...);
74+
to.lgr.fn(lvl, to.buffer);
75+
}
6976
}
7077

7178
// Shorthand for each log level we use

0 commit comments

Comments
 (0)