pesos / grofer

A system and resource monitoring tool written in Golang!
Apache License 2.0
352 stars 52 forks source link

Optimize struct memory usage #96

Closed MadhavJivrajani closed 3 years ago

MadhavJivrajani commented 3 years ago

Description

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

Caveats:

These caveats need to be documented in CONTRIBUTING.md

Checklist: