{"id":13609847,"url":"https://github.com/dkorunic/betteralign","last_synced_at":"2025-04-14T05:20:20.979Z","repository":{"id":144887172,"uuid":"614591980","full_name":"dkorunic/betteralign","owner":"dkorunic","description":"Make your Go programs use less memory (maybe)","archived":false,"fork":false,"pushed_at":"2025-03-31T07:38:26.000Z","size":283,"stargazers_count":799,"open_issues_count":0,"forks_count":13,"subscribers_count":9,"default_branch":"main","last_synced_at":"2025-04-14T05:20:15.747Z","etag":null,"topics":["alignment","analyzer","garbage-collector","golang","memory"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dkorunic.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-15T22:54:16.000Z","updated_at":"2025-04-14T01:55:14.000Z","dependencies_parsed_at":"2024-02-03T16:27:48.660Z","dependency_job_id":"9f3ad3b4-832e-46ed-8c74-5830e9d18fb6","html_url":"https://github.com/dkorunic/betteralign","commit_stats":{"total_commits":46,"total_committers":6,"mean_commits":7.666666666666667,"dds":"0.13043478260869568","last_synced_commit":"68b02da106ffc95155f95be025ae535829d0affa"},"previous_names":[],"tags_count":30,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dkorunic%2Fbetteralign","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dkorunic%2Fbetteralign/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dkorunic%2Fbetteralign/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dkorunic%2Fbetteralign/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dkorunic","download_url":"https://codeload.github.com/dkorunic/betteralign/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248824928,"owners_count":21167395,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["alignment","analyzer","garbage-collector","golang","memory"],"created_at":"2024-08-01T19:01:38.647Z","updated_at":"2025-04-14T05:20:20.950Z","avatar_url":"https://github.com/dkorunic.png","language":"Go","readme":"# betteralign\n\n[![GitHub license](https://img.shields.io/github/license/dkorunic/betteralign)](https://github.com/dkorunic/betteralign/blob/master/LICENSE)\n[![GitHub release](https://img.shields.io/github/release/dkorunic/betteralign)](https://github.com/dkorunic/betteralign/releases/latest)\n[![go-recipes](https://raw.githubusercontent.com/nikolaydubina/go-recipes/main/badge.svg?raw=true)](https://github.com/nikolaydubina/go-recipes)\n\n![](gopher.png)\n\n## About\n\n**betteralign** is a tool to detect structs that would use less memory if their fields were sorted and optionally sort such fields.\n\nThis is a fork of an official Go [fieldalignment](https://cs.opensource.google/go/x/tools/+/master:go/analysis/passes/fieldalignment/fieldalignment.go) tool and vast majority of the alignment code has remained the same. There are however some notable changes:\n\n- skips over generated files, either files with known \"generated\" suffix (`_generated.go`, `_gen.go`, `.gen.go`, `.pb.go`, `.pb.gw.go`) or due to package-level comment containing `Code generated by... DO NOT EDIT.` string,\n- skips over test files (files with `_test.go` suffix),\n- skips over structs marked with comment `betteralign:ignore`,\n- can be executed in opt-in mode with specific structs marked with comment `betteralign:check`,\n- doesn't lose comments (field comments, doc comments, floating comments or otherwise) but the comment position heuristics is still work in progress,\n- does very reliable atomic file I/O with strong promise not to corrupt and/or lose contents upon rewrite ([not on Windows](https://github.com/golang/go/issues/22397#issuecomment-498856679) platform),\n- has more thorough testing in regards to expected optimised vs golden results,\n- integrates better with environments with restricted CPU and/or memory resources (Docker containers, K8s containers, LXC, LXD etc).\n\nRetaining comments has been done with using [DST](https://github.com/dave/dst) (Decorated Syntax Tree) with decorating regular AST. Sadly when using DST we cannot use \"fix\" mode with SuggestedFixes, but we have to print whole DST (over the original file) to retain decorations. Exactly this reason is why `-fix` flag from analysis package doesn't work (we don't use SuggestedFixes) and why we cannot integrate with [golangci-lint](https://golangci-lint.run/).\n\nIn case you are wondering why DST and not AST, in general sense AST does not [associate comments to nodes](https://github.com/golang/go/issues/20744), but it holds fixed offsets. Original fieldalignment tool just erases all struct/field/floating comments due to this issue and while there is a CL with [a possible fix](https://go-review.googlesource.com/c/go/+/429639), it's still a work in progress as of this time.\n\nNote, this is a single-pass analysis tool and sorting all structs optimally might require **more than one pass**.\n\nLet us also mention original projects handling this task and without which this derivative work wouldn't exist in the first place:\n\n- [fieldalignment](https://cs.opensource.google/go/x/tools/+/master:go/analysis/passes/fieldalignment/fieldalignment.go) from **Go Authors**\n- [maligned](https://github.com/mdempsky/maligned) from **Matthew Dempsky**\n- [structslop](https://github.com/orijtech/structslop) from **orijtech**\n\n## Installation\n\nThere are two ways of installing betteralign:\n\n### Manual:\n\nDownload your preferred flavor from [the releases](https://github.com/dkorunic/betteralign/releases) page and install manually, typically to `/usr/local/bin/betteralign`\n\n### Using go install:\n\n```shell\ngo install github.com/dkorunic/betteralign/cmd/betteralign@latest\n```\n\n## Usage\n\n```shell\nbetteralign: find structs that would use less memory if their fields were sorted\n\nUsage: betteralign [-flag] [package]\n\nThis analyzer find structs that can be rearranged to use less memory, and provides\na suggested edit with the most compact order.\n\nNote that there are two different diagnostics reported. One checks struct size,\nand the other reports \"pointer bytes\" used. Pointer bytes is how many bytes of the\nobject that the garbage collector has to potentially scan for pointers, for example:\n\n\tstruct { uint32; string }\n\nhave 16 pointer bytes because the garbage collector has to scan up through the string's\ninner pointer.\n\n\tstruct { string; *uint32 }\n\nhas 24 pointer bytes because it has to scan further through the *uint32.\n\n\tstruct { string; uint32 }\n\nhas 8 because it can stop immediately after the string pointer.\n\nBe aware that the most compact order is not always the most efficient.\nIn rare cases it may cause two variables each updated by its own goroutine\nto occupy the same CPU cache line, inducing a form of memory contention\nknown as \"false sharing\" that slows down both goroutines.\n\nUnlike most analyzers, which report likely mistakes, the diagnostics\nproduced by betteralign very rarely indicate a significant problem,\nso the analyzer is not included in typical suites such as vet or\ngopls. Use this standalone command to run it on your code:\n\n   $ go install github.com/dkorunic/betteralign/cmd/betteralign@latest\n   $ betteralign [packages]\n\n\n\nFlags:\n  -V\tprint version and exit\n  -all\n    \tno effect (deprecated)\n  -apply\n    \tapply suggested fixes\n  -c int\n    \tdisplay offending line with this many lines of context (default -1)\n  -cpuprofile string\n    \twrite CPU profile to this file\n  -debug string\n    \tdebug flags, any subset of \"fpstv\"\n  -exclude_dirs value\n    \texclude directories matching a pattern\n  -exclude_files value\n    \texclude files matching a pattern\n  -fix\n    \tapply all suggested fixes\n  -flags\n    \tprint analyzer flags in JSON\n  -generated_files\n    \talso check and fix generated files\n  -json\n    \temit JSON output\n  -memprofile string\n    \twrite memory profile to this file\n  -source\n    \tno effect (deprecated)\n  -tags string\n    \tno effect (deprecated)\n  -test\n    \tindicates whether test files should be analyzed, too (default true)\n  -test_files\n    \talso check and fix test files\n  -trace string\n    \twrite trace log to this file\n  -v\tno effect (deprecated)\n```\n\nTo get all recommendations on your project:\n\n```shell\nbetteralign ./...\n```\n\nTo automatically fix your project files, while ignoring test and generated files:\n\n```shell\nbetteralign -apply ./...\n```\n\nIt is possible to include generated files and test files by enabling `generated_files` and/or `test_files` flags, or exclude certain files or directories with the `exclude_dirs` and/or `exclude_files` flags.\n\n## Star history\n\n[![Star History Chart](https://api.star-history.com/svg?repos=dkorunic/betteralign\u0026type=Date)](https://star-history.com/#dkorunic/betteralign\u0026Date)\n","funding_links":[],"categories":["Go"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdkorunic%2Fbetteralign","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdkorunic%2Fbetteralign","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdkorunic%2Fbetteralign/lists"}