Prealloc은 잠재적으로 사전에 배치 될 수있는 슬라이스 선언을 찾는 GO 정적 분석 도구입니다.
go install github.com/alexkohler/prealloc@latest
다른 GO 정적 분석 도구 (예 : Golint, Go Vet)와 마찬가지로 Prealloc은 가져 오기 경로에서 명명 된 하나 이상의 파일 이름, 디렉토리 또는 패키지로 호출 할 수 있습니다. Prealloc은 또한 ... 와일드 카드를 지원합니다.
prealloc [flags] files/directories/packages
GO는 용량을 미리 성장시켜 재 할당을 피 하려고 하지만 때로는 더 긴 슬라이스에 충분하지 않습니다. 창조 시점에 슬라이스의 크기가 알려진 경우 지정해야합니다.
다음 벤치 마크를 고려하십시오. (이것은 Prealloc_test.go에서 찾을 수 있습니다)
import "testing"
func BenchmarkNoPreallocate ( b * testing. B ) {
existing := make ([] int64 , 10 , 10 )
b . ResetTimer ()
for i := 0 ; i < b . N ; i ++ {
// Don't preallocate our initial slice
var init [] int64
for _ , element := range existing {
init = append ( init , element )
}
}
}
func BenchmarkPreallocate ( b * testing. B ) {
existing := make ([] int64 , 10 , 10 )
b . ResetTimer ()
for i := 0 ; i < b . N ; i ++ {
// Preallocate our initial slice
init := make ([] int64 , 0 , len ( existing ))
for _ , element := range existing {
init = append ( init , element )
}
}
}$ go test -bench=. -benchmem
goos: linux
goarch: amd64
BenchmarkNoPreallocate-4 3000000 510 ns/op 248 B/op 5 allocs/op
BenchmarkPreallocate-4 20000000 111 ns/op 80 B/op 1 allocs/op 보시다시피, Preallocating은 주로 기본 배열을 재 할당해야하기 때문에 성능을 유발할 수 있습니다. 위에서 벤치마킹 된 패턴은 GO에서 일반적으로 일반적입니다. 슬라이스를 선언 한 다음, 어떤 종류의 범위를 작성하거나 그 안에 추가 또는 색인을 작성하는 루프를 작성하십시오. 이 도구의 목적은 BenchmarkNoPreallocate 의 것과 같은 슬라이스/루프 선언을 플래그하는 것입니다.
Go 1.9.2 출처의 몇 가지 예 :
$ prealloc go/src/....
archive/tar/reader_test.go:854 Consider preallocating ss
archive/zip/zip_test.go:201 Consider preallocating all
cmd/api/goapi.go:301 Consider preallocating missing
cmd/api/goapi.go:476 Consider preallocating files
cmd/asm/internal/asm/endtoend_test.go:345 Consider preallocating extra
cmd/cgo/main.go:60 Consider preallocating ks
cmd/cgo/ast.go:149 Consider preallocating pieces
cmd/compile/internal/ssa/flagalloc.go:64 Consider preallocating oldSched
cmd/compile/internal/ssa/regalloc.go:719 Consider preallocating phis
cmd/compile/internal/ssa/regalloc.go:718 Consider preallocating oldSched
cmd/compile/internal/ssa/regalloc.go:1674 Consider preallocating oldSched
cmd/compile/internal/ssa/gen/rulegen.go:145 Consider preallocating ops
cmd/compile/internal/ssa/gen/rulegen.go:145 Consider preallocating ops
cmd/dist/build.go:893 Consider preallocating all
cmd/dist/build.go:1246 Consider preallocating plats
cmd/dist/build.go:1264 Consider preallocating results
cmd/dist/buildgo.go:59 Consider preallocating list
cmd/doc/pkg.go:363 Consider preallocating names
cmd/fix/typecheck.go:219 Consider preallocating b
cmd/go/internal/base/path.go:34 Consider preallocating out
cmd/go/internal/get/get.go:175 Consider preallocating out
cmd/go/internal/load/pkg.go:1894 Consider preallocating dirent
cmd/go/internal/work/build.go:2402 Consider preallocating absOfiles
cmd/go/internal/work/build.go:2731 Consider preallocating absOfiles
cmd/internal/objfile/pe.go:48 Consider preallocating syms
cmd/internal/objfile/pe.go:38 Consider preallocating addrs
cmd/internal/objfile/goobj.go:43 Consider preallocating syms
cmd/internal/objfile/elf.go:35 Consider preallocating syms
cmd/link/internal/ld/lib.go:1070 Consider preallocating argv
cmd/vet/all/main.go:91 Consider preallocating pp
database/sql/sql.go:66 Consider preallocating list
debug/macho/file.go:506 Consider preallocating all
internal/trace/order.go:55 Consider preallocating batches
mime/quotedprintable/reader_test.go:191 Consider preallocating outcomes
net/dnsclient_unix_test.go:954 Consider preallocating confLines
net/interface_solaris.go:85 Consider preallocating ifat
net/interface_linux_test.go:91 Consider preallocating ifmat4
net/interface_linux_test.go:100 Consider preallocating ifmat6
net/internal/socktest/switch.go:34 Consider preallocating st
os/os_windows_test.go:766 Consider preallocating args
runtime/pprof/internal/profile/filter.go:77 Consider preallocating lines
runtime/pprof/internal/profile/profile.go:554 Consider preallocating names
text/template/parse/node.go:189 Consider preallocating decl // cmd/api/goapi.go:301
var missing [] string
for feature := range optionalSet {
missing = append ( missing , feature )
}
// cmd/fix/typecheck.go:219
var b []ast. Expr
for _ , x := range a {
b = append ( b , x )
}
// net/internal/socktest/switch.go:34
var st [] Stat
sw . smu . RLock ()
for _ , s := range sw . stats {
ns := * s
st = append ( st , ns )
}
sw . smu . RUnlock ()
// cmd/api/goapi.go:301
var missing [] string
for feature := range optionalSet {
missing = append ( missing , feature )
} 슬라이스가 사라지는 크기가 작더라도, 용량을 명시 append 으로 지정하는 데있어 성능 이점이 여전히 남아 있어야합니다. 물론, Preallocation은 어디에서나 수행 할 필요는 없습니다. 이 도구의 임무는 단지 Preallocating을 고려해야 할 장소를 제안하는 것입니다.
var 와 함께 슬라이스의 0 값을 사용하지 않고 슬라이스 선언 중에 GO의 내장 make 기능으로 초기화하여 적절한 유형과 길이를 전달합니다. 이 길이는 일반적으로 당신이 범위를 둔 모든 것입니다. 위에서 예제를 수정하는 것은 다음과 같습니다.
// cmd/api/goapi.go:301
missing := make ([] string , 0 , len ( optionalSet ))
for feature := range optionalSet {
missing = append ( missing , feature )
}
// cmd/fix/typecheck.go:219
b := make ([]ast. Expr , 0 , len ( a ))
for _ , x := range a {
b = append ( b , x )
}
// net/internal/socktest/switch.go:34
st := make ([] Stat , 0 , len ( sw . stats ))
sw . smu . RLock ()
for _ , s := range sw . stats {
ns := * s
st = append ( st , ns )
}
sw . smu . RUnlock ()
// cmd/api/goapi.go:301
missing := make ([] string , 0 , len ( optionalSet ))
for feature := range optionalSet {
missing = append ( missing , feature )
} 참고 : 성능이 절대적으로 중요하다면 더 큰 슬라이스에 대한 append 대신 copy 사용하는 것이 더 효율적일 수 있습니다. 참고로 다음 벤치 마크를 참조하십시오.
func BenchmarkSize200PreallocateCopy ( b * testing. B ) {
existing := make ([] int64 , 200 , 200 )
b . ResetTimer ()
for i := 0 ; i < b . N ; i ++ {
// Preallocate our initial slice
init := make ([] int64 , len ( existing ))
copy ( init , existing )
}
} $ go test -bench=. -benchmem
goos: linux
goarch: amd64
BenchmarkSize200NoPreallocate-4 500000 3080 ns/op 4088 B/op 9 allocs/op
BenchmarkSize200Preallocate-4 1000000 1163 ns/op 1792 B/op 1 allocs/op
BenchmarkSize200PreallocateCopy-4 2000000 807 ns/op 1792 B/op 1 allocs/op
요청을 당기는 것을 환영합니다!
Prealloc을 즐겼다면 다른 정적 분석 도구를 살펴보십시오!