authorgravatar for prokop@rdck.devProkop Randáček <prokop@rdck.dev> 2024-02-02 07:51:30+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-09 14:02:57-08:00
log6fb23542fe8503ba5c97bde950b1ebbe8f07f951
tree394f6d549a08d4319c3d78acfb05120b4a114da4
parent731ff120d0ee68f3ce2eb30648eeaf96a283142d

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 files changed, 7 insertions(+), 1 deletions(-)

lib/std/log.zig+7-1
...@@ -149,9 +149,15 @@ pub fn defaultLog(...@@ -149,9 +149,15 @@ pub fn defaultLog(
149 const level_txt = comptime message_level.asText();149 const level_txt = comptime message_level.asText();
150 const prefix2 = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): ";150 const prefix2 = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): ";
151 const stderr = std.io.getStdErr().writer();151 const stderr = std.io.getStdErr().writer();
152 var bw = std.io.bufferedWriter(stderr);
153 const writer = bw.writer();
154
152 std.debug.getStderrMutex().lock();155 std.debug.getStderrMutex().lock();
153 defer std.debug.getStderrMutex().unlock();156 defer std.debug.getStderrMutex().unlock();
154 nosuspend stderr.print(level_txt ++ prefix2 ++ format ++ "\n", args) catch return;157 nosuspend {
158 writer.print(level_txt ++ prefix2 ++ format ++ "\n", args) catch return;
159 bw.flush() catch return;
160 }
155}161}
156162
157/// Returns a scoped logging namespace that logs all messages using the scope163/// Returns a scoped logging namespace that logs all messages using the scope