{"repo":"alexkohler/prealloc","free":true,"listed":false,"github":"https://github.com/alexkohler/prealloc","clone":"git clone https://github.com/alexkohler/prealloc.git","description":"prealloc is a Go static analysis tool to find slice declarations that could potentially be preallocated.","language":"Go","stars":666,"topics":["golang","go","static-code-analysis","static-analyzer","static-analysis","prealloc-suggestions","slice"],"license":"MIT","category":"dev-tools","readme_excerpt":"prealloc prealloc is a Go static analysis tool to find slice declarations that could potentially be preallocated. Installation go install github.com/alexkohler/prealloc@latest Usage Similar to other Go static analysis tools (such as golint, go vet), prealloc can be invoked with one or more filenames, directories, or packages named by its import path. Prealloc also supports the ... wildcard. prealloc [flags] files/directories/packages Flags - -simple (default true) - Report preallocation suggestions only on simple loops that have no returns/breaks/continues/gotos in them. Setting this to false may increase false positives. - -rangeloops (default true) - Report preallocation suggestions on range loops. - -forloops (default false) - Report preallocation suggestions on for loops. This is false by default due to there generally being weirder things happening inside for loops (at least from what I've observed in the Standard Library). Purpose While Go does attempt to avoid reallocation by growing the capacity in advance, this sometimes isn't enough for longer slices. If the size of a slice is known at the time of its creation, it should be specified. Consider the following benchmark: (this can be found in prealloc test.go in this repo) As you can see, not preallocating can cause a performance hit, primarily due to Go having to reallocate the underlying array. The pattern benchmarked above is common in Go: declare a slice, then write some sort of range or for loop that appends or in","default_branch":null,"files":null,"tree":[],"storefront":"/r/alexkohler","claimed":false,"request_supported":{"post":"https://gitbuyer.com/r/alexkohler/prealloc/request-supported","requests":0},"note":"indexed from public GitHub; nothing is for sale on this page. Clone it from GitHub. Paid listings live at /search."}