Ordering of fields in structs can impact the consumption of memory due to Go trying to pad all fields according to the largest field in the struct. The fieldalignment tool is helpful in detecting and also helping with re-writing structs automatically if needed.
Output after running fieldalignment on the existing codebase:
grofer/src/utils/barGraph.go:28:15: struct with 296 pointer bytes could be 272
grofer/src/utils/data.go:19:16: struct with 88 pointer bytes could be 80
grofer/src/general/cpuInfo.go:31:14: struct with 88 pointer bytes could be 8
grofer/src/display/general/init.go:26:15: struct with 64 pointer bytes could be 48
grofer/src/process/process.go:23:14: struct of size 200 could be 184
grofer/src/export/general/generalExport.go:34:16: struct with 56 pointer bytes could be 24
grofer/src/export/general/generalExport.go:56:19: struct with 184 pointer bytes could be 144
grofer/src/export/general/generalExport.go:56:19: struct with 144 pointer bytes could be 64
grofer/src/export/proc/procExport.go:60:15: struct with 232 pointer bytes could be 144
grofer/src/utils/utils_test.go:27:13: struct with 56 pointer bytes could be 48
grofer/src/utils/utils_test.go:92:13: struct with 16 pointer bytes could be 8
To check for field alignment scripts/field_align_check.sh can be run. This needs to be documented in CONTRIBUTING.md
Caveats:
If the -fix flag is used with the fieldalignment tool, then if structs are created in the code without qualifying the values with field names, the change in order can break the build. As a general practice, we can follow that all struct values be qualifies with field names to ensure that the order of the fields won't break code.
This can be a separate issue by itself.
Sometimes, after applying the -fix, if fieldalignment is run again, it might come up with newer optimizations.
These caveats need to be documented in CONTRIBUTING.md
Description
Ordering of fields in
struct
s can impact the consumption of memory due to Go trying to pad all fields according to the largest field in the struct. Thefieldalignment
tool is helpful in detecting and also helping with re-writing structs automatically if needed.Output after running
fieldalignment
on the existing codebase:scripts/field_align_check.sh
can be run. This needs to be documented in CONTRIBUTING.mdCaveats:
-fix
flag is used with thefieldalignment
tool, then if structs are created in the code without qualifying the values with field names, the change in order can break the build. As a general practice, we can follow that allstruct
values be qualifies with field names to ensure that the order of the fields won't break code.-fix
, iffieldalignment
is run again, it might come up with newer optimizations.These caveats need to be documented in CONTRIBUTING.md
Checklist:
go fmt
on my code (reference)