Skip to content

Commit 89db784

Browse files
author
Simon Grondin
committed
Refactor to optimize memory usage, step 3
1 parent 0e397c1 commit 89db784

File tree

8 files changed

+349
-352
lines changed

8 files changed

+349
-352
lines changed

es5.js

Lines changed: 141 additions & 129 deletions
Large diffs are not rendered by default.

lib/Bottleneck.js

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,7 @@ Bottleneck = function () {
4343
class Bottleneck {
4444
constructor(options = {}, ...invalid) {
4545
var storeInstanceOptions, storeOptions;
46-
this._drainOne = this._drainOne.bind(this);
47-
this.submit = this.submit.bind(this);
48-
this.schedule = this.schedule.bind(this);
49-
this.updateSettings = this.updateSettings.bind(this);
50-
this.incrementReservoir = this.incrementReservoir.bind(this);
46+
this._addToQueue = this._addToQueue.bind(this);
5147

5248
this._validateOptions(options, invalid);
5349

@@ -351,7 +347,7 @@ Bottleneck = function () {
351347
return waitForExecuting(1);
352348
});
353349

354-
this._addToQueue = function (job) {
350+
this._receive = function (job) {
355351
return job._reject(new Bottleneck.prototype.BottleneckError(options.enqueueErrorMessage));
356352
};
357353

@@ -365,25 +361,17 @@ Bottleneck = function () {
365361
_addToQueue(job) {
366362
var _this2 = this;
367363

368-
var args, options;
369-
args = job.args;
370-
options = job.options;
371-
372-
if (!job.doReceive()) {
373-
return false;
374-
}
375-
376-
return this._submitLock.schedule(
377-
/*#__PURE__*/
378-
_asyncToGenerator(function* () {
379-
var blocked, error, reachedHWM, shifted, strategy;
364+
return _asyncToGenerator(function* () {
365+
var args, blocked, error, options, reachedHWM, shifted, strategy;
366+
args = job.args;
367+
options = job.options;
380368

381369
try {
382-
var _ref3 = yield _this2._store.__submit__(_this2.queued(), options.weight);
370+
var _ref2 = yield _this2._store.__submit__(_this2.queued(), options.weight);
383371

384-
reachedHWM = _ref3.reachedHWM;
385-
blocked = _ref3.blocked;
386-
strategy = _ref3.strategy;
372+
reachedHWM = _ref2.reachedHWM;
373+
blocked = _ref2.blocked;
374+
strategy = _ref2.strategy;
387375
} catch (error1) {
388376
error = error1;
389377

@@ -424,21 +412,29 @@ Bottleneck = function () {
424412

425413
yield _this2._drainAll();
426414
return reachedHWM;
427-
}));
415+
})();
416+
}
417+
418+
_receive(job) {
419+
if (job.doReceive()) {
420+
return this._submitLock.schedule(this._addToQueue, job);
421+
} else {
422+
return false;
423+
}
428424
}
429425

430426
submit(...args) {
431427
var cb, fn, job, options, ref, ref1, task;
432428

433429
if (typeof args[0] === "function") {
434-
var _ref4, _ref5, _splice$call, _splice$call2;
430+
var _ref3, _ref4, _splice$call, _splice$call2;
435431

436-
ref = args, (_ref4 = ref, _ref5 = _toArray(_ref4), fn = _ref5[0], args = _ref5.slice(1), _ref4), (_splice$call = splice.call(args, -1), _splice$call2 = _slicedToArray(_splice$call, 1), cb = _splice$call2[0], _splice$call);
432+
ref = args, (_ref3 = ref, _ref4 = _toArray(_ref3), fn = _ref4[0], args = _ref4.slice(1), _ref3), (_splice$call = splice.call(args, -1), _splice$call2 = _slicedToArray(_splice$call, 1), cb = _splice$call2[0], _splice$call);
437433
options = parser.load({}, this.jobDefaults);
438434
} else {
439-
var _ref6, _ref7, _splice$call3, _splice$call4;
435+
var _ref5, _ref6, _splice$call3, _splice$call4;
440436

441-
ref1 = args, (_ref6 = ref1, _ref7 = _toArray(_ref6), options = _ref7[0], fn = _ref7[1], args = _ref7.slice(2), _ref6), (_splice$call3 = splice.call(args, -1), _splice$call4 = _slicedToArray(_splice$call3, 1), cb = _splice$call4[0], _splice$call3);
437+
ref1 = args, (_ref5 = ref1, _ref6 = _toArray(_ref5), options = _ref6[0], fn = _ref6[1], args = _ref6.slice(2), _ref5), (_splice$call3 = splice.call(args, -1), _splice$call4 = _slicedToArray(_splice$call3, 1), cb = _splice$call4[0], _splice$call3);
442438
options = parser.load(options, this.jobDefaults);
443439
}
444440

@@ -460,7 +456,7 @@ Bottleneck = function () {
460456
return typeof cb === "function" ? cb(args) : void 0;
461457
}
462458
});
463-
return this._addToQueue(job);
459+
return this._receive(job);
464460
}
465461

466462
schedule(...args) {
@@ -486,20 +482,20 @@ Bottleneck = function () {
486482

487483
job = new Job(task, args, options, this.jobDefaults, this.rejectOnDrop, this.Events, this._states, this.Promise);
488484

489-
this._addToQueue(job);
485+
this._receive(job);
490486

491487
return job.promise;
492488
}
493489

494490
wrap(fn) {
495491
var schedule, wrapped;
496-
schedule = this.schedule;
492+
schedule = this.schedule.bind(this);
497493

498494
wrapped = function wrapped(...args) {
499495
return schedule(fn.bind(this), ...args);
500496
};
501497

502-
wrapped.withOptions = (options, ...args) => {
498+
wrapped.withOptions = function (options, ...args) {
503499
return schedule(options, fn, ...args);
504500
};
505501

lib/Group.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ Group = function () {
2323
class Group {
2424
constructor(limiterOptions = {}) {
2525
this.deleteKey = this.deleteKey.bind(this);
26-
this.updateSettings = this.updateSettings.bind(this);
2726
this.limiterOptions = limiterOptions;
2827
parser.load(this.limiterOptions, this.defaults, this);
2928
this.Events = new Events(this);

lib/Sync.js

Lines changed: 49 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,14 @@
11
"use strict";
22

3-
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
3+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
44

5-
function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
5+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
66

7-
function _toArray(arr) { return _arrayWithHoles(arr) || _iterableToArray(arr) || _nonIterableRest(); }
8-
9-
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
10-
11-
function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
12-
13-
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
14-
15-
var DLList,
16-
Sync,
17-
splice = [].splice;
7+
var DLList, Sync;
188
DLList = require("./DLList");
199
Sync = class Sync {
2010
constructor(name, Promise) {
21-
this.submit = this.submit.bind(this);
11+
this.schedule = this.schedule.bind(this);
2212
this.name = name;
2313
this.Promise = Promise;
2414
this._running = 0;
@@ -30,56 +20,60 @@ Sync = class Sync {
3020
}
3121

3222
_tryToRun() {
33-
var next;
34-
35-
if (this._running < 1 && this._queue.length > 0) {
36-
this._running++;
37-
next = this._queue.shift();
38-
return next.task(...next.args, (...args) => {
39-
this._running--;
40-
41-
this._tryToRun();
42-
43-
return typeof next.cb === "function" ? next.cb(...args) : void 0;
44-
});
45-
}
23+
var _this = this;
24+
25+
return _asyncToGenerator(function* () {
26+
var args, cb, error, reject, resolve, returned, task;
27+
28+
if (_this._running < 1 && _this._queue.length > 0) {
29+
_this._running++;
30+
31+
var _this$_queue$shift = _this._queue.shift();
32+
33+
task = _this$_queue$shift.task;
34+
args = _this$_queue$shift.args;
35+
resolve = _this$_queue$shift.resolve;
36+
reject = _this$_queue$shift.reject;
37+
cb = yield _asyncToGenerator(function* () {
38+
try {
39+
returned = yield task(...args);
40+
return function () {
41+
return resolve(returned);
42+
};
43+
} catch (error1) {
44+
error = error1;
45+
return function () {
46+
return reject(error);
47+
};
48+
}
49+
})();
50+
_this._running--;
51+
52+
_this._tryToRun();
53+
54+
return cb();
55+
}
56+
})();
4657
}
4758

48-
submit(task, ...args) {
49-
var _ref, _ref2, _splice$call, _splice$call2;
50-
51-
var cb, ref;
52-
ref = args, (_ref = ref, _ref2 = _toArray(_ref), args = _ref2.slice(0), _ref), (_splice$call = splice.call(args, -1), _splice$call2 = _slicedToArray(_splice$call, 1), cb = _splice$call2[0], _splice$call);
59+
schedule(task, ...args) {
60+
var promise, reject, resolve;
61+
resolve = reject = null;
62+
promise = new this.Promise(function (_resolve, _reject) {
63+
resolve = _resolve;
64+
return reject = _reject;
65+
});
5366

5467
this._queue.push({
5568
task,
5669
args,
57-
cb
70+
resolve,
71+
reject
5872
});
5973

60-
return this._tryToRun();
61-
}
74+
this._tryToRun();
6275

63-
schedule(task, ...args) {
64-
var wrapped;
65-
66-
wrapped = function wrapped(...args) {
67-
var _ref3, _ref4, _splice$call3, _splice$call4;
68-
69-
var cb, ref;
70-
ref = args, (_ref3 = ref, _ref4 = _toArray(_ref3), args = _ref4.slice(0), _ref3), (_splice$call3 = splice.call(args, -1), _splice$call4 = _slicedToArray(_splice$call3, 1), cb = _splice$call4[0], _splice$call3);
71-
return task(...args).then(function (...args) {
72-
return cb(null, ...args);
73-
}).catch(function (...args) {
74-
return cb(...args);
75-
});
76-
};
77-
78-
return new this.Promise((resolve, reject) => {
79-
return this.submit(wrapped, ...args, function (...args) {
80-
return (args[0] != null ? reject : (args.shift(), resolve))(...args);
81-
});
82-
});
76+
return promise;
8377
}
8478

8579
};

0 commit comments

Comments
 (0)