zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 6fb23542fe8503ba5c97bde950b1ebbe8f07f951 (tree)
parent 731ff120d0ee68f3ce2eb30648eeaf96a283142d
Author: Prokop Randacek <prokop@rdck.dev>
Date:   Fri,  2 Feb 2024 07:51:30 +0100

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.

Diffstat:
Mlib/std/log.zig | 8+++++++-
1 file changed, 7 insertions(+), 1 deletion(-)

diff --git 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