|
3 | 3 | //! `*in*` readers (text_io, host_stream), via `std.Io.File`. |
4 | 4 |
|
5 | 5 | const std = @import("std"); |
| 6 | +const builtin = @import("builtin"); |
6 | 7 |
|
7 | 8 | /// Read up to `buf.len` bytes from stdin into `buf`. Returns bytes read |
8 | 9 | /// (0 = EOF). |
9 | 10 | pub fn readChunk(io: std.Io, buf: []u8) std.Io.File.ReadStreamingError!usize { |
10 | 11 | return std.Io.File.stdin().readStreaming(io, &.{buf}); |
11 | 12 | } |
| 13 | + |
| 14 | +/// How long a stdin fill waits for input before running an idle collect. |
| 15 | +pub const idle_poll_ms: i32 = 1000; |
| 16 | + |
| 17 | +/// Wait up to `timeout_ms` for stdin to become readable (data, EOF or |
| 18 | +/// hang-up). True when a read will not block. Answers true at once where |
| 19 | +/// there is no `poll`, so callers never spin. |
| 20 | +pub fn waitReadable(timeout_ms: i32) bool { |
| 21 | + if (comptime builtin.os.tag == .wasi or builtin.os.tag == .windows) return true; |
| 22 | + var fds = [_]std.posix.pollfd{.{ |
| 23 | + .fd = std.posix.STDIN_FILENO, |
| 24 | + .events = std.posix.POLL.IN, |
| 25 | + .revents = 0, |
| 26 | + }}; |
| 27 | + const n = std.posix.poll(&fds, timeout_ms) catch return true; |
| 28 | + return n > 0; |
| 29 | +} |
| 30 | + |
| 31 | +/// Ready a demand-filled stdin buffer for its next fill: drop its consumed |
| 32 | +/// prefix, then wait for input, calling `gc.collectBeforeIdle()` each time |
| 33 | +/// `idle_poll_ms` passes with none, so a process blocked on stdin still |
| 34 | +/// collects and returns memory. |
| 35 | +pub fn beforeFill(infra: std.mem.Allocator, data: *std.ArrayList(u8), pos: *usize, gc: anytype) void { |
| 36 | + dropConsumed(infra, data, pos); |
| 37 | + while (!waitReadable(idle_poll_ms)) gc.collectBeforeIdle(); |
| 38 | +} |
| 39 | + |
| 40 | +/// Capacity up to which `dropConsumed` never frees a stdin buffer's excess, so |
| 41 | +/// a steady stream of small lines does not realloc per fill. |
| 42 | +pub const retained_capacity: usize = 64 * 1024; |
| 43 | + |
| 44 | +/// Drop the consumed prefix `data[0..pos]` of a demand-filled stdin buffer |
| 45 | +/// and reset `pos` to 0, so a long-running reader holds only its unread tail. |
| 46 | +/// Frees the excess allocation once it is over 4x the tail and |
| 47 | +/// `retained_capacity`. |
| 48 | +/// Call before appending a fill: the append may move `data` anyway, so no |
| 49 | +/// caller holds a slice or offset across it. |
| 50 | +pub fn dropConsumed(infra: std.mem.Allocator, data: *std.ArrayList(u8), pos: *usize) void { |
| 51 | + const consumed = pos.*; |
| 52 | + if (consumed == 0) return; |
| 53 | + const rest = data.items.len - consumed; |
| 54 | + std.mem.copyForwards(u8, data.items[0..rest], data.items[consumed..]); |
| 55 | + data.shrinkRetainingCapacity(rest); |
| 56 | + pos.* = 0; |
| 57 | + if (data.capacity > retained_capacity and data.capacity > rest * 4) |
| 58 | + data.shrinkAndFree(infra, rest); |
| 59 | +} |
| 60 | + |
| 61 | +test "dropConsumed keeps the unread tail and resets pos" { |
| 62 | + const a = std.testing.allocator; |
| 63 | + var data: std.ArrayList(u8) = .empty; |
| 64 | + defer data.deinit(a); |
| 65 | + try data.appendSlice(a, "line one\nline two"); |
| 66 | + var pos: usize = 9; |
| 67 | + dropConsumed(a, &data, &pos); |
| 68 | + try std.testing.expectEqualStrings("line two", data.items); |
| 69 | + try std.testing.expectEqual(@as(usize, 0), pos); |
| 70 | +} |
| 71 | + |
| 72 | +test "dropConsumed releases a buffer that grew far past its tail" { |
| 73 | + const a = std.testing.allocator; |
| 74 | + var data: std.ArrayList(u8) = .empty; |
| 75 | + defer data.deinit(a); |
| 76 | + try data.appendNTimes(a, 'x', 1024 * 1024); |
| 77 | + var pos: usize = data.items.len - 3; |
| 78 | + dropConsumed(a, &data, &pos); |
| 79 | + try std.testing.expectEqual(@as(usize, 3), data.items.len); |
| 80 | + try std.testing.expect(data.capacity <= retained_capacity); |
| 81 | +} |
| 82 | + |
| 83 | +test "dropConsumed is a no-op at pos 0" { |
| 84 | + const a = std.testing.allocator; |
| 85 | + var data: std.ArrayList(u8) = .empty; |
| 86 | + defer data.deinit(a); |
| 87 | + try data.appendSlice(a, "abc"); |
| 88 | + var pos: usize = 0; |
| 89 | + dropConsumed(a, &data, &pos); |
| 90 | + try std.testing.expectEqualStrings("abc", data.items); |
| 91 | +} |
0 commit comments