| 1 |
# Crunchy |
| 2 |
|
| 3 |
The crunchy unroller - deterministically unroll constant loops. For number |
| 4 |
"crunching". |
| 5 |
|
| 6 |
The Rust optimizer will unroll constant loops that don't use the loop variable, |
| 7 |
like this: |
| 8 |
|
| 9 |
```rust |
| 10 |
for _ in 0..100 { |
| 11 |
println!("Hello!"); |
| 12 |
} |
| 13 |
``` |
| 14 |
|
| 15 |
However, using the loop variable will cause it to never unroll the loop. This is |
| 16 |
unfortunate because it means that you can't constant-fold the loop variable, and |
| 17 |
if you end up stomping on the registers it will have to do a load for each |
| 18 |
iteration. This crate ensures that your code is unrolled and const-folded. It |
| 19 |
only works on literals, unfortunately, but there's a work-around: |
| 20 |
|
| 21 |
```rust |
| 22 |
debug_assert_eq!(MY_CONSTANT, 100); |
| 23 |
unroll! { |
| 24 |
for i in 0..100 { |
| 25 |
println!("Iteration {}", i); |
| 26 |
} |
| 27 |
} |
| 28 |
``` |
| 29 |
|
| 30 |
This means that your tests will catch if you redefine the constant. |
| 31 |
|
| 32 |
To default maximum number of loops to unroll is `64`, but that can be easily increased using the cargo features: |
| 33 |
|
| 34 |
* `limit_128` |
| 35 |
* `limit_256` |
| 36 |
* `limit_512` |
| 37 |
* `limit_1024` |
| 38 |
* `limit_2048` |