-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Allocate memory for known size slices #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allocate memory for known size slices #42
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dixonwille thank you for pull request!
Also I'm planning to integrate https://github.com/alexkohler/prealloc to golangci-lint
pkg/golinters/gocyclo.go
Outdated
@@ -22,17 +22,20 @@ func (Gocyclo) Desc() string { | |||
func (g Gocyclo) Run(ctx context.Context, lintCtx *Context) ([]result.Issue, error) { | |||
var stats []gocycloAPI.Stat | |||
for _, f := range lintCtx.ASTCache.GetAllValidFiles() { | |||
stats = gocycloAPI.BuildStats(f.F, f.Fset, stats) | |||
stats = append(stats, gocycloAPI.BuildStats(f.F, f.Fset, stats)...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gocycloAPI.BuildStats
has the counter-intuitive signature, but inside it already appends:
func BuildStats(f *ast.File, fset *token.FileSet, stats []Stat) []Stat {
for _, decl := range f.Decls {
if fn, ok := decl.(*ast.FuncDecl); ok {
stats = append(stats, Stat{
PkgName: f.Name.Name,
FuncName: funcName(fn),
Complexity: complexity(fn),
Pos: fset.Position(fn.Pos()),
})
}
}
return stats
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. That is very counter-intuitive. Thanks
for _, s := range stats { | ||
if s.Complexity <= lintCtx.Settings().Gocyclo.MinComplexity { | ||
continue | ||
break //Break as the stats is already sorted from greatest to least |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
as BuildStats already appends
I know this does not save that many memory allocations compared to other areas but every little bit helps in my opinion.
Also, think I fixed a bug that was not reported in GoCyclo on line 25. It was iterating through the files but only kept the latest stats instead of appending.