authorgravatar for nico.b.elbers@gmail.comNico Elbers <nico.b.elbers@gmail.com> 2025-12-07 00:26:03+01:00
committergravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2026-05-31 02:37:50+02:00
log7d7752ed411222276ccdbf6b45b21edddbc05caa
tree242f7caf365c525053a3984e936b40f98b52e0be
parentef6341ee8df9e0d9b932815366c627186b536d7c

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.

1 files changed, 6 insertions(+), 2 deletions(-)

lib/std/Io/Writer.zig+6-2
...@@ -2772,10 +2772,14 @@ pub const Allocating = struct {...@@ -2772,10 +2772,14 @@ pub const Allocating = struct {
2772 if (limit == .nothing) return 0;2772 if (limit == .nothing) return 0;
2773 const a: *Allocating = @fieldParentPtr("writer", w);2773 const a: *Allocating = @fieldParentPtr("writer", w);
2774 const pos = file_reader.logicalPos();2774 const pos = file_reader.logicalPos();
2775 const additional = if (file_reader.getSize()) |size| size - pos else |_| std.atomic.cache_line;2775 const additional, const exact = if (file_reader.getSize()) |size|
2776 .{ size - pos, true }
2777 else |_|
2778 .{ std.atomic.cache_line, false };
2776 if (additional == 0) return error.EndOfStream;2779 if (additional == 0) return error.EndOfStream;
2777 a.ensureUnusedCapacity(limit.minInt64(additional)) catch return error.WriteFailed;2780 a.ensureUnusedCapacity(limit.minInt64(additional)) catch return error.WriteFailed;
2778 const dest = limit.slice(a.writer.buffer[a.writer.end..]);2781 const buffer = a.writer.buffer[a.writer.end..];
2782 const dest = if (exact) buffer[0..limit.minInt64(additional)] else limit.slice(buffer);
2779 const n = try file_reader.interface.readSliceShort(dest);2783 const n = try file_reader.interface.readSliceShort(dest);
2780 if (n == 0) return error.EndOfStream;2784 if (n == 0) return error.EndOfStream;
2781 a.writer.end += n;2785 a.writer.end += n;