commit 7d7752ed411222276ccdbf6b45b21edddbc05caa (tree)
parent ef6341ee8df9e0d9b932815366c627186b536d7c
Author: Nico Elbers <nico.b.elbers@gmail.com>
Date: Sun, 7 Dec 2025 00:26:03 +0100
Writer.Allocating.sendFile: avoid useless syscall
Previously, the `readSliceShort` call would call `readVec` twice, as
there was still space left in the buffer after the first `readVec` call
from `Allocating.Writer` overallocating, even if we know the exact size
of the file.
Diffstat:
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/lib/std/Io/Writer.zig b/lib/std/Io/Writer.zig
@@ -2772,10 +2772,14 @@ pub const Allocating = struct {
if (limit == .nothing) return 0;
const a: *Allocating = @fieldParentPtr("writer", w);
const pos = file_reader.logicalPos();
- const additional = if (file_reader.getSize()) |size| size - pos else |_| std.atomic.cache_line;
+ const additional, const exact = if (file_reader.getSize()) |size|
+ .{ size - pos, true }
+ else |_|
+ .{ std.atomic.cache_line, false };
if (additional == 0) return error.EndOfStream;
a.ensureUnusedCapacity(limit.minInt64(additional)) catch return error.WriteFailed;
- const dest = limit.slice(a.writer.buffer[a.writer.end..]);
+ const buffer = a.writer.buffer[a.writer.end..];
+ const dest = if (exact) buffer[0..limit.minInt64(additional)] else limit.slice(buffer);
const n = try file_reader.interface.readSliceShort(dest);
if (n == 0) return error.EndOfStream;
a.writer.end += n;