Commit 6fb23542fe

Prokop Randacek <prokop@rdck.dev>
2024-02-02 07:51:30
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.
1 parent 731ff12
Changed files (1)
lib
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