Skip to content
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

Implement struct slop detection rules #5

Merged
merged 5 commits into from
Sep 22, 2020
Merged

Implement struct slop detection rules #5

merged 5 commits into from
Sep 22, 2020

Conversation

cuonglm
Copy link
Contributor

@cuonglm cuonglm commented Sep 21, 2020

Using go/types.Sizes interface.

Copy link
Member

@odeke-em odeke-em left a comment

Choose a reason for hiding this comment

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

Thank you for this change @cuonglm! So some concerns:
a) We actually should generate for them the optimal organization and show them which fields to re-arrange instead of making them fiddle and do the Maths themselves -- this is error prone and tedious for users
b) We need to implement this with the x/analysis framework, which would involve a slightly different organization than Matthew's code
c) Given that this code will be owned by Orijtech, I am worried about code copying binding us to Matthew's code. Let's implement this afresh, there is a straightforward approach to it

structslop.go Outdated Show resolved Hide resolved
@odeke-em
Copy link
Member

odeke-em commented Sep 21, 2020 via email

@cuonglm
Copy link
Contributor Author

cuonglm commented Sep 21, 2020

At least from the expected string I didn’t see a change in the struct fields except it saying was 24 can be 16.

This is how analysis frame work works. The message only shows what’s wrong. The suggested fixed will be applied to the source when you run with -fix flag.

I think a flag to actually display the fix to user will be added soon.

@odeke-em
Copy link
Member

I think a flag to actually display the fix to user will be added soon.

And that’s what this task is about. I think we need a from the ground up approach where we build and display the optimal struct, not just what maligned displays as a warning.

@cuonglm
Copy link
Contributor Author

cuonglm commented Sep 21, 2020

And that’s what this task is about. I think we need a from the ground up approach where we build and display the optimal struct, not just what maligned displays as a warning.

Do you mean we will make a CL to go/analysis to do that?

Note that this PR built the optimal struct already (look at suggestedStruct field in result struct)

@cuonglm
Copy link
Contributor Author

cuonglm commented Sep 21, 2020

@odeke-em I updated to show suggested fix to user, I think the only thing left is porting the code from maligned to our own.

@cuonglm
Copy link
Contributor Author

cuonglm commented Sep 21, 2020

@odeke-em PTAL, latest commit should address all your concern.

@cuonglm cuonglm mentioned this pull request Sep 21, 2020
Copy link
Member

@odeke-em odeke-em left a comment

Choose a reason for hiding this comment

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

Nice work @cuonglm, this is much better and built from the ground up! Just some minor nits.
In regards to more test cases, let's add

type s1 struct {
	b  bool
	i1 int
	i2 int
	a3 [3]bool
	_  [0]func()
}

Let's also a TODO to recognize the special case of

_ [0]func()

if at the front and it shouldn't be moved.

structslop.go Outdated Show resolved Hide resolved
structslop.go Outdated Show resolved Hide resolved
@odeke-em
Copy link
Member

Also please update the commit message as this code is anew and doesn't refer to nor depend on maligned.

@cuonglm
Copy link
Contributor Author

cuonglm commented Sep 22, 2020

Also please update the commit message as this code is anew and doesn't refer to nor depend on maligned.

Will do it once the PR is ready to merge (squashing before merge).

@cuonglm
Copy link
Contributor Author

cuonglm commented Sep 22, 2020

Let's also a TODO to recognize the special case of

Could you elaborate this? Do you mean if the _ [0]func() is at the front, we shouldn't move it eve if the moving can make struct size better?

@odeke-em
Copy link
Member

Do you mean if the _ [0]func() is at the front, we shouldn't move it eve if the moving can make struct size better?

Exactly that. This is because that’s an intentional change to make the struct incomparable and we want to keep that. All other fields can be moved around except for that one iff it exists as the first element.

@cuonglm
Copy link
Contributor Author

cuonglm commented Sep 22, 2020

Do you mean if the _ [0]func() is at the front, we shouldn't move it eve if the moving can make struct size better?

Exactly that. This is because that’s an intentional change to make the struct incomparable and we want to keep that. All other fields can be moved around except for that one iff it exists as the first element.

I don't think the position of _ [0]func() does matter. It can be appear anywhere in the struct fields, and still make the struct incomparable.

https://play.golang.org/p/iG8z94Mp-RD

@odeke-em
Copy link
Member

odeke-em commented Sep 22, 2020 via email

@cuonglm
Copy link
Contributor Author

cuonglm commented Sep 22, 2020

For it not to consume space it has to be the first element :) that’s the difference

On Mon, Sep 21, 2020 at 8:06 PM Cuong Manh Le @.***> wrote: Do you mean if the _ [0]func() is at the front, we shouldn't move it eve if the moving can make struct size better? Exactly that. This is because that’s an intentional change to make the struct incomparable and we want to keep that. All other fields can be moved around except for that one iff it exists as the first element. I don't think the position of _ [0]func() does matter. It can be appear anywhere in the struct fields, and still make the struct incomparable. https://play.golang.org/p/iG8z94Mp-RD — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <#5 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABFL3V3X5RETZCQUYIH3VNDSHAIDJANCNFSM4RUCOBFQ .

Ah, that makes sense, TIL.

Do you have any reference for this? (or I will take a look at the compiler when I have time 👍 )

@odeke-em
Copy link
Member

odeke-em commented Sep 22, 2020 via email

@cuonglm cuonglm merged commit 4ec902d into master Sep 22, 2020
@cuonglm cuonglm deleted the cuonglm/issue-3 branch September 22, 2020 03:50
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.

2 participants