| ... | @@ -107,6 +107,13 @@ pub fn BufferedInStreamCustom(comptime buffer_size: usize, comptime Error: type) | ... | @@ -107,6 +107,13 @@ pub fn BufferedInStreamCustom(comptime buffer_size: usize, comptime Error: type) |
| 107 | fn readFn(in_stream: *Stream, dest: []u8) !usize { | 107 | fn readFn(in_stream: *Stream, dest: []u8) !usize { |
| 108 | const self = @fieldParentPtr(Self, "stream", in_stream); | 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 | var dest_index: usize = 0; | 117 | var dest_index: usize = 0; |
| 111 | while (true) { | 118 | while (true) { |
| 112 | const dest_space = dest.len - dest_index; | 119 | const dest_space = dest.len - dest_index; |
| ... | @@ -126,6 +133,13 @@ pub fn BufferedInStreamCustom(comptime buffer_size: usize, comptime Error: type) | ... | @@ -126,6 +133,13 @@ pub fn BufferedInStreamCustom(comptime buffer_size: usize, comptime Error: type) |
| 126 | if (dest_space < buffer_size) { | 133 | if (dest_space < buffer_size) { |
| 127 | self.start_index = 0; | 134 | self.start_index = 0; |
| 128 | self.end_index = try self.unbuffered_in_stream.read(self.buffer[0..]); | 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 | } else { | 143 | } else { |
| 130 | // asking for so much data that buffering is actually less efficient. | 144 | // asking for so much data that buffering is actually less efficient. |
| 131 | // forward the request directly to the unbuffered stream | 145 | // forward the request directly to the unbuffered stream |