Skip to content

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

Merged
merged 2 commits into from
May 30, 2018
Merged

Allocate memory for known size slices #42

merged 2 commits into from
May 30, 2018

Conversation

dixonwille
Copy link

@dixonwille dixonwille commented May 30, 2018

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.

Copy link
Collaborator

@golangci golangci left a 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

@@ -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)...)
Copy link
Collaborator

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
}

Copy link
Author

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

as BuildStats already appends
@golangci golangci merged commit 80a5ff2 into golangci:master May 30, 2018
@dixonwille dixonwille deleted the feature/better-memory-alloc-for-slices branch May 30, 2018 16:36
@ldez ldez added this to the v1.3 milestone Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants