pemistahl / lingua-go

The most accurate natural language detection library for Go, suitable for short text and mixed-language text
Apache License 2.0
1.19k stars 66 forks source link

Reorder struct fields to decrease memory footprint. #35

Closed lukaszraczylo closed 1 year ago

pemistahl commented 1 year ago

Hi @lukaszraczylo, can you please explain how the reordering of the struct fields would decrease the memory footprint? Apart from that, the build fails because you have not reordered all occurrences of the fields.

lukaszraczylo commented 1 year ago

Of course, more information - https://en.wikipedia.org/wiki/Data_structure_alignment

As an example - before the alignment image After alignment image

pemistahl commented 1 year ago

Well, how much memory is saved by reordering the fields then? Please measure this. And the build still fails.

lukaszraczylo commented 1 year ago
serialization/model.pb.go:311:32: struct of size 80 could be 72
serialization/model.pb.go:387:27: struct with 56 pointer bytes could be 40
detector.go:90:23: struct with 112 pointer bytes could be 80
model.go:25:32: struct with 24 pointer bytes could be 16
detector_test.go:222:17: struct with 16 pointer bytes could be 8
detector_test.go:289:17: struct with 32 pointer bytes could be 16
detector_test.go:470:17: struct with 48 pointer bytes could be 40
detector_test.go:510:17: struct with 72 pointer bytes could be 56
model_test.go:201:14: struct with 32 pointer bytes could be 24
model_test.go:229:14: struct with 16 pointer bytes could be 8

Via https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/fieldalignment You're welcome.