-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathoptimizer.go
161 lines (145 loc) · 3.75 KB
/
optimizer.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package ecs
import (
"reflect"
"sort"
"time"
)
type ShapeInfo struct {
typ reflect.Type
eNum int64
shapes []IShape
}
type OptimizerReporter struct {
shapeUsage map[reflect.Type]IShape
}
func (o *OptimizerReporter) init() {
o.shapeUsage = map[reflect.Type]IShape{}
}
type optimizer struct {
world *ecsWorld
startTime time.Time
expireTime time.Time
lastSample time.Time
shapeInfos []*ShapeInfo
lastCollectConsumption time.Duration
}
func newOptimizer(world *ecsWorld) *optimizer {
return &optimizer{world: world}
}
// Collect 采集分布在各个系统中的OptimizerReporter
func (o *optimizer) collect() {
start := time.Now()
var opts []*OptimizerReporter
for _, value := range o.world.systemFlow.systems {
system, ok := value.(ISystem)
if !ok {
continue
}
if system != nil {
opts = append(opts, system.getOptimizer())
}
}
//all shapes
var shapeRef = map[reflect.Type]*ShapeInfo{}
for _, opt := range opts {
for _, shp := range opt.shapeUsage {
if info, ok := shapeRef[shp.getType()]; ok {
info.eNum += shp.base().executeNum
} else {
shapeInfo := &ShapeInfo{
typ: shp.getType(),
eNum: shp.base().executeNum,
shapes: []IShape{shp},
}
shapeRef[shp.getType()] = shapeInfo
}
}
}
//sort
o.shapeInfos = []*ShapeInfo{}
for _, info := range shapeRef {
o.shapeInfos = append(o.shapeInfos, info)
}
sort.Slice(o.shapeInfos, func(i, j int) bool {
return o.shapeInfos[i].eNum > o.shapeInfos[j].eNum
})
o.lastCollectConsumption = time.Since(start)
}
func (o *optimizer) optimize(IdleTime time.Duration, force bool) {
Log.Infof("start optimize, rest time: %v", IdleTime)
o.startTime = time.Now()
o.lastSample = time.Now()
o.expireTime = o.startTime.Add(IdleTime)
o.collect()
elapsed := o.elapsedStep()
Log.Infof("collect step 1: %v", elapsed)
o.memTidy(force)
rest := o.expire()
total := time.Now().Sub(o.startTime)
Log.Infof("end optimize, rest time: %v, total: %v", rest, total)
}
func (o *optimizer) expire() time.Duration {
return time.Until(o.expireTime)
}
func (o *optimizer) elapsed() time.Duration {
return time.Now().Sub(o.startTime)
}
func (o *optimizer) elapsedStep() time.Duration {
now := time.Now()
r := now.Sub(o.lastSample)
o.lastSample = now
return r
}
func (o *optimizer) memTidy(force bool) {
//seq := uint32(0)
//m := map[interface{}][]*EntityInfo{}
//o.world.entities.foreach(func(entity Entity, info *EntityInfo) bool {
// c := info.getCompound().Type()
// _, ok := m[c]
// if !ok {
// m[c] = []*EntityInfo{}
// }
// m[c] = append(m[c], info)
// return true
//})
//
//elapsed := o.elapsedStep()
//rest := o.expire()
//Log.Infof("memTidy step 1: %v, expire: %v", elapsed, rest)
//if !force && rest < time.Millisecond {
// return
//}
//
//for _, infos := range m {
// for _, info := range infos {
// seq++
// for _, component := range info.components {
// component.setSeq(seq)
// c := o.world.components.getComponentSet(component.Type()).GetByEntity(int64(component.Owner().Entity()))
// verify := c.(IComponent)
// println(component.debugAddress(), verify.debugAddress())
// if verify.getSeq() != component.getSeq() {
// Log.Errorf("component seq error, %v, %v", verify.getSeq(), component.getSeq())
// }
// }
// }
//}
//
//elapsed = o.elapsedStep()
//rest = o.expire()
//Log.Infof("memTidy step 2: %v, expire: %v", elapsed, rest)
//if !force && rest < time.Millisecond {
// return
//}
//
//for _, collection := range o.world.components.getCollections() {
// collection.Sort()
// if !force && o.expire() < time.Millisecond {
// break
// }
//}
//
//elapsed = o.elapsedStep()
//rest = o.expire()
//Log.Infof("memTidy step 3: %v, expire: %v", elapsed, rest)
}