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: false,
|
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 |
|
198 |
|
199 |
|
200 | ObjectPool.prototype.allocateArray = function (lengthOrArray) {
|
201 | var array;
|
202 | var length;
|
203 | if (Array.isArray(lengthOrArray)) {
|
204 | array = lengthOrArray;
|
205 | length = lengthOrArray.length;
|
206 | }
|
207 | else {
|
208 | length = lengthOrArray;
|
209 | array = new Array(length);
|
210 | }
|
211 | this._borrowRate += length;
|
212 | this._flowRate += length;
|
213 | var filled = 0;
|
214 |
|
215 | if (this._freeCount > 0) {
|
216 | var pool = this._freeList;
|
217 | var poolFilled = Math.min(this._freeCount, length);
|
218 | var poolSize = this._freeCount;
|
219 | for (var i = 0; i < poolFilled; i++) {
|
220 | array[filled] = pool[poolSize - 1];
|
221 | ++filled;
|
222 | --poolSize;
|
223 | }
|
224 | this._freeCount = poolSize;
|
225 | }
|
226 |
|
227 | while (filled < length) {
|
228 | array[filled] = this.create();
|
229 | ++filled;
|
230 | }
|
231 | return array;
|
232 | };
|
233 | |
234 |
|
235 |
|
236 |
|
237 |
|
238 | ObjectPool.prototype.release = function (object) {
|
239 | ++this._returnRate;
|
240 | --this._flowRate;
|
241 | if (this._freeCount === this.capacity) {
|
242 | this.capacity *= this._capacityRatio;
|
243 | }
|
244 | this._freeList[this._freeCount] = object;
|
245 | ++this._freeCount;
|
246 | };
|
247 | |
248 |
|
249 |
|
250 |
|
251 |
|
252 | ObjectPool.prototype.releaseArray = function (array) {
|
253 | this._returnRate += array.length;
|
254 | this._flowRate -= array.length;
|
255 | if (this._freeCount + array.length > this.capacity) {
|
256 |
|
257 | this.capacity = Math.max(this.capacity * this._capacityRatio, this._freeCount + array.length);
|
258 | }
|
259 |
|
260 | for (var i = 0, j = array.length; i < j; i++) {
|
261 | this._freeList[this._freeCount] = array[i];
|
262 | ++this._freeCount;
|
263 | }
|
264 | };
|
265 | |
266 |
|
267 |
|
268 |
|
269 |
|
270 | ObjectPool.prototype.reserve = function (count) {
|
271 | this._reserveCount = count;
|
272 | if (this._freeCount < count) {
|
273 | var diff = this._freeCount - count;
|
274 | for (var i = 0; i < diff; i++) {
|
275 | this._freeList[this._freeCount] = this.create();
|
276 | ++this._freeCount;
|
277 | }
|
278 | }
|
279 | };
|
280 | |
281 |
|
282 |
|
283 |
|
284 |
|
285 | ObjectPool.prototype.limit = function (count) {
|
286 | if (this._freeCount > count) {
|
287 | var oldCapacity = this.capacity;
|
288 | if (oldCapacity > count * this._capacityRatio) {
|
289 | this.capacity = count * this._capacityRatio;
|
290 | }
|
291 | var excessBound = Math.min(this._freeCount, this.capacity);
|
292 | for (var i = count; i < excessBound; i++) {
|
293 | this._freeList[i] = null;
|
294 | }
|
295 | }
|
296 | };
|
297 | |
298 |
|
299 |
|
300 |
|
301 |
|
302 | ObjectPool.prototype.startGC = function (ticker) {
|
303 | if (ticker === void 0) { ticker = Ticker.shared; }
|
304 | ticker.add(this._gcTick, null, UPDATE_PRIORITY.UTILITY);
|
305 | };
|
306 | |
307 |
|
308 |
|
309 |
|
310 |
|
311 | ObjectPool.prototype.stopGC = function (ticker) {
|
312 | if (ticker === void 0) { ticker = Ticker.shared; }
|
313 | ticker.remove(this._gcTick);
|
314 | };
|
315 | return ObjectPool;
|
316 | }());
|
317 |
|
318 | var poolMap = new Map();
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 | var ObjectPoolFactory = (function () {
|
339 | function ObjectPoolFactory() {
|
340 | }
|
341 | |
342 |
|
343 |
|
344 | ObjectPoolFactory.build = function (Type) {
|
345 | var pool = poolMap.get(Type);
|
346 | if (pool) {
|
347 | return pool;
|
348 | }
|
349 | pool = new ( (function (_super) {
|
350 | __extends(DefaultObjectPool, _super);
|
351 | function DefaultObjectPool() {
|
352 | return _super !== null && _super.apply(this, arguments) || this;
|
353 | }
|
354 | DefaultObjectPool.prototype.create = function () {
|
355 | return new Type();
|
356 | };
|
357 | return DefaultObjectPool;
|
358 | }(ObjectPool)))();
|
359 | poolMap.set(Type, pool);
|
360 | return pool;
|
361 | };
|
362 | return ObjectPoolFactory;
|
363 | }());
|
364 |
|
365 | export { ObjectPool, ObjectPoolFactory };
|
366 |
|