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

fix: replace node buffers with uint8arrays #51

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
4 changes: 3 additions & 1 deletion benchmarks/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* eslint-disable no-console */
'use strict'

const uint8ArrayFromString = require('uint8arrays/from-string')

console.log('PID: %s', process.pid)

const Benchmark = require('benchmark')
Expand All @@ -21,7 +23,7 @@ const parsed = vectors

const buffers = vectors
.filter((v) => v.hex && v.decoded)
.map((v) => Buffer.from(v.hex, 'hex'))
.map((v) => uint8ArrayFromString(v.hex, 'base16'))

const suite = new Benchmark.Suite('cbor')

Expand Down
21,281 changes: 21,281 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,18 @@
"benchmark": "^2.1.0",
"budo": "^11.2.0",
"cbor": "^4.0.0",
"chai": "^4.1.2",
"dirty-chai": "^2.0.1",
"garbage": "0.0.0"
},
"license": "MIT",
"readmeFilename": "README.md",
"dependencies": {
"bignumber.js": "^9.0.0",
"buffer": "^5.5.0",
"commander": "^2.15.0",
"ieee754": "^1.1.13",
"iso-url": "~0.4.7",
"json-text-sequence": "~0.1.0",
"readable-stream": "^3.6.0"
"readable-stream": "^3.6.0",
"uint8arrays": "^1.1.0"
},
"engines": {
"node": ">=4"
Expand Down
20 changes: 10 additions & 10 deletions src/commented.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* eslint-disable */
'use strict'

const { Buffer } = require('buffer')
const stream = require('readable-stream')
const Decoder = require('./decoder')
const constants = require('./constants')
const bignumber = require('bignumber.js').BigNumber
const uint8ArrayToString = require('uint8arrays/to-string')

const MT = constants.MT

Expand Down Expand Up @@ -79,11 +79,11 @@ class Commented extends stream.Transform {
*/

/**
* Comment on an input Buffer or string, creating a string passed to the
* Comment on an input Uint8Array or string, creating a string passed to the
* callback. If callback not specified, a promise is returned.
*
* @static
* @param {(string|Buffer|NoFilter)} input
* @param {(string|Uint8Array|NoFilter)} input
* @param {(string|object|function)} options
* @param {number} [options.max_depth=10] - how many times to indent the dashes
* @param {commentCallback=} cb
Expand All @@ -93,7 +93,7 @@ class Commented extends stream.Transform {
if (input == null) {
throw new Error('input required')
}
let encoding = (typeof input === 'string') ? 'hex' : void 0
let encoding = (typeof input === 'string') ? 'base16' : void 0
let max_depth = 10
switch (typeof options) {
case 'function':
Expand Down Expand Up @@ -122,13 +122,13 @@ class Commented extends stream.Transform {
let p = null
if (typeof cb === 'function') {
d.on('end', function () {
return cb(null, bs.toString('utf8'))
return cb(null, uint8ArrayToString(bs))
})
d.on('error', cb)
} else {
p = new Promise(function (resolve, reject) {
d.on('end', function () {
return resolve(bs.toString('utf8'))
return resolve(uint8ArrayToString(bs))
})
return d.on('error', reject)
})
Expand All @@ -152,7 +152,7 @@ class Commented extends stream.Transform {
*/
_on_read (buf) {
this.all.write(buf)
const hex = buf.toString('hex')
const hex = uint8ArrayToString(buf, 'base16')
this.push(new Array(this.depth + 1).join(' '))
this.push(hex)
let ind = (this.max_depth - this.depth) * 2
Expand Down Expand Up @@ -299,10 +299,10 @@ class Commented extends stream.Transform {
this.push(JSON.stringify(val))
this.push('\n')
}
} else if (Buffer.isBuffer(val)) {
} else if (val instanceof Uint8Array) {
this.depth--
if (val.length > 0) {
this.push(val.toString('hex'))
this.push(uint8ArrayToString(val, 'base16'))
this.push('\n')
}
} else if (val instanceof bignumber) {
Expand All @@ -327,7 +327,7 @@ class Commented extends stream.Transform {
*/
_on_data () {
this.push('0x')
this.push(this.all.read().toString('hex'))
this.push(uint8ArrayToString(this.all.read(), 'base16'))
return this.push('\n')
}
}
Expand Down
28 changes: 13 additions & 15 deletions src/decoder.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const { Buffer } = require('buffer')
const ieee754 = require('ieee754')
const Bignumber = require('bignumber.js').BigNumber

Expand All @@ -10,6 +9,9 @@ const c = require('./constants')
const Simple = require('./simple')
const Tagged = require('./tagged')
const { URL } = require('iso-url')
const uint8ArrayToString = require('uint8arrays/to-string')
const uint8ArrayFromString = require('uint8arrays/from-string')
const uint8ArrayConcat = require('uint8arrays/concat')

/**
* Transform binary cbor data into JavaScript objects.
Expand All @@ -31,8 +33,6 @@ class Decoder {

// Heap use to share the input with the parser
this._heap = new ArrayBuffer(opts.size)
this._heap8 = new Uint8Array(this._heap)
this._buffer = Buffer.from(this._heap)

this._reset()

Expand Down Expand Up @@ -266,15 +266,11 @@ class Decoder {
}

createByteString (raw, len) {
return Buffer.concat(raw)
return uint8ArrayConcat(raw)
}

createByteStringFromHeap (start, end) {
if (start === end) {
return Buffer.alloc(0)
}

return Buffer.from(this._heap.slice(start, end))
return new Uint8Array(this._heap, start, end - start)
}

createInt (val) {
Expand Down Expand Up @@ -357,7 +353,7 @@ class Decoder {
return ''
}

return this._buffer.toString('utf8', start, end)
return uint8ArrayToString(new Uint8Array(this._heap, start, end - start))
}

createSimpleUnassigned (val) {
Expand Down Expand Up @@ -550,7 +546,9 @@ class Decoder {
}

this._reset()
this._heap8.set(input)

new Uint8Array(this._heap).set(input)

const code = this.parser.parse(input.byteLength)

if (this._depth > 1) {
Expand Down Expand Up @@ -589,12 +587,12 @@ class Decoder {
* Decode the first cbor object.
*
* @param {Buffer|string} input
* @param {string} [enc='hex'] - Encoding used if a string is passed.
* @param {string} [enc='base16'] - Encoding used if a string is passed.
* @returns {*}
*/
static decode (input, enc) {
if (typeof input === 'string') {
input = Buffer.from(input, enc || 'hex')
input = uint8ArrayFromString(input, enc || 'base16')
}

const dec = new Decoder({ size: input.length })
Expand All @@ -605,12 +603,12 @@ class Decoder {
* Decode all cbor objects.
*
* @param {Buffer|string} input
* @param {string} [enc='hex'] - Encoding used if a string is passed.
* @param {string} [enc='base16'] - Encoding used if a string is passed.
* @returns {Array<*>}
*/
static decodeAll (input, enc) {
if (typeof input === 'string') {
input = Buffer.from(input, enc || 'hex')
input = uint8ArrayFromString(input, enc || 'base16')
}

const dec = new Decoder({ size: input.length })
Expand Down
16 changes: 8 additions & 8 deletions src/diagnose.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use strict'

const { Buffer } = require('buffer')
const Decoder = require('./decoder')
const utils = require('./utils')
const uint8ArrayFromString = require('uint8arrays/from-string')
const uint8ArrayToString = require('uint8arrays/to-string')

/**
* Output the diagnostic format from a stream of CBOR bytes.
Expand Down Expand Up @@ -70,9 +71,10 @@ class Diagnose extends Decoder {
}

createByteStringFromHeap (start, end) {
const val = (Buffer.from(
super.createByteStringFromHeap(start, end)
)).toString('hex')
const val = uint8ArrayToString(
super.createByteStringFromHeap(start, end),
'base16'
)

return `h'${val}'`
}
Expand Down Expand Up @@ -151,16 +153,14 @@ class Diagnose extends Decoder {
}

createUtf8StringFromHeap (start, end) {
const val = (Buffer.from(
super.createUtf8StringFromHeap(start, end)
)).toString('utf8')
const val = super.createUtf8StringFromHeap(start, end)

return `"${val}"`
}

static diagnose (input, enc) {
if (typeof input === 'string') {
input = Buffer.from(input, enc || 'hex')
input = uint8ArrayFromString(input, enc || 'base16')
}

const dec = new Diagnose()
Expand Down
48 changes: 25 additions & 23 deletions src/encoder.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict'

const { Buffer } = require('buffer')
const { URL } = require('iso-url')
const Bignumber = require('bignumber.js').BigNumber
const uint8ArrayFromString = require('uint8arrays/from-string')

const utils = require('./utils')
const constants = require('./constants')
Expand All @@ -20,9 +20,9 @@ const UNDEFINED = (constants.MT.SIMPLE_FLOAT << 5) | constants.SIMPLE.UNDEFINED
const NULL = (constants.MT.SIMPLE_FLOAT << 5) | constants.SIMPLE.NULL

const MAXINT_BN = new Bignumber('0x20000000000000')
const BUF_NAN = Buffer.from('f97e00', 'hex')
const BUF_INF_NEG = Buffer.from('f9fc00', 'hex')
const BUF_INF_POS = Buffer.from('f97c00', 'hex')
const BUF_NAN = uint8ArrayFromString('f97e00', 'base16')
const BUF_INF_NEG = uint8ArrayFromString('f9fc00', 'base16')
const BUF_INF_POS = uint8ArrayFromString('f97c00', 'base16')

function toType (obj) {
// [object Type]
Expand All @@ -37,7 +37,7 @@ function toType (obj) {
class Encoder {
/**
* @param {Object} [options={}]
* @param {function(Buffer)} options.stream
* @param {function(Uint8Array)} options.stream
*/
constructor (options) {
options = options || {}
Expand Down Expand Up @@ -132,18 +132,20 @@ class Encoder {
}

_pushFloat (obj) {
const b2 = Buffer.allocUnsafe(2)
const b2 = new Uint8Array(2)

if (utils.writeHalf(b2, obj)) {
if (utils.parseHalf(b2) === obj) {
return this._pushUInt8(HALF) && this.push(b2)
}
}

const b4 = Buffer.allocUnsafe(4)
b4.writeFloatBE(obj, 0)
if (b4.readFloatBE(0) === obj) {
return this._pushUInt8(FLOAT) && this.push(b4)
const buf = new ArrayBuffer(4)
const b4 = new DataView(buf, buf.byteOffset, buf.byteLength)
b4.setFloat32(0, obj)

if (b4.getFloat32(0) === obj) {
return this._pushUInt8(FLOAT) && this.push(new Uint8Array(b4.buffer, b4.byteOffset, b4.byteLength))
}

return this._pushUInt8(DOUBLE) && this._pushDoubleBE(obj)
Expand Down Expand Up @@ -202,7 +204,7 @@ class Encoder {
}

_pushString (obj) {
const len = Buffer.byteLength(obj, 'utf8')
const len = uint8ArrayFromString(obj).length
return this._pushInt(len, MT.UTF8_STRING) && this.pushWrite(obj, 5, len)
}

Expand Down Expand Up @@ -277,7 +279,7 @@ class Encoder {
if (str.length % 2) {
str = '0' + str
}
const buf = Buffer.from(str, 'hex')
const buf = uint8ArrayFromString(str, 'base16')
return this._pushTag(tag) && this._pushBuffer(this, buf)
}

Expand Down Expand Up @@ -404,7 +406,7 @@ class Encoder {
case 'Array':
return this._pushArray(this, obj)
case 'Uint8Array':
return this._pushBuffer(this, Buffer.isBuffer(obj) ? obj : Buffer.from(obj))
return this._pushBuffer(this, obj)
case 'Null':
return this._pushUInt8(NULL)
case 'Undefined':
Expand Down Expand Up @@ -454,7 +456,9 @@ class Encoder {
size += resultLength[i]
}

var res = Buffer.allocUnsafe(size)
Copy link

@mikeal mikeal Aug 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably where the perf difference is coming from.

There’s no equivalent to this outside of Node.js, so this change is moving us from a request for an already allocated piece of dirty memory to a new allocation of zero’d memory.

const buffer = new ArrayBuffer(size)
var view = new DataView(buffer, buffer.byteOffset, buffer.byteLength)
var bytes = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength)
var index = 0
var length = 0

Expand All @@ -464,22 +468,22 @@ class Encoder {

switch (resultMethod[i]) {
case 0:
result[i].copy(res, index)
bytes.set(result[i].subarray(0, length), index)
break
case 1:
res.writeUInt8(result[i], index, true)
view.setUint8(index, result[i])
break
case 2:
res.writeUInt16BE(result[i], index, true)
view.setUint16(index, result[i])
break
case 3:
res.writeUInt32BE(result[i], index, true)
view.setUint32(index, result[i])
break
case 4:
res.writeDoubleBE(result[i], index, true)
view.setFloat64(index, result[i])
break
case 5:
res.write(result[i], index, length, 'utf8')
bytes.set(uint8ArrayFromString(result[i].substring(0, length), 'utf8'), index)
break
default:
throw new Error('unkown method')
Expand All @@ -488,11 +492,9 @@ class Encoder {
index += length
}

var tmp = res

this._reset()

return tmp
return bytes
}

_reset () {
Expand Down
Loading