Skip to content

Commit b7ed668

Browse files
committed
perf: ⚡️ use new BufferList
1 parent aafcb6d commit b7ed668

25 files changed

+73
-39
lines changed

benchmarks/bench.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const Benchmark = require('benchmark');
2+
const packets = require('./packets');
3+
const {MqttDecoder} = require('../es6/MqttDecoder');
4+
const mqtt = require('mqtt-packet');
5+
6+
const suite = new Benchmark.Suite;
7+
8+
suite
9+
.add(`mqtt-codec`, function() {
10+
const decoder = new MqttDecoder();
11+
for (const name of Object.keys(packets)) {
12+
decoder.push(Buffer.from(packets[name]));
13+
decoder.parse();
14+
}
15+
})
16+
.add(`mqtt-packet`, function() {
17+
const parser = mqtt.parser();
18+
for (const name of Object.keys(packets)) {
19+
parser.parse(Buffer.from(packets[name]));
20+
}
21+
})
22+
.on('cycle', function(event) {
23+
console.log(String(event.target));
24+
})
25+
.on('complete', function() {
26+
console.log('Fastest is ' + this.filter('fastest').map('name'));
27+
})
28+
.run();

benchmarks/test.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@
3333
"release": "semantic-release"
3434
},
3535
"keywords": [],
36-
"dependencies": {
37-
"bl": "^4.0.3"
38-
},
36+
"dependencies": {},
3937
"peerDependencies": {
4038
"tslib": "2"
4139
},

src/BufferList.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ export class BufferList {
1313
public consume(size: number) {
1414
this.offset += size;
1515
this.length -= size;
16-
while (this.offset >= this.list[0].length) {
16+
if (this.length < 0) {
17+
this.list = [];
18+
this.offset = 0;
19+
this.length = 0;
20+
return;
21+
}
22+
while (this.list.length && this.offset >= this.list[0].length) {
1723
this.offset -= this.list[0].length;
1824
this.list.shift();
1925
}

src/MqttDecoder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import BufferList from 'bl';
1+
import {BufferList} from './BufferList';
22
import {ERROR, PACKET_TYPE} from './enums';
33
import {PacketConnack, parseConnack} from './packets/connack';
44
import {PacketConnect, parseConnect} from './packets/connect';

src/__tests__/BufferList.spec.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,22 @@ it('can consume bytes in multiple blocks', () => {
9494
expect(list.readUInt8(0)).toBe(3);
9595
});
9696

97-
it('consuming out-of-bounds throws error', () => {
97+
it('consuming out-of-bounds is a no-op', () => {
9898
const list = new BufferList();
9999
list.append(Buffer.from([1, 2, 3]));
100+
expect(list.length).toBe(3);
101+
list.consume(1);
102+
expect(list.length).toBe(2);
103+
list.consume(1);
104+
expect(list.length).toBe(1);
105+
list.consume(1);
106+
expect(list.length).toBe(0);
100107
list.consume(1);
108+
expect(list.length).toBe(0);
101109
list.consume(1);
102-
expect(() => list.consume(1)).toThrowError();
110+
expect(list.length).toBe(0);
111+
list.consume(10);
112+
expect(list.length).toBe(0);
103113
});
104114

105115
it('can read ui16', () => {
@@ -126,7 +136,8 @@ it('can read ui16 from adjacent buffers', () => {
126136
list.consume(1);
127137
expect(list.readUInt16BE(0)).toBe(256);
128138
list.consume(1);
129-
expect(() => list.consume(1)).toThrowError();
139+
list.consume(123);
140+
expect(list.length).toBe(0);
130141
});
131142

132143
it('can read ui32 from adjacent buffers', () => {

src/__tests__/MqttDecoder.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {MqttDecoder} from '../MqttDecoder';
2-
import {connect, connectAck, connectWithClientId, publish3111, subscribe, subscribeAck} from './util';
2+
import {connect, connectAck, connectWithClientId, publish3111} from './util';
33
import {ERROR, PACKET_TYPE, PROPERTY} from '../enums';
44
import {PacketConnect} from '../packets/connect';
55
import {PacketConnack} from '../packets/connack';

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export const hello = () => 'world';
1+
export * from './types';
2+
export * from './MqttDecoder';

src/packets/auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import BufferList from 'bl';
1+
import {BufferList} from '../BufferList';
22
import {Packet, PacketHeaderData} from '../packet';
33
import {Properties} from '../types';
44
import {parseProps} from '../util/parse';

src/packets/connack.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import BufferList from 'bl';
1+
import {BufferList} from '../BufferList';
22
import {Packet, PacketHeaderData} from '../packet';
3-
import {Properties, QoS} from '../types';
4-
import {parseProps, parseBinary} from '../util/parse';
3+
import {Properties} from '../types';
4+
import {parseProps} from '../util/parse';
55

66
export interface PacketConnackData extends PacketHeaderData {
77
/** Connect Acknowledge Flags. */

0 commit comments

Comments
 (0)