Skip to content

Commit

Permalink
adding entity.discard() to stop data events
Browse files Browse the repository at this point in the history
entity.discard() is a performance optimization for those using this library to only read specific files from the tar.
the parser looks at this property in the same manner it looks at _remaining to know when to skip calling write on the entity.

an entity which discard() has been called on will not emit any more data events.
discard() does not take any arguments and only sets the value of _discard to true.

this saves 100ms or so for me just `stating` all of the files in the npm v 3 tar
  • Loading branch information
soldair committed Aug 19, 2015
1 parent 2cbe6c8 commit 33ee078
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lib/entry.js
Expand Up @@ -24,6 +24,7 @@ function Entry (header, extended, global) {
this._ending = false
this._ended = false
this._remaining = 0
this._discard = false
this._queue = []
this._index = 0
this._queueLen = 0
Expand Down Expand Up @@ -209,5 +210,11 @@ Entry.prototype._setProps = function () {
this._remaining = props.size
}

// the parser may not call write if discard is true.
// useful for skipping data from some files quickly.
Entry.prototype.discard = function(){
this._discard = true
}

Entry.prototype.warn = fstream.warn
Entry.prototype.error = fstream.error
6 changes: 5 additions & 1 deletion lib/parse.js
Expand Up @@ -102,7 +102,11 @@ Parse.prototype._process = function (c) {

if (this._entry) {
var entry = this._entry
entry.write(c)
if(!entry._discard) entry.write(c)
else {
entry._remaining -= c.length
if(entry._remaining < 0) entry._remaining = 0
}
if (entry._remaining === 0) {
entry.end()
this._entry = null
Expand Down
29 changes: 29 additions & 0 deletions test/parse-discard.js
@@ -0,0 +1,29 @@
var tap = require("tap")
, tar = require("../tar.js")
, fs = require("fs")
, path = require("path")
, file = path.resolve(__dirname, "fixtures/c.tar")

tap.test("parser test", function (t) {
var parser = tar.Parse()
var total = 0;
var dataTotal = 0;

parser.on("end", function () {

t.equals(total-513,dataTotal,'should have discarded only c.txt')

t.end()
})

fs.createReadStream(file)
.pipe(parser)
.on('entry',function(entry){
if(entry.path === 'c.txt') entry.discard()

total += entry.size;
entry.on('data',function(data){
dataTotal += data.length
})
})
})

0 comments on commit 33ee078

Please sign in to comment.