dkorunic / betteralign Goto Github PK
View Code? Open in Web Editor NEWMake your Go programs use less memory (maybe)
License: BSD 3-Clause "New" or "Revised" License
Make your Go programs use less memory (maybe)
License: BSD 3-Clause "New" or "Revised" License
I am on windows running the command and getting the following error:
go install github.com/dkorunic/betteralign/cmd/betteralign@latest
# github.com/dkorunic/betteralign
..\..\go\pkg\mod\github.com\dkorunic\[email protected]\betteralign.go:519:21: undefined: renameio.WriteFile
..\..\go\pkg\mod\github.com\dkorunic\[email protected]\betteralign.go:519:60: undefined: renameio.IgnoreUmask
First of all thank you for your work on this! I noticed something that I'm not sure this tool will be able to address but thought I would bring this up. I have an easy way to reproduce the "issue".
package main
import "net/http"
type MyStruct struct {
Handler http.HandlerFunc
Method string
Pattern string
SomeUint uint16
Resource string
}
func main() {
register("3", MyStruct{
func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
},
http.MethodPost,
"pattern",
0,
"resource",
})
}
func register(version string, m MyStruct) error {
return nil
}
The problem being that MyStruct
gets optimally sorted but the inline struct maintains the previous order of fields which I then have to fix manually. Feel free to close this out if it's not something we can address, I might also take a look tonight and see if it's doable but wanted to let you know first.
I am using the betteralign
tool to align the code in my project automatically. When I run the command betteralign -apply ./...
in my project CI, the tool successfully aligns the code, but it returns an exit code of 3, which causes my CI to fail.
I expect betteralign
to return an exit code of 0 when it successfully aligns my code with the -apply
flag.
I am using the betteralign
tool to align the code in my project automatically. When I run the command betteralign -apply ./...
in my project CI, the tool successfully aligns the code, but it returns an exit code of 3, which causes my CI to fail.
I expect betteralign
to return an exit code of 0 when it successfully aligns my code with the -apply
flag.
https://golangci-lint.run/contributing/new-linters/
looks like most of the work is impl the go/analysis interface
Understanding the metrics is crucial, especially when there is a noticeable enhancement. This request is for the inclusion of a feature that would add details regarding the percentage or quantity of memory optimized after the execution of the betteralign ./...
command. Such information would contribute to a more comprehensive assessment of the impact and effectiveness of the optimization process.
Given the code:
type structB struct {
b uint8
a net.IP
}
betteralign
suggests to realign:
% betteralign ./...
/tmp/tmp.RrY2d8UsZv/main.go:14:14: struct with 16 pointer bytes could be 8
However, that makes no difference:
size of *main.structA = 32
size of *main.structB = 32
i'm very interesting to reuse you code to align my files after it generated, do you plan to change code to be able to use as package?
Cool tool. I found the fieldalignment
tool unsuitable for general purpose use (e.g. as a core part of the build). So far, this resolves the qualms I had, so thanks.
I'm guessing there's a reason you made it -apply
rather than -fix
, but just thought I'd raise that -fix
is also documented, but doesn't seem to do anything. I tried -fix
first, for whatever reason.
$ betteralign -h
betteralign: find structs that would use less memory if their fields were sorted
Usage: betteralign [-flag] [package]
This analyzer find structs that can be rearranged to use less memory, and provides
a suggested edit with the most compact order.
Note that there are two different diagnostics reported. One checks struct size,
and the other reports "pointer bytes" used. Pointer bytes is how many bytes of the
object that the garbage collector has to potentially scan for pointers, for example:
struct { uint32; string }
have 16 pointer bytes because the garbage collector has to scan up through the string's
inner pointer.
struct { string; *uint32 }
has 24 pointer bytes because it has to scan further through the *uint32.
struct { string; uint32 }
has 8 because it can stop immediately after the string pointer.
Be aware that the most compact order is not always the most efficient.
In rare cases it may cause two variables each updated by its own goroutine
to occupy the same CPU cache line, inducing a form of memory contention
known as "false sharing" that slows down both goroutines.
Flags:
-V print version and exit
-all
no effect (deprecated)
-apply
apply suggested fixes
-c int
display offending line with this many lines of context (default -1)
-cpuprofile string
write CPU profile to this file
-debug string
debug flags, any subset of "fpstv"
-fix
apply all suggested fixes
-flags
print analyzer flags in JSON
-generated_files
also check and fix generated files
-json
emit JSON output
-memprofile string
write memory profile to this file
-source
no effect (deprecated)
-tags string
no effect (deprecated)
-test
indicates whether test files should be analyzed, too (default true)
-test_files
also check and fix test files
-trace string
write trace log to this file
-v no effect (deprecated)
Why not use stable sort for struct fields?
// something like
type before struct {
topBool bool
string1 string
string2 string
string3 string
bottomBool bool
}
// sometimes turns into
type after_bad struct {
string2 string
string1 string
string3 string
topBool bool
bottomBool bool
}
// when we can have
type after_good struct {
string1 string
string2 string
string3 string
topBool bool
bottomBool bool
}
The fix is like five keystrokes, but I'll attach a PR.
Thanks for your work btw, great tool!
Tried versions both 0.3.1 and 0.3.2
I have a struct that I am trying to ignore. I have tried to comment it in multiple ways -
//betteralign:ignore
type MyStruct struct {
Ts time.Time `json:"ts"`
Val float64 `json:"val"`
MetricID int64 `json:"metricId"`
EntityID int64 `json:"entityId"`
Tags pgtype.JSONB `json:"tags"`
Parameters pgtype.JSONB `json:"parameters"`
}
// betteralign:ignore
type MyStruct struct {
Ts time.Time `json:"ts"`
Val float64 `json:"val"`
MetricID int64 `json:"metricId"`
EntityID int64 `json:"entityId"`
Tags pgtype.JSONB `json:"tags"`
Parameters pgtype.JSONB `json:"parameters"`
}
type MyStruct struct {
// betteralign:ignore
Ts time.Time `json:"ts"`
Val float64 `json:"val"`
MetricID int64 `json:"metricId"`
EntityID int64 `json:"entityId"`
Tags pgtype.JSONB `json:"tags"`
Parameters pgtype.JSONB `json:"parameters"`
}
And each time, the library reorders the struct.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.