From 6fb23542fe8503ba5c97bde950b1ebbe8f07f951 Mon Sep 17 00:00:00 2001 From: Prokop Randacek Date: Fri, 2 Feb 2024 07:51:30 +0100 Subject: [PATCH] Buffer the logging function The default logging function used to have no buffer. So a single log statement could result in many individual write syscalls each writing only a couple of bytes. After this change the logging function now has a 4kb buffer. Only log statements longer than 4kb now do multiple write syscalls. 4kb is the default bufferedWriter size and was choosen arbitrarily. The downside of this is that the log function now allocates 4kb more stack space but I think that is an acceptable trade-off. --- lib/std/log.zig | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/std/log.zig b/lib/std/log.zig index 5c4650cc1c..0562d09c51 100644 --- a/lib/std/log.zig +++ b/lib/std/log.zig @@ -149,9 +149,15 @@ pub fn defaultLog( const level_txt = comptime message_level.asText(); const prefix2 = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): "; const stderr = std.io.getStdErr().writer(); + var bw = std.io.bufferedWriter(stderr); + const writer = bw.writer(); + std.debug.getStderrMutex().lock(); defer std.debug.getStderrMutex().unlock(); - nosuspend stderr.print(level_txt ++ prefix2 ++ format ++ "\n", args) catch return; + nosuspend { + writer.print(level_txt ++ prefix2 ++ format ++ "\n", args) catch return; + bw.flush() catch return; + } } /// Returns a scoped logging namespace that logs all messages using the scope