Skip to content

Commit ee525c9

Browse files
committedJun 19, 2018
langref: organize docs for inline loops and add note about when to use it
·
0.15.20.3.0
1 parent c780427 commit ee525c9

File tree

1 file changed

+42
-15
lines changed

1 file changed

+42
-15
lines changed
 

‎doc/langref.html.in‎

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2355,11 +2355,18 @@ fn eventuallyErrorSequence() error!u32 {
23552355
break :blk numbers_left;
23562356
};
23572357
}
2358+
{#code_end#}
2359+
2360+
{#header_open|inline while#}
2361+
<p>
2362+
While loops can be inlined. This causes the loop to be unrolled, which
2363+
allows the code to do some things which only work at compile time,
2364+
such as use types as first class values.
2365+
</p>
2366+
{#code_begin|test#}
2367+
const assert = @import("std").debug.assert;
23582368

23592369
test "inline while loop" {
2360-
// While loops can be inlined. This causes the loop to be unrolled, which
2361-
// allows the code to do some things which only work at compile time,
2362-
// such as use types as first class values.
23632370
comptime var i = 0;
23642371
var sum: usize = 0;
23652372
inline while (i < 3) : (i += 1) {
@@ -2378,6 +2385,16 @@ fn typeNameLength(comptime T: type) usize {
23782385
return @typeName(T).len;
23792386
}
23802387
{#code_end#}
2388+
<p>
2389+
It is recommended to use <code>inline</code> loops only for one of these reasons:
2390+
</p>
2391+
<ul>
2392+
<li>You need the loop to execute at {#link|comptime#} for the semantics to work.</li>
2393+
<li>
2394+
You have a benchmark to prove that forcibly unrolling the loop in this way is measurably faster.
2395+
</li>
2396+
</ul>
2397+
{#header_close#}
23812398
{#see_also|if|Optionals|Errors|comptime|unreachable#}
23822399
{#header_close#}
23832400
{#header_open|for#}
@@ -2445,15 +2462,20 @@ test "for else" {
24452462
break :blk sum;
24462463
};
24472464
}
2448-
2465+
{#code_end#}
2466+
{#header_open|inline for#}
2467+
<p>
2468+
For loops can be inlined. This causes the loop to be unrolled, which
2469+
allows the code to do some things which only work at compile time,
2470+
such as use types as first class values.
2471+
The capture value and iterator value of inlined for loops are
2472+
compile-time known.
2473+
</p>
2474+
{#code_begin|test#}
2475+
const assert = @import("std").debug.assert;
24492476

24502477
test "inline for loop" {
24512478
const nums = []i32{2, 4, 6};
2452-
// For loops can be inlined. This causes the loop to be unrolled, which
2453-
// allows the code to do some things which only work at compile time,
2454-
// such as use types as first class values.
2455-
// The capture value and iterator value of inlined for loops are
2456-
// compile-time known.
24572479
var sum: usize = 0;
24582480
inline for (nums) |i| {
24592481
const T = switch (i) {
@@ -2471,6 +2493,16 @@ fn typeNameLength(comptime T: type) usize {
24712493
return @typeName(T).len;
24722494
}
24732495
{#code_end#}
2496+
<p>
2497+
It is recommended to use <code>inline</code> loops only for one of these reasons:
2498+
</p>
2499+
<ul>
2500+
<li>You need the loop to execute at {#link|comptime#} for the semantics to work.</li>
2501+
<li>
2502+
You have a benchmark to prove that forcibly unrolling the loop in this way is measurably faster.
2503+
</li>
2504+
</ul>
2505+
{#header_close#}
24742506
{#see_also|while|comptime|Arrays|Slices#}
24752507
{#header_close#}
24762508
{#header_open|if#}
@@ -4222,13 +4254,8 @@ pub fn main() void {
42224254
task in userland. It does so without introducing another language on top of Zig, such as
42234255
a macro language or a preprocessor language. It's Zig all the way down.
42244256
</p>
4225-
<p>TODO: suggestion to not use inline unless necessary</p>
4226-
{#header_close#}
42274257
{#header_close#}
4228-
{#header_open|inline#}
4229-
<p>TODO: inline while</p>
4230-
<p>TODO: inline for</p>
4231-
<p>TODO: suggestion to not use inline unless necessary</p>
4258+
{#see_also|inline while|inline for#}
42324259
{#header_close#}
42334260
{#header_open|Assembly#}
42344261
<p>TODO: example of inline assembly</p>

0 commit comments

Comments
 (0)
Please sign in to comment.