| ... | ... | @@ -107,6 +107,13 @@ pub fn BufferedInStreamCustom(comptime buffer_size: usize, comptime Error: type) |
| 107 | 107 | fn readFn(in_stream: *Stream, dest: []u8) !usize { |
| 108 | 108 | const self = @fieldParentPtr(Self, "stream", in_stream); |
| 109 | 109 | |
| 110 | // Hot path for one byte reads |
| 111 | if (dest.len == 1 and self.end_index > self.start_index) { |
| 112 | dest[0] = self.buffer[self.start_index]; |
| 113 | self.start_index += 1; |
| 114 | return 1; |
| 115 | } |
| 116 | |
| 110 | 117 | var dest_index: usize = 0; |
| 111 | 118 | while (true) { |
| 112 | 119 | const dest_space = dest.len - dest_index; |
| ... | ... | @@ -126,6 +133,13 @@ pub fn BufferedInStreamCustom(comptime buffer_size: usize, comptime Error: type) |
| 126 | 133 | if (dest_space < buffer_size) { |
| 127 | 134 | self.start_index = 0; |
| 128 | 135 | self.end_index = try self.unbuffered_in_stream.read(self.buffer[0..]); |
| 136 | |
| 137 | // Shortcut |
| 138 | if (self.end_index >= dest_space) { |
| 139 | mem.copy(u8, dest[dest_index..], self.buffer[0..dest_space]); |
| 140 | self.start_index = dest_space; |
| 141 | return dest.len; |
| 142 | } |
| 129 | 143 | } else { |
| 130 | 144 | // asking for so much data that buffering is actually less efficient. |
| 131 | 145 | // forward the request directly to the unbuffered stream |