Skip to content

Commit f0812c4

Browse files
authored
Merge pull request #1401 from testn/remove-slice
Optimize string decoding by removing the use of slice()
2 parents 3e5d32f + 1336ff0 commit f0812c4

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

lib/packets/column_definition.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ class ColumnDefinition {
4545
this.characterSet = packet.readInt16();
4646
this.encoding = CharsetToEncoding[this.characterSet];
4747
this.name = StringParser.decode(
48-
this._buf.slice(_nameStart, _nameStart + _nameLength),
49-
this.encoding === 'binary' ? this._clientEncoding : this.encoding
48+
this._buf,
49+
this.encoding === 'binary' ? this._clientEncoding : this.encoding,
50+
_nameStart,
51+
_nameStart + _nameLength
5052
);
5153
this.columnLength = packet.readInt32();
5254
this.columnType = packet.readInt8();
@@ -113,8 +115,10 @@ const addString = function(name) {
113115
const start = this[`_${name}Start`];
114116
const end = start + this[`_${name}Length`];
115117
const val = StringParser.decode(
116-
this._buf.slice(start, end),
117-
this.encoding === 'binary' ? this._clientEncoding : this.encoding
118+
this._buf,
119+
this.encoding === 'binary' ? this._clientEncoding : this.encoding,
120+
start,
121+
end
118122
);
119123

120124
Object.defineProperty(this, name, {

lib/packets/packet.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,10 @@ class Packet {
387387
// TODO: Use characterSetCode to get proper encoding
388388
// https://github.com/sidorares/node-mysql2/pull/374
389389
return StringParser.decode(
390-
this.buffer.slice(this.offset - len, this.offset),
391-
encoding
390+
this.buffer,
391+
encoding,
392+
this.offset - len,
393+
this.offset
392394
);
393395
}
394396

@@ -407,7 +409,7 @@ class Packet {
407409
end = end + 1; // TODO: handle OOB check
408410
}
409411
this.offset = end + 1;
410-
return StringParser.decode(this.buffer.slice(start, end), encoding);
412+
return StringParser.decode(this.buffer, encoding, start, end);
411413
}
412414

413415
// TODO reuse?
@@ -421,8 +423,10 @@ class Packet {
421423
}
422424
this.offset += len;
423425
return StringParser.decode(
424-
this.buffer.slice(this.offset - len, this.offset),
425-
encoding
426+
this.buffer,
427+
encoding,
428+
this.offset - len,
429+
this.offset
426430
);
427431
}
428432

lib/parsers/string.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
const Iconv = require('iconv-lite');
44

5-
exports.decode = function(buffer, encoding, options) {
5+
exports.decode = function(buffer, encoding, start, end, options) {
66
if (Buffer.isEncoding(encoding)) {
7-
return buffer.toString(encoding);
7+
return buffer.toString(encoding, start, end);
88
}
99

1010
const decoder = Iconv.getDecoder(encoding, options || {});
1111

12-
const res = decoder.write(buffer);
12+
const res = decoder.write(buffer.slice(start, end));
1313
const trail = decoder.end();
1414

1515
return trail ? res + trail : res;

0 commit comments

Comments
 (0)