Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/m0t9/arrcopy
Go linter prohibiting for-range over arrays copies
https://github.com/m0t9/arrcopy
go linter optimization performance-tuning
Last synced: 22 days ago
JSON representation
Go linter prohibiting for-range over arrays copies
- Host: GitHub
- URL: https://github.com/m0t9/arrcopy
- Owner: m0t9
- License: mit
- Created: 2025-01-03T16:53:36.000Z (25 days ago)
- Default Branch: master
- Last Pushed: 2025-01-04T19:01:17.000Z (24 days ago)
- Last Synced: 2025-01-04T19:29:40.779Z (24 days ago)
- Topics: go, linter, optimization, performance-tuning
- Language: Go
- Homepage:
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# arrcopy
[![coverage](https://raw.githubusercontent.com/m0t9/arrcopy/badges/.badges/master/coverage.svg)](https://github.com/m0t9/arrcopy/actions/workflows/.testcoverage.yml)
[![build](https://github.com/m0t9/arrcopy/actions/workflows/go.yml/badge.svg)](https://github.com/m0t9/arrcopy/actions/workflows/go.yml)Simple Go linter prohibiting `for-range` iteration over array copy.
## Details
The `range` operator creates a copy of the array before iterating over it.
In case of heavyweight arrays it may lead to significant performance degradation.
One can see explanation with clear benchmarks [here](https://medium.com/@haaawk/i-thought-i-understood-how-iteration-over-an-array-works-but-apparently-not-in-golang-441a7abd6540)## Usage
### Installation
`go install github.com/m0t9/arrcopy/cmd/...@latest`
### Running
Linter is compatible with `go vet`
`go vet -vettool=$(which arrcopy) ./...`
## Optimization example
The inefficiency of array copying can be shown in the benchmark below
```go
func BenchmarkArrayIteration(b *testing.B) {
const Size = 1000000
type Item struct {
s string
subarr [5]int64
sl []int
p *int
}b.Run("over copy", func(b *testing.B) {
for i := 0; i < b.N; i++ {
b.StopTimer()
arr := [Size]Item{}
b.StartTimer()
for idx, item := range arr {
_, _ = idx, item
}
}
})b.Run("over reference", func(b *testing.B) {
for i := 0; i < b.N; i++ {
b.StopTimer()
arr := [Size]Item{}
b.StartTimer()
for idx, item := range &arr { // added & before arr
_, _ = idx, item
}
}
})
}
``````
goos: darwin
goarch: arm64
pkg: github.com/m0t9/arrcopy/cmd/arrcopy
cpu: Apple M1
BenchmarkArrayIteration/over_copy-8 52 21382312 ns/op
BenchmarkArrayIteration/over_reference-8 3085 382052 ns/op
PASS
ok github.com/m0t9/arrcopy/cmd/arrcopy 9.923s
```