Skip to content
This repository was archived by the owner on Apr 29, 2020. It is now read-only.

Commit 4e5b618

Browse files
authored
perf: switch out pull-block for bl (#12)
1 parent aa1b5ad commit 4e5b618

File tree

5 files changed

+56
-7
lines changed

5 files changed

+56
-7
lines changed

.aegir.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict'
2+
3+
module.exports = {
4+
karma: {
5+
browserNoActivityTimeout: 100 * 1000
6+
}
7+
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
},
3838
"homepage": "https://github.com/ipfs/js-ipfs-unixfs-importer#readme",
3939
"devDependencies": {
40-
"aegir": "^17.0.0",
40+
"aegir": "^18.0.2",
4141
"chai": "^4.2.0",
4242
"detect-node": "^2.0.4",
4343
"dirty-chai": "^2.0.1",
@@ -52,6 +52,7 @@
5252
"dependencies": {
5353
"async": "^2.6.1",
5454
"async-iterator-to-pull-stream": "^1.1.0",
55+
"bl": "^2.1.2",
5556
"cids": "~0.5.5",
5657
"deep-extend": "~0.6.0",
5758
"hamt-sharding": "~0.0.2",
@@ -60,7 +61,6 @@
6061
"left-pad": "^1.3.0",
6162
"multihashing-async": "~0.5.1",
6263
"pull-batch": "^1.0.0",
63-
"pull-block": "^1.4.0",
6464
"pull-pair": "^1.1.0",
6565
"pull-paramap": "^1.2.2",
6666
"pull-pause": "0.0.2",

src/chunker/fixed-size.js

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,50 @@
11
'use strict'
22

3-
const pullBlock = require('pull-block')
3+
const BufferList = require('bl')
4+
const through = require('pull-through')
45

56
module.exports = (options) => {
67
let maxSize = (typeof options === 'number') ? options : options.maxChunkSize
7-
return pullBlock(maxSize, { zeroPadding: false, emitEmpty: true })
8+
let bl = new BufferList()
9+
let currentLength = 0
10+
let emitted = false
11+
12+
return through(
13+
function onData (buffer) {
14+
bl.append(buffer)
15+
16+
currentLength += buffer.length
17+
18+
while (currentLength >= maxSize) {
19+
this.queue(bl.slice(0, maxSize))
20+
21+
emitted = true
22+
23+
// throw away consumed bytes
24+
if (maxSize === bl.length) {
25+
bl = new BufferList()
26+
currentLength = 0
27+
} else {
28+
const newBl = new BufferList()
29+
newBl.append(bl.shallowSlice(maxSize))
30+
bl = newBl
31+
32+
// update our offset
33+
currentLength -= maxSize
34+
}
35+
}
36+
},
37+
function onEnd () {
38+
if (currentLength) {
39+
this.queue(bl.slice(0, currentLength))
40+
emitted = true
41+
}
42+
43+
if (!emitted) {
44+
this.queue(Buffer.alloc(0))
45+
}
46+
47+
this.queue(null)
48+
}
49+
)
850
}

test/chunker-fixed-size.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ describe('chunker: fixed size', function () {
6666
const KiB256 = 262144
6767

6868
pull(
69-
values(rawFile),
69+
values([rawFile]),
7070
chunker(KiB256),
7171
collect((err, chunks) => {
7272
expect(err).to.not.exist()
@@ -85,7 +85,7 @@ describe('chunker: fixed size', function () {
8585
let file = Buffer.concat([rawFile, Buffer.from('hello')])
8686

8787
pull(
88-
values(file),
88+
values([file]),
8989
chunker(KiB256),
9090
collect((err, chunks) => {
9191
expect(err).to.not.exist()

test/import-export.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ describe('import and export', function () {
5959
const path = strategy + '-big.dat'
6060

6161
pull(
62-
values([{ path: path, content: values(bigFile) }]),
62+
values([{ path: path, content: values([bigFile]) }]),
6363
importer(ipld, importerOptions),
6464
map((file) => {
6565
expect(file.path).to.eql(path)

0 commit comments

Comments
 (0)