Beta 7 with some repo names changed.
This commit is contained in:
1 parent
0f7b40cca7
commit
963c19493a
3408 files changed
+462217
-15516
No files matched your search
+57
@@ -0,0 +1,57 @@
|
||||
var N = 1e7
|
||||
var M = 10
|
||||
/*
|
||||
benchmark encoding and decoding N random integers.
|
||||
|
||||
A number is encoded into a buffer, (the buffer is reused so
|
||||
that allocation does not affect the benchmark)
|
||||
|
||||
to test the effect on performance of invalid records
|
||||
(i.e. too short, with the Most Significant Byte missing)
|
||||
every M items, attempt to decode from a shorter slice of the buffer.
|
||||
This will probably be produce an invalid result. We do not
|
||||
need to write into that buffer - because it refurs to the same memory as
|
||||
the full size buffer.
|
||||
|
||||
run with INVALID=1 to include N/M invalid decodes.
|
||||
|
||||
results:
|
||||
with no invalid decodes, I get about 2428 decodes/ms
|
||||
with invalid decodes:
|
||||
old code that overruns buffer: 1122 decodes/ms
|
||||
check length & return undefined: 2439 decodecs/ms
|
||||
check length & return NaN: 2434 d/ms
|
||||
check length & return -1: 2400 d/ms
|
||||
|
||||
conclusion, it doesn't make a significant difference whether
|
||||
what is returned to show an invalid read,
|
||||
but if you overrun the buffer the cost is considerable.
|
||||
|
||||
recomendation: return undefined
|
||||
*/
|
||||
|
||||
var buffer = new Buffer(8)
|
||||
var _buffer = buffer.slice(0, 4)
|
||||
var varint = require('./')
|
||||
var l = N
|
||||
var invalid = 0
|
||||
|
||||
includeInvalid = !!process.env.INVALID
|
||||
|
||||
var start = Date.now()
|
||||
while (l--) {
|
||||
var int = Math.floor(Math.random()*0x01fffffffffffff)
|
||||
varint.encode(int, buffer, 0)
|
||||
//console.log(int, varint.decode(buffer, 0))
|
||||
//every 1000 varints, do one that will be too short,
|
||||
//measure
|
||||
if(includeInvalid && !(l%M)) {
|
||||
if(undefined == varint.decode(_buffer, 0))
|
||||
invalid ++
|
||||
} else
|
||||
if(int !== varint.decode(buffer, 0))
|
||||
throw new Error('decode was incorrect')
|
||||
}
|
||||
|
||||
console.log('decode&encode/ms, invalidDecodes')
|
||||
console.log(N/(Date.now() - start) + ',', invalid)
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
module.exports = read
|
||||
|
||||
var MSB = 0x80
|
||||
, REST = 0x7F
|
||||
|
||||
function read(buf, offset) {
|
||||
var res = 0
|
||||
, offset = offset || 0
|
||||
, shift = 0
|
||||
, counter = offset
|
||||
, b
|
||||
, l = buf.length
|
||||
|
||||
do {
|
||||
if (counter >= l) {
|
||||
read.bytes = 0
|
||||
throw new RangeError('Could not decode varint')
|
||||
}
|
||||
b = buf[counter++]
|
||||
res += shift < 28
|
||||
? (b & REST) << shift
|
||||
: (b & REST) * Math.pow(2, shift)
|
||||
shift += 7
|
||||
} while (b >= MSB)
|
||||
|
||||
read.bytes = counter - offset
|
||||
|
||||
return res
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
module.exports = encode
|
||||
|
||||
var MSB = 0x80
|
||||
, REST = 0x7F
|
||||
, MSBALL = ~REST
|
||||
, INT = Math.pow(2, 31)
|
||||
|
||||
function encode(num, out, offset) {
|
||||
out = out || []
|
||||
offset = offset || 0
|
||||
var oldOffset = offset
|
||||
|
||||
while(num >= INT) {
|
||||
out[offset++] = (num & 0xFF) | MSB
|
||||
num /= 128
|
||||
}
|
||||
while(num & MSBALL) {
|
||||
out[offset++] = (num & 0xFF) | MSB
|
||||
num >>>= 7
|
||||
}
|
||||
out[offset] = num | 0
|
||||
|
||||
encode.bytes = offset - oldOffset + 1
|
||||
|
||||
return out
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
module.exports = {
|
||||
encode: require('./encode.js')
|
||||
, decode: require('./decode.js')
|
||||
, encodingLength: require('./length.js')
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
|
||||
var N1 = Math.pow(2, 7)
|
||||
var N2 = Math.pow(2, 14)
|
||||
var N3 = Math.pow(2, 21)
|
||||
var N4 = Math.pow(2, 28)
|
||||
var N5 = Math.pow(2, 35)
|
||||
var N6 = Math.pow(2, 42)
|
||||
var N7 = Math.pow(2, 49)
|
||||
var N8 = Math.pow(2, 56)
|
||||
var N9 = Math.pow(2, 63)
|
||||
|
||||
module.exports = function (value) {
|
||||
return (
|
||||
value < N1 ? 1
|
||||
: value < N2 ? 2
|
||||
: value < N3 ? 3
|
||||
: value < N4 ? 4
|
||||
: value < N5 ? 5
|
||||
: value < N6 ? 6
|
||||
: value < N7 ? 7
|
||||
: value < N8 ? 8
|
||||
: value < N9 ? 9
|
||||
: 10
|
||||
)
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
{
|
||||
"_from": "varint@^5.0.0",
|
||||
"_id": "varint@5.0.2",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-lKxKYG6H03yCZUpAGOPOsMcGxd1RHCu1iKvEHYDPmTyq2HueGhD73ssNBqqQWfvYs04G9iUFRvmAVLW20Jw6ow==",
|
||||
"_location": "/varint",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "range",
|
||||
"registry": true,
|
||||
"raw": "varint@^5.0.0",
|
||||
"name": "varint",
|
||||
"escapedName": "varint",
|
||||
"rawSpec": "^5.0.0",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "^5.0.0"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/cids/multicodec",
|
||||
"/multicodec",
|
||||
"/multihashes"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/varint/-/varint-5.0.2.tgz",
|
||||
"_shasum": "5b47f8a947eb668b848e034dcfa87d0ff8a7f7a4",
|
||||
"_spec": "varint@^5.0.0",
|
||||
"_where": "C:\\Users\\Luis\\source\\repos\\AAMasters\\BackendServers\\node_modules\\cids\\node_modules\\multicodec",
|
||||
"author": {
|
||||
"name": "Chris Dickinson",
|
||||
"email": "chris@neversaw.us"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/chrisdickinson/varint/issues"
|
||||
},
|
||||
"bundleDependencies": false,
|
||||
"deprecated": false,
|
||||
"description": "protobuf-style varint bytes - use msb to create integer values of varying sizes",
|
||||
"devDependencies": {
|
||||
"tape": "~2.12.3"
|
||||
},
|
||||
"homepage": "https://github.com/chrisdickinson/varint#readme",
|
||||
"keywords": [
|
||||
"varint",
|
||||
"protobuf",
|
||||
"encode",
|
||||
"decode"
|
||||
],
|
||||
"license": "MIT",
|
||||
"main": "index.js",
|
||||
"name": "varint",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/chrisdickinson/varint.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "node test.js"
|
||||
},
|
||||
"version": "5.0.2"
|
||||
}
|
||||
+139
@@ -0,0 +1,139 @@
|
||||
var varint = require('./index')
|
||||
, test = require('tape')
|
||||
, decode = varint.decode
|
||||
, encode = varint.encode
|
||||
, encodingLength = varint.encodingLength
|
||||
|
||||
test('fuzz test', function(assert) {
|
||||
var expect
|
||||
, encoded
|
||||
|
||||
for(var i = 0, len = 100; i < len; ++i) {
|
||||
expect = randint(0x7FFFFFFF)
|
||||
encoded = encode(expect)
|
||||
var data = decode(encoded)
|
||||
assert.equal(expect, data, 'fuzz test: ' + expect.toString())
|
||||
assert.equal(decode.bytes, encoded.length)
|
||||
}
|
||||
|
||||
assert.end()
|
||||
})
|
||||
|
||||
test('test single byte works as expected', function(assert) {
|
||||
var buf = new Uint8Array(2)
|
||||
buf[0] = 172
|
||||
buf[1] = 2
|
||||
var data = decode(buf)
|
||||
assert.equal(data, 300, 'should equal 300')
|
||||
assert.equal(decode.bytes, 2)
|
||||
assert.end()
|
||||
})
|
||||
|
||||
test('test encode works as expected', function(assert) {
|
||||
var out = []
|
||||
|
||||
assert.deepEqual(encode(300), [0xAC, 0x02])
|
||||
|
||||
assert.end()
|
||||
})
|
||||
|
||||
test('test decode single bytes', function(assert) {
|
||||
var expected = randint(parseInt('1111111', '2'))
|
||||
var buf = new Uint8Array(1)
|
||||
buf[0] = expected
|
||||
var data = decode(buf)
|
||||
assert.equal(data, expected)
|
||||
assert.equal(decode.bytes, 1)
|
||||
assert.end()
|
||||
})
|
||||
|
||||
test('test decode multiple bytes with zero', function(assert) {
|
||||
var expected = randint(parseInt('1111111', '2'))
|
||||
var buf = new Uint8Array(2)
|
||||
buf[0] = 128
|
||||
buf[1] = expected
|
||||
var data = decode(buf)
|
||||
assert.equal(data, expected << 7)
|
||||
assert.equal(decode.bytes, 2)
|
||||
assert.end()
|
||||
})
|
||||
|
||||
test('encode single byte', function(assert) {
|
||||
var expected = randint(parseInt('1111111', '2'))
|
||||
assert.deepEqual(encode(expected), [expected])
|
||||
assert.equal(encode.bytes, 1)
|
||||
assert.end()
|
||||
})
|
||||
|
||||
test('encode multiple byte with zero first byte', function(assert) {
|
||||
var expected = 0x0F00
|
||||
assert.deepEqual(encode(expected), [0x80, 0x1E])
|
||||
assert.equal(encode.bytes, 2)
|
||||
assert.end()
|
||||
})
|
||||
|
||||
test('big integers', function (assert) {
|
||||
|
||||
var bigs = []
|
||||
for(var i = 32; i <= 53; i++) (function (i) {
|
||||
bigs.push(Math.pow(2, i) - 1)
|
||||
bigs.push(Math.pow(2, i))
|
||||
})(i)
|
||||
|
||||
bigs.forEach(function (n) {
|
||||
var data = encode(n)
|
||||
console.error(n, '->', data)
|
||||
assert.equal(decode(data), n)
|
||||
assert.notEqual(decode(data), n - 1)
|
||||
})
|
||||
assert.end()
|
||||
})
|
||||
|
||||
test('fuzz test - big', function(assert) {
|
||||
var expect
|
||||
, encoded
|
||||
|
||||
var MAX_INTD = Math.pow(2, 55)
|
||||
var MAX_INT = Math.pow(2, 31)
|
||||
|
||||
for(var i = 0, len = 100; i < len; ++i) {
|
||||
expect = randint(MAX_INTD - MAX_INT) + MAX_INT
|
||||
encoded = encode(expect)
|
||||
var data = decode(encoded)
|
||||
assert.equal(expect, data, 'fuzz test: ' + expect.toString())
|
||||
assert.equal(decode.bytes, encoded.length)
|
||||
}
|
||||
|
||||
assert.end()
|
||||
})
|
||||
|
||||
test('encodingLength', function (assert) {
|
||||
|
||||
for(var i = 0; i <= 53; i++) {
|
||||
var n = Math.pow(2, i)
|
||||
assert.equal(encode(n).length, encodingLength(n))
|
||||
}
|
||||
|
||||
assert.end()
|
||||
})
|
||||
|
||||
test('buffer too short', function (assert) {
|
||||
|
||||
var value = encode(9812938912312)
|
||||
var buffer = encode(value)
|
||||
|
||||
var l = buffer.length
|
||||
while(l--) {
|
||||
try {
|
||||
var val = decode(buffer.slice(0, l))
|
||||
} catch (err) {
|
||||
assert.equal(err.constructor, RangeError)
|
||||
assert.equal(decode.bytes, 0)
|
||||
}
|
||||
}
|
||||
assert.end()
|
||||
})
|
||||
|
||||
function randint(range) {
|
||||
return Math.floor(Math.random() * range)
|
||||
}
|
||||
Reference in new issue
Block a user