unroller
unroller is a tool for loop unrolling in Go. It generates unrolled versions of functions by adding comments to the top-level for loops.
Loop unrolling is an optimization technique where the compiler expands the iterations of a loop, reducing the overhead of iteration. This can improve the execution speed of the code.
Installation
To install unroller, you need to have Go installed and set up. Then, you can use the following go install
command:
go install github.com/orisano/unroller@latest
Usage
unroller is intended to be used with go generate
. To generate an unrolled version of a file, add the comment //go:generate unroller
to the file. For each function that you want to unroll, add the comment // UNROLL
immediately before the corresponding for loop. When you run go generate
on that file, unroller will generate a new file with the name {filename}_unrolled.go
. You can use the -o
option to specify a different output file name.
Example usage in a file:
//go:generate unroller -o unrolled_file.go
package main
import "fmt"
func foo() {
fmt.Println("start")
// UNROLL
for i := 0; i < 5; i++ {
fmt.Println(i)
}
fmt.Println("end")
}
To generate the unrolled version, run the following command:
go generate path/to/your/file.go
The generated unrolled file will have the following content:
// Code generated by unroller. DO NOT EDIT.
package main
import "fmt"
func fooUnrolled() {
fmt.Println("start")
{
const i = 0
fmt.Println(i)
}
{
const i = 1
fmt.Println(i)
}
{
const i = 2
fmt.Println(i)
}
{
const i = 3
fmt.Println(i)
}
{
const i = 4
fmt.Println(i)
}
fmt.Println("end")
}
You can then use the generated unrolled file in your codebase.
It's important to note that the // UNROLL
comment should be placed immediately before the for loop that you want to unroll. Also, remember to include the //go:generate unroller
comment at the top of the file to ensure that the unrolling process is triggered when running go generate.
Limitations
- unroller currently supports only top-level for loops. Nested loops are not supported.
Contributing
Contributions to unroller are welcome! If you find a bug or have a suggestion, please open an issue on the GitHub repository.
License
unroller is licensed under the MIT License. See the LICENSE file for more information.