From 0bb222b2cbd5fd5b49c6dbb7e20758f7b1ef6f9a Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 4 Jul 2023 12:00:46 -0500 Subject: [PATCH] perf: avoid heap allocations in daemon::printMessage() (#5724) --- daemon/daemon.cc | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/daemon/daemon.cc b/daemon/daemon.cc index 1e3686301..6bf38dbd6 100644 --- a/daemon/daemon.cc +++ b/daemon/daemon.cc @@ -311,14 +311,22 @@ static void printMessage( std::string_view filename, long line) { - auto const out = std::empty(name) ? fmt::format(FMT_STRING("{:s} ({:s}:{:d})"), message, filename, line) : - fmt::format(FMT_STRING("{:s} {:s} ({:s}:{:d})"), name, message, filename, line); + auto out = tr_strbuf{}; + + if (std::empty(name)) + { + fmt::format_to(std::back_inserter(out), "{:s} ({:s}:{:d})", message, filename, line); + } + else + { + fmt::format_to(std::back_inserter(out), "{:s} {:s} ({:s}:{:d})", name, message, filename, line); + } if (file != TR_BAD_SYS_FILE) { auto timestr = std::array{}; tr_logGetTimeStr(std::data(timestr), std::size(timestr)); - tr_sys_file_write_line(file, fmt::format(FMT_STRING("[{:s}] {:s} {:s}"), std::data(timestr), levelName(level), out)); + tr_sys_file_write_line(file, fmt::format("[{:s}] {:s} {:s}", std::data(timestr), levelName(level), std::data(out))); } #ifdef HAVE_SYSLOG