Skip to content

Commit

Permalink
Benchmark binary frame and JavaScript checksum.
Browse files Browse the repository at this point in the history
JavaScript DJB is faster than `crypto` SHA1. The binary frame is faster
than the UTF8 frame, of course. There is still room for improvement in
read performance by calling `splice` directly on the items, or at least
removing the invocation of `reduce` in `Page.splice`.

insert: 61, gather: 36

See #470.
  • Loading branch information
flatheadmill committed Feb 12, 2015
1 parent 84afeea commit ccf0940
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 9 deletions.
7 changes: 6 additions & 1 deletion benchmark/djb.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ function djb (block, start, end) {
for (var i = start; i < end; i++) {
seed = (seed * 33 + block[i]) >>> 0
}
return seed
return new Buffer([
seed >>> 24 & 0xff,
seed >>> 16 & 0xff,
seed >>> 8 & 0xff,
seed & 0xff
])
}
module.exports = djb
6 changes: 3 additions & 3 deletions benchmark/fnv.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ function fnv (block, start, end) {
var hash = (0 ^ 2166136261) >>> 0
for (var i = start; i < end; i++) {
hash = (hash ^ block[i]) >>> 0
hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24)
hash = hash >>> 0
hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24) >>> 0
// hash = hash >>> 0
}
var buffer = new Buffer(4)
buffer.writeUInt32LE(hash, 0)
buffer.writeUInt32LE(hash, 0, true)
return buffer
}
module.exports = fnv
7 changes: 6 additions & 1 deletion benchmark/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ var seedrandom = require('seedrandom')
var rimraf = require('rimraf')
var mkdirp = require('mkdirp')
var Strata = require('..')
var djb = require('./djb')
var murmur3 = require('./murmur3')
var fnv = require('./fnv')

var random = (function () {
var random = seedrandom(0)
Expand All @@ -29,12 +32,14 @@ var random = (function () {

var runner = cadence(function (async) {
var start, insert, gather
var Binary = require('../frame/binary')
var directory = path.join(__dirname, 'tmp'), db, count = 0
var strata = new Strata({
directory: directory,
leafSize: 256,
branchSize: 256,
writeStage: 'leaf'
writeStage: 'leaf',
framer: new Binary(djb)
})

var batches = []
Expand Down
9 changes: 6 additions & 3 deletions benchmark/murmur3.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,12 @@ function murmur (buffer, start, end) {

hash = fmix32(hash) >>> 0

var buffer = new Buffer(4)
buffer.writeUInt32LE(hash, 0, true)
return buffer
return new Buffer([
hash >>> 24 & 0xff,
hash >>> 16 & 0xff,
hash >>> 8 & 0xff,
hash & 0xff
])
}

module.exports = murmur
2 changes: 1 addition & 1 deletion frame/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Binary.prototype.deserialize = function (deserialize, buffer, offset, length) {
if (checksum != null) {
var digest = checksum(buffer, offset + this.checksumLength, end)
for (var i = 0, I = digest.length; i < I; i++) {
if (buffer[offset++] != digest[i]) {
if (buffer[offset++] !== digest[i]) {
throw new Error('invalid checksum')
}
}
Expand Down
1 change: 1 addition & 0 deletions player.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Player.prototype._play = function (sheaf, slice, start, page) {
ok(header[0] === ++page.entries, 'entry count is off')
var index = header[1]
if (leaf) {
// todo: see if it is faster to perform the slices here directly.
if (index > 0) {
page.splice(index - 1, 0, {
key: sheaf.extractor(entry.body),
Expand Down

0 comments on commit ccf0940

Please sign in to comment.