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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
field alignment for generated structs #1117
Comments
Thanks for this proposal @napei. I find it cleaner to provide this functionality using a standalone CLI tool that can be executed with another Is there any CLI that uses |
This code generates a CLI application which can run the analysis on a file. The only thing it doesn't do well is preserving comments. Unfortunately |
https://github.com/orijtech/structslop possibly can
|
Launching In this case And as @napei said it also deletes some comments (the ones on structs it aligns). |
Comments are problem, but issues like you provide is not a problem. I think that generated code must use struct field names |
Agree. Feel free to send a patch or I can do it later. Thanks 馃檹 |
Sent a patch: #2648 |
Can we check if there are PRs on golang repository for fieldalignment removing comments? Where to look? |
I tested the PR with my project, now the |
Except for comments, right? What do you think about align struct before applying comments during generation steps? |
Yes, fieldalignment remove all comments on the struct fields. The comment and code was generate by Go template as GQLGen/other tools did. Do you have any idea to align the struct with Go template? But, imo the comment should keep for public field only, because the private field was common to grouped by the intent - which may not in the right order after struct align. |
Summary 馃挕
Because of how memory allocation is done in go, it's possible to optimise the amount of memory that a struct takes by ordering fields based on their size. In the case of structs inside
./ent
there is possibly some performance/memory benefits by ordering these during codegen.Consider this struct (comments removed):
Not exactly a dramatic change, but quite possible could be a helpful change with more complex structs especially when part of the usage of
ent
is to load data into memory and do stuff with it.Because it's an internal package inside golang, it's hard to use programmatically, instead needing to be made into a cli tool..perhaps the algorithms can be taken and re-used internally in ent.
Example: running on an ent directory
It's an extra checker for
gopls
that I only recently turned on and has fixed a few things for me so far.The text was updated successfully, but these errors were encountered: