Skip to content

why this re-arrange algorithm works? #55

@cuiweixie

Description

@cuiweixie

structslop/structslop.go

Lines 246 to 267 in 13637e2

sort.Slice(fields, func(i, j int) bool {
ti, tj := fields[i].Type(), fields[j].Type()
si, sj := sizes.Sizeof(ti), sizes.Sizeof(tj)
if si == 0 && sj != 0 {
return true
}
if sj == 0 && si != 0 {
return false
}
ai, aj := sizes.Alignof(ti), sizes.Alignof(tj)
if ai != aj {
return ai > aj
}
if si != sj {
return si > sj
}
return false
})

why sort by <align of field, size of field> can minimize the sizeof struct?
is there some formal prove?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions