Skip to content

Commit 549a466

Browse files
mluggandrewrk
authored andcommitted
std.Io.Reader: encourage inlining hot buffer check
Resolves: #24424
1 parent 5b4e982 commit 549a466

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

lib/std/Io/Reader.zig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,18 @@ pub fn fill(r: *Reader, n: usize) Error!void {
10001000
@branchHint(.likely);
10011001
return;
10021002
}
1003+
return fillUnbuffered(r, n);
1004+
}
1005+
1006+
/// This internal function is separated from `fill` to encourage optimizers to inline `fill`, hence
1007+
/// propagating its `@branchHint` to usage sites. If these functions are combined, `fill` is large
1008+
/// enough that LLVM is reluctant to inline it, forcing usages of APIs like `takeInt` to go through
1009+
/// an expensive runtime function call just to figure out that the data is, in fact, already in the
1010+
/// buffer.
1011+
///
1012+
/// Missing this optimization can result in wall-clock time for the most affected benchmarks
1013+
/// increasing by a factor of 5 or more.
1014+
fn fillUnbuffered(r: *Reader, n: usize) Error!void {
10031015
if (r.seek + n <= r.buffer.len) while (true) {
10041016
const end_cap = r.buffer[r.end..];
10051017
var writer: Writer = .fixed(end_cap);

0 commit comments

Comments
 (0)