From a22d5dc9f491191322978e61edf307e172d33709 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 8 May 2025 12:20:48 +0930 Subject: [PATCH] lightningd: use static buffer for common log path. This speeds logging slightly, but most of the time is actually spent in fflush() (which we need). Result (10 runs, eatmydata): FAILED tests/test_connection.py::test_bench - assert 24.086638-25.347475(24.6901+/-0.4) == 0 This is an 8% performance improvement (over the entire series), which is not bad. Signed-off-by: Rusty Russell --- lightningd/log.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/lightningd/log.c b/lightningd/log.c index 8828f6996..9775e79f4 100644 --- a/lightningd/log.c +++ b/lightningd/log.c @@ -208,6 +208,13 @@ static void log_to_files(const char *log_prefix, { char tstamp[sizeof("YYYY-mm-ddTHH:MM:SS.nnnZ ")]; char *entry, nodestr[hex_str_size(PUBKEY_CMPR_LEN)]; + char buf[sizeof("%s%s%s %s-%s: %s\n") + + strlen(log_prefix) + + sizeof(tstamp) + + strlen(level_prefix(level)) + + sizeof(nodestr) + + strlen(entry_prefix) + + strlen(str)]; bool filtered; if (print_timestamps) { @@ -235,14 +242,18 @@ static void log_to_files(const char *log_prefix, entry_prefix, str, dir, hex); tal_free(hex); } else { + size_t len; + entry = buf; if (!node_id) - entry = tal_fmt(tmpctx, "%s%s%s %s: %s\n", - log_prefix, tstamp, level_prefix(level), entry_prefix, str); + len = snprintf(buf, sizeof(buf), + "%s%s%s %s: %s\n", + log_prefix, tstamp, level_prefix(level), entry_prefix, str); else - entry = tal_fmt(tmpctx, "%s%s%s %s-%s: %s\n", - log_prefix, tstamp, level_prefix(level), - nodestr, - entry_prefix, str); + len = snprintf(buf, sizeof(buf), "%s%s%s %s-%s: %s\n", + log_prefix, tstamp, level_prefix(level), + nodestr, + entry_prefix, str); + assert(len < sizeof(buf)); } /* In complex configurations, we tell loggers to overshare: then we