Skip to content

Commit

Permalink
Generation speedup by disabling safety checks.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Sep 13, 2015
1 parent 52a5cb1 commit d378199
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
3 changes: 2 additions & 1 deletion benchmarks/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ var mqtt = require('../')
, i
, start = Date.now()
, time
, buf = new Buffer('test')

for (i = 0; i < max; i++) {
mqtt.generate({
cmd: 'publish'
, topic: 'test'
, payload: 'test'
, payload: buf
})
}

Expand Down
38 changes: 19 additions & 19 deletions generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,16 @@ function connect(opts) {

length += clientId.length + 2
} else {

if(protocolVersion < 4) {

throw new Error('clientId must be supplied before 3.1.1');
}

if(clean == 0) {

throw new Error('clientId must be given if cleanSession set to 0');
}
}
}

// Must be a two byte number
Expand Down Expand Up @@ -152,14 +152,14 @@ function connect(opts) {
, pos = 0

// Generate header
buffer.writeUInt8(protocol.codes['connect'] << protocol.CMD_SHIFT, pos++)
buffer.writeUInt8(protocol.codes['connect'] << protocol.CMD_SHIFT, pos++, true)

// Generate length
pos += writeLength(buffer, pos, length)

// Generate protocol ID
pos += writeStringOrBuffer(buffer, pos, protocolId)
buffer.writeUInt8(protocolVersion, pos++)
buffer.writeUInt8(protocolVersion, pos++, true)

// Connect flags
var flags = 0
Expand All @@ -171,7 +171,7 @@ function connect(opts) {
flags |= will ? protocol.WILL_FLAG_MASK : 0
flags |= clean ? protocol.CLEAN_SESSION_MASK : 0

buffer.writeUInt8(flags, pos++)
buffer.writeUInt8(flags, pos++, true)

// Keepalive
pos += writeNumber(buffer, pos, keepalive)
Expand Down Expand Up @@ -206,10 +206,10 @@ function connack(opts) {
var buffer = new Buffer(4)
, pos = 0;

buffer.writeUInt8(protocol.codes['connack'] << protocol.CMD_SHIFT, pos++);
buffer.writeUInt8(protocol.codes['connack'] << protocol.CMD_SHIFT, pos++, true);
pos += writeLength(buffer, pos, 2);
buffer.writeUInt8(opts.sessionPresent && protocol.SESSIONPRESENT_MASK || 0, pos++);
buffer.writeUInt8(rc, pos++);
buffer.writeUInt8(opts.sessionPresent && protocol.SESSIONPRESENT_MASK || 0, pos++, true);
buffer.writeUInt8(rc, pos++, true);

return buffer;
}
Expand Down Expand Up @@ -251,11 +251,11 @@ function publish(opts) {
, pos = 0;

// Header
buffer[pos++] =
buffer.writeUInt8(
protocol.codes['publish'] << protocol.CMD_SHIFT |
dup |
qos << protocol.QOS_SHIFT |
retain;
retain, pos++, true);

// Remaining length
pos += writeLength(buffer, pos, length);
Expand Down Expand Up @@ -354,7 +354,7 @@ function subscribe(opts) {
buffer.writeUInt8(
protocol.codes['subscribe'] << protocol.CMD_SHIFT |
dup |
1 << protocol.QOS_SHIFT, pos++);
1 << protocol.QOS_SHIFT, pos++, true);

// Generate length
pos += writeLength(buffer, pos, length);
Expand All @@ -371,7 +371,7 @@ function subscribe(opts) {
// Write topic string
pos += writeString(buffer, pos, topic);
// Write qos
buffer.writeUInt8(qos, pos++);
buffer.writeUInt8(qos, pos++, true);
}

return buffer;
Expand Down Expand Up @@ -406,7 +406,7 @@ function suback(opts) {
, pos = 0;

// Header
buffer.writeUInt8(protocol.codes['suback'] << protocol.CMD_SHIFT, pos++);
buffer.writeUInt8(protocol.codes['suback'] << protocol.CMD_SHIFT, pos++, true);

// Length
pos += writeLength(buffer, pos, length);
Expand All @@ -416,7 +416,7 @@ function suback(opts) {

// Subscriptions
for (var i = 0; i < granted.length; i++) {
buffer.writeUInt8(granted[i], pos++);
buffer.writeUInt8(granted[i], pos++, true);
}

return buffer;
Expand Down Expand Up @@ -519,7 +519,7 @@ function writeLength(buffer, pos, length) {
if (length > 0) {
digit = digit | 0x80
}
buffer.writeUInt8(digit, pos++)
buffer.writeUInt8(digit, pos++, true)
} while (length > 0)

return pos - origPos
Expand Down Expand Up @@ -576,8 +576,8 @@ function writeBuffer(buffer, pos, src) {
* @api private
*/
function writeNumber(buffer, pos, number) {
buffer.writeUInt8(number >> 8, pos)
buffer.writeUInt8(number & 0x00FF, pos + 1)
buffer.writeUInt8(number >> 8, pos, true)
buffer.writeUInt8(number & 0x00FF, pos + 1, true)

return 2
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"devDependencies": {
"faucet": "0.0.1",
"pre-commit": "0.0.9",
"tape": "^2.14.0"
"tape": "^4.2.0"
},
"dependencies": {
"bl": "^0.9.1",
Expand Down

0 comments on commit d378199

Please sign in to comment.