From 7d7752ed411222276ccdbf6b45b21edddbc05caa Mon Sep 17 00:00:00 2001 From: Nico Elbers Date: Sun, 7 Dec 2025 00:26:03 +0100 Subject: [PATCH] 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. --- lib/std/Io/Writer.zig | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/std/Io/Writer.zig b/lib/std/Io/Writer.zig index 3f6fde21afd53b820714feb5982f2e81c054c7c2..e4eb52d294ec7972b2059ada1fb9e15f313b3f6a 100644 --- 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; -- 2.54.0