https://github.com/quasilyte/go-n2o
Go external optimizer.
https://github.com/quasilyte/go-n2o
go golang optimizer source-code
Last synced: 5 months ago
JSON representation
Go external optimizer.
- Host: GitHub
- URL: https://github.com/quasilyte/go-n2o
- Owner: quasilyte
- License: mit
- Created: 2018-07-27T21:38:25.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-08-13T11:52:01.000Z (almost 8 years ago)
- Last Synced: 2025-04-07T19:51:20.729Z (about 1 year ago)
- Topics: go, golang, optimizer, source-code
- Language: Go
- Homepage:
- Size: 7.81 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# n2o: nitrous boost for Go
## Quick overview
You start by writing ordinary Go code.
Then you spot some execution path that requires optimizations, the hot one.
```go
func array8sum(xs *[8]int) int {
total := 0
for _, x := range xs {
total += x
}
return total
}
```
Suppose you think that loop unrolling might help.
Instead of doing this:
```go
func array8sum(xs *[8]int) int {
total := 0
total += xs[0]
total += xs[1]
total += xs[2]
total += xs[3]
total += xs[4]
total += xs[5]
total += xs[6]
total += xs[7]
return total
}
```
You can do this:
```go
func array8sum(xs *[8]int) int {
total := 0
//n2o: unroll
for _, x := range xs {
total += x
}
return total
}
```
And you get best of two worlds:
1. Readability of the initial version.
2. Performance of the optimized (unrolled) code.
There are other optimizations that can be performed by the `n2o`.
For example, you may ask to apply all sensible optimizations to the function:
```go
// Can also use func/size to optimize for code size.
//n2o: func/speed
func array8sum(xs *[8]int) int {
total := 0
for _, x := range xs {
total += x
}
return total
}
```
Read docs to know more about supported directives and their meaning.
Basically, you write Go and then improve performance by optimizer hints, instead of doing dirty work by yourself.
## Optimizations
Please note that some optimizations may be dangerous when used inappropriately.
For example, one should not use `deadcode` when unsure that such code path is
never executed in the production environment.
This is as dangerous as turning off asserts in C and alike, caveat emptor.
For your convenience, such unsafe optimizations are listed in a separate list.
### Statement/expression local
Some optimizations are applied to the particular statement or expression:
**Safe optimizations**:
* `inline` - force function inlining at the specified call site.
* `unroll` - unroll a whole loop or make it execute several steps at each iteration.
**Unsafe optimizations**:
* `deadcode` - replace expression or statement that is believed to be "dead" during execution.
### Function attributes
On the function level, there are various optimization attributes that affect
whole function body.
```go
//n2o: inline
// Force function inlining at the every call site, even if `gc` compiler
// does not consider function as inlineable candidate.
// If possible, n2o may transform function body to make function
// inlineable by `gc` itself, otherwise it does inlining on its own.
func example() {}
```
There are also `speed` and `size` optional attributes that can't be used together:
```go
//n2o: size
// Try to make function more compact by applying various optimizations to its body.
func optimizedForSize() {}
//n2o: speed
// Try to make function run faster by applying various optimizations to its body.
func optimizedForExecutionTime() {}
```