/
pack.go
39 lines (34 loc) 路 1.04 KB
/
pack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package strategies
import (
"context"
"sort"
"github.com/1pkg/gopium/collections"
"github.com/1pkg/gopium/gopium"
)
// list of pack presets
var (
pck = pack{}
)
// pack defines strategy implementation
// that rearranges structure fields
// to obtain optimal memory utilization
// by sorting fields accordingly
// to their aligns and sizes in some order
type pack struct{} // struct size: 0 bytes; struct align: 1 bytes; struct aligned size: 0 bytes; - 馃尯 gopium @1pkg
// Apply pack implementation
func (stg pack) Apply(ctx context.Context, o gopium.Struct) (gopium.Struct, error) {
// copy original structure to result
r := collections.CopyStruct(o)
// execute memory sorting
sort.SliceStable(r.Fields, func(i, j int) bool {
// first compare aligns of two fields
// bigger aligmnet means upper position
if r.Fields[i].Align != r.Fields[j].Align {
return r.Fields[i].Align > r.Fields[j].Align
}
// then compare sizes of two fields
// bigger size means upper position
return r.Fields[i].Size > r.Fields[j].Size
})
return r, ctx.Err()
}