1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | import { Ticker, UPDATE_PRIORITY } from '@pixi/ticker';
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | var extendStatics = function(d, b) {
|
27 | extendStatics = Object.setPrototypeOf ||
|
28 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
29 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
30 | return extendStatics(d, b);
|
31 | };
|
32 |
|
33 | function __extends(d, b) {
|
34 | extendStatics(d, b);
|
35 | function __() { this.constructor = d; }
|
36 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
37 | }
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | var AverageProvider = (function () {
|
49 | |
50 |
|
51 |
|
52 |
|
53 |
|
54 | function AverageProvider(windowSize, decayRatio) {
|
55 | this._history = new Array(windowSize);
|
56 | this._decayRatio = decayRatio;
|
57 | this._currentIndex = 0;
|
58 | for (var i = 0; i < windowSize; i++) {
|
59 | this._history[i] = 0;
|
60 | }
|
61 | }
|
62 | |
63 |
|
64 |
|
65 |
|
66 |
|
67 | AverageProvider.prototype.next = function (input) {
|
68 | var _a = this, history = _a._history, decayRatio = _a._decayRatio;
|
69 | var historyLength = history.length;
|
70 | this._currentIndex = this._currentIndex < historyLength - 1 ? this._currentIndex + 1 : 0;
|
71 | history[this._currentIndex] = input;
|
72 | var weightedSum = 0;
|
73 | var weight = 0;
|
74 | for (var i = this._currentIndex + 1; i < historyLength; i++) {
|
75 | weightedSum = (weightedSum + history[i]) * decayRatio;
|
76 | weight = (weight + 1) * decayRatio;
|
77 | }
|
78 | for (var i = 0; i <= this._currentIndex; i++) {
|
79 | weightedSum = (weightedSum + history[i]) * decayRatio;
|
80 | weight = (weight + 1) * decayRatio;
|
81 | }
|
82 | this._average = weightedSum / weight;
|
83 | return this._average;
|
84 | };
|
85 | AverageProvider.prototype.absDev = function () {
|
86 | var errSum = 0;
|
87 | for (var i = 0, j = this._history.length; i < j; i++) {
|
88 | errSum += Math.abs(this._history[i] - this._average);
|
89 | }
|
90 | return errSum / this._history.length;
|
91 | };
|
92 | return AverageProvider;
|
93 | }());
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 | var ObjectPool = (function () {
|
105 | |
106 |
|
107 |
|
108 | function ObjectPool(options) {
|
109 | var _this = this;
|
110 | if (options === void 0) { options = {}; }
|
111 | this._gcTick = function () {
|
112 | _this._borrowRateAverage = _this._borrowRateAverageProvider.next(_this._borrowRate);
|
113 | _this._marginAverage = _this._marginAverageProvider.next(_this._freeCount - _this._borrowRate);
|
114 | var absDev = _this._borrowRateAverageProvider.absDev();
|
115 | _this._flowRate = 0;
|
116 | _this._borrowRate = 0;
|
117 | _this._returnRate = 0;
|
118 | var poolSize = _this._freeCount;
|
119 | var poolCapacity = _this._freeList.length;
|
120 |
|
121 | if (poolSize < 128 && _this._borrowRateAverage < 128 && poolCapacity < 128) {
|
122 | return;
|
123 | }
|
124 |
|
125 | var threshold = Math.max(_this._borrowRateAverage * (_this._capacityRatio - 1), _this._reserveCount);
|
126 | if (_this._freeCount > threshold + absDev) {
|
127 | var newCap = threshold + absDev;
|
128 | _this.capacity = Math.min(_this._freeList.length, Math.ceil(newCap));
|
129 | _this._freeCount = _this._freeList.length;
|
130 | }
|
131 | };
|
132 | |
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 | this._freeList = [];
|
139 | |
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 | this._freeCount = 0;
|
146 | this._borrowRate = 0;
|
147 | this._returnRate = 0;
|
148 | this._flowRate = 0;
|
149 | this._borrowRateAverage = 0;
|
150 | this._reserveCount = options.reserve || 0;
|
151 | this._capacityRatio = options.capacityRatio || 1.2;
|
152 | this._decayRatio = options.decayRatio || 0.67;
|
153 | this._marginAverage = 0;
|
154 | this._borrowRateAverageProvider = new AverageProvider(128, this._decayRatio);
|
155 | this._marginAverageProvider = new AverageProvider(128, this._decayRatio);
|
156 | }
|
157 | Object.defineProperty(ObjectPool.prototype, "capacity", {
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 | |
166 |
|
167 |
|
168 |
|
169 |
|
170 | get: function () {
|
171 | return this._freeList.length;
|
172 | },
|
173 | set: function (cp) {
|
174 | this._freeList.length = Math.ceil(cp);
|
175 | },
|
176 | enumerable: true,
|
177 | configurable: true
|
178 | });
|
179 | |
180 |
|
181 |
|
182 |
|
183 |
|
184 | ObjectPool.prototype.allocate = function () {
|
185 | ++this._borrowRate;
|
186 | ++this._flowRate;
|
187 | if (this._freeCount > 0) {
|
188 | return this._freeList[--this._freeCount];
|
189 | }
|
190 | return this.create();
|
191 | };
|
192 | |
193 |
|
194 |
|
195 |
|
196 |
|
197 | ObjectPool.prototype.release = function (object) {
|
198 | ++this._returnRate;
|
199 | --this._flowRate;
|
200 | if (this._freeCount === this.capacity) {
|
201 | this.capacity *= this._capacityRatio;
|
202 | }
|
203 | this._freeList[this._freeCount] = object;
|
204 | ++this._freeCount;
|
205 | };
|
206 | |
207 |
|
208 |
|
209 |
|
210 |
|
211 | ObjectPool.prototype.reserve = function (count) {
|
212 | this._reserveCount = count;
|
213 | if (this._freeCount < count) {
|
214 | var diff = this._freeCount - count;
|
215 | for (var i = 0; i < diff; i++) {
|
216 | this._freeList[this._freeCount] = this.create();
|
217 | ++this._freeCount;
|
218 | }
|
219 | }
|
220 | };
|
221 | |
222 |
|
223 |
|
224 |
|
225 |
|
226 | ObjectPool.prototype.limit = function (count) {
|
227 | if (this._freeCount > count) {
|
228 | var oldCapacity = this.capacity;
|
229 | if (oldCapacity > count * this._capacityRatio) {
|
230 | this.capacity = count * this._capacityRatio;
|
231 | }
|
232 | var excessBound = Math.min(this._freeCount, this.capacity);
|
233 | for (var i = count; i < excessBound; i++) {
|
234 | this._freeList[i] = null;
|
235 | }
|
236 | }
|
237 | };
|
238 | |
239 |
|
240 |
|
241 |
|
242 |
|
243 | ObjectPool.prototype.startGC = function (ticker) {
|
244 | if (ticker === void 0) { ticker = Ticker.shared; }
|
245 | ticker.add(this._gcTick, null, UPDATE_PRIORITY.UTILITY);
|
246 | };
|
247 | |
248 |
|
249 |
|
250 |
|
251 |
|
252 | ObjectPool.prototype.stopGC = function (ticker) {
|
253 | if (ticker === void 0) { ticker = Ticker.shared; }
|
254 | ticker.remove(this._gcTick);
|
255 | };
|
256 | return ObjectPool;
|
257 | }());
|
258 |
|
259 | var poolMap = new Map();
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 | var ObjectPoolFactory = (function () {
|
280 | function ObjectPoolFactory() {
|
281 | }
|
282 | |
283 |
|
284 |
|
285 | ObjectPoolFactory.build = function (Type) {
|
286 | var pool = poolMap.get(Type);
|
287 | if (pool) {
|
288 | return pool;
|
289 | }
|
290 | pool = new ( (function (_super) {
|
291 | __extends(DefaultObjectPool, _super);
|
292 | function DefaultObjectPool() {
|
293 | return _super !== null && _super.apply(this, arguments) || this;
|
294 | }
|
295 | DefaultObjectPool.prototype.create = function () {
|
296 | return new Type();
|
297 | };
|
298 | return DefaultObjectPool;
|
299 | }(ObjectPool)))();
|
300 | poolMap.set(Type, pool);
|
301 | return pool;
|
302 | };
|
303 | return ObjectPoolFactory;
|
304 | }());
|
305 |
|
306 | export { ObjectPool, ObjectPoolFactory };
|
307 |
|