authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-06-19 17:21:08-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-06-19 17:21:08-04:00
logee525c92a4c3bafa8f30c46e0303e0bca8f81860
tree40cd9be09516360ebcee0c302f0c8366ca4f38a3
parentc7804277bf390eeba368e3565b2aff0cf96f86b0

langref: organize docs for inline loops and add note about when to use it


1 files changed, 42 insertions(+), 15 deletions(-)

doc/langref.html.in+42-15
......@@ -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#}
2367const assert = @import("std").debug.assert;
23582368
23592369test "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#}
2475const assert = @import("std").debug.assert;
24492476
24502477test "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>