dkorunic / betteralign

Make your Go programs use less memory (maybe)
BSD 3-Clause "New" or "Revised" License
671 stars 14 forks source link

Incorrect size calculation? #7

Closed derlaft closed 1 year ago

derlaft commented 1 year ago

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
dkorunic commented 1 year ago

@derlaft Report states the amount of object bytes that GC has to scan for pointers. With the following:

type structB struct {
        a net.IP
        b uint8
}

GC would scan first 8 bytes for pointers (net.IP type) and immediately stop upon reaching uint8 type.

derlaft commented 1 year ago

Thanks a lot!

derlaft commented 1 year ago

btw is there an article (or anything similar) that I could read about that effect on GC? would be interesting to know more details, but so far cannot find anything

derlaft commented 1 year ago

nvm, found it :) https://cs.opensource.google/go/x/tools/+/refs/tags/v0.9.1:go/analysis/passes/fieldalignment/fieldalignment.go;l=23

dkorunic commented 1 year ago

Yes, actually Vincent has an excellent blog series on Golang GC, but it's also mentioned I guess everywhere including official GC Guide.