Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: migrate deque to js-sdsl #1633

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const Tls = require('tls');
const Timers = require('timers');
const EventEmitter = require('events').EventEmitter;
const Readable = require('stream').Readable;
const Queue = require('denque');
const Queue = require('js-sdsl').Deque;
const SqlString = require('sqlstring');
const LRU = require('lru-cache');

Expand Down Expand Up @@ -223,12 +223,14 @@ class Connection extends EventEmitter {
!(
this._command &&
this._command.constructor === Commands.ClientHandshake &&
this._commands.length > 0
!this._commands.empty()
)
) {
bubbleErrorToConnection = true;
}
while ((command = this._commands.shift())) {
while (!this._commands.empty()) {
command = this._commands.front();
this._commands.popFront();
if (command.onResult) {
command.onResult(err);
} else {
Expand Down Expand Up @@ -404,7 +406,7 @@ class Connection extends EventEmitter {

handlePacket(packet) {
if (this._paused) {
this._paused_packets.push(packet);
this._paused_packets.pushBack(packet);
return;
}
if (packet) {
Expand Down Expand Up @@ -460,7 +462,8 @@ class Connection extends EventEmitter {
}
const done = this._command.execute(packet, this);
if (done) {
this._command = this._commands.shift();
this._command = this._commands.front();
this._commands.popFront();
if (this._command) {
this.sequenceId = 0;
this.compressedSequenceId = 0;
Expand All @@ -482,7 +485,7 @@ class Connection extends EventEmitter {
this._command = cmd;
this.handlePacket();
} else {
this._commands.push(cmd);
this._commands.pushBack(cmd);
}
return cmd;
}
Expand Down Expand Up @@ -559,7 +562,9 @@ class Connection extends EventEmitter {
resume() {
let packet;
this._paused = false;
while ((packet = this._paused_packets.shift())) {
while (!this._paused_packets.empty()) {
packet = this._paused_packets.front();
this._paused_packets.popFront();
this.handlePacket(packet);
// don't resume if packet handler paused connection
if (this._paused) {
Expand Down
42 changes: 23 additions & 19 deletions lib/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ const mysql = require('../index.js');

const EventEmitter = require('events').EventEmitter;
const PoolConnection = require('./pool_connection.js');
const Queue = require('denque');
const Queue = require('js-sdsl').Deque;
const Connection = require('./connection.js');

function spliceConnection(queue, connection) {
const len = queue.length;
const len = queue.size();
for (let i = 0; i < len; i++) {
if (queue.get(i) === connection) {
queue.removeOne(i);
if (queue.getElementByPos(i) === connection) {
queue.eraseElementByPos(i);
break;
}
}
Expand All @@ -39,19 +39,20 @@ class Pool extends EventEmitter {
return process.nextTick(() => cb(new Error('Pool is closed.')));
}
let connection;
if (this._freeConnections.length > 0) {
connection = this._freeConnections.shift();
if (!this._freeConnections.empty()) {
const connection = this._freeConnections.front();
this._freeConnections.popFront();
this.emit('acquire', connection);
return process.nextTick(() => cb(null, connection));
}
if (
this.config.connectionLimit === 0 ||
this._allConnections.length < this.config.connectionLimit
this._allConnections.size() < this.config.connectionLimit
) {
connection = new PoolConnection(this, {
config: this.config.connectionConfig
});
this._allConnections.push(connection);
this._allConnections.pushBack(connection);
return connection.connect(err => {
if (this._closed) {
return cb(new Error('Pool is closed.'));
Expand All @@ -69,27 +70,29 @@ class Pool extends EventEmitter {
}
if (
this.config.queueLimit &&
this._connectionQueue.length >= this.config.queueLimit
this._connectionQueue.size() >= this.config.queueLimit
) {
return cb(new Error('Queue limit reached.'));
}
this.emit('enqueue');
return this._connectionQueue.push(cb);
return this._connectionQueue.pushBack(cb);
}

releaseConnection(connection) {
let cb;
if (!connection._pool) {
// The connection has been removed from the pool and is no longer good.
if (this._connectionQueue.length) {
cb = this._connectionQueue.shift();
if (!this._connectionQueue.empty()) {
cb = this._connectionQueue.front();
this._connectionQueue.popFront();
process.nextTick(this.getConnection.bind(this, cb));
}
} else if (this._connectionQueue.length) {
cb = this._connectionQueue.shift();
} else if (!this._connectionQueue.empty()) {
cb = this._connectionQueue.front();
this._connectionQueue.popFront();
process.nextTick(cb.bind(null, null, connection));
} else {
this._freeConnections.push(connection);
this._freeConnections.pushBack(connection);
this.emit('release', connection);
}
}
Expand All @@ -110,18 +113,19 @@ class Pool extends EventEmitter {
if (calledBack) {
return;
}
if (err || ++closedConnections >= this._allConnections.length) {
if (err || ++closedConnections >= this._allConnections.size()) {
calledBack = true;
cb(err);
return;
}
}.bind(this);
if (this._allConnections.length === 0) {
if (this._allConnections.empty()) {
endCB();
return;
}
for (let i = 0; i < this._allConnections.length; i++) {
connection = this._allConnections.get(i);
const length = this._allConnections.size();
for (let i = 0; i < length; i++) {
connection = this._allConnections.getElementByPos(i);
connection._realEnd(endCB);
}
}
Expand Down
25 changes: 11 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@
"dependencies": {
"@types/chai": "^4.3.3",
"chai": "^4.3.6",
"denque": "^2.0.1",
"generate-function": "^2.3.1",
"iconv-lite": "^0.6.3",
"js-sdsl": "^4.1.4",
"long": "^4.0.0",
"lru-cache": "^6.0.0",
"mocha": "^10.0.0",
Expand Down