Skip to content

Commit 150da0e

Browse files
joaocgreisisaacs
authored andcommitted
unpack: conditionally use a file mapping to write files
Use a file mapping to write files up to 512KB. The limit where using a file mapping stops being an advantage varies from machine to machine. 512KB is a reasonable value to use here, close to the lower bound, to avoid tar becoming slower in some machines. PR-URL: #230 Credit: @joaocgreis Close: #230 Reviewed-by: @isaacs
1 parent eea7dd7 commit 150da0e

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

lib/unpack.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const assert = require('assert')
1010
const EE = require('events').EventEmitter
1111
const Parser = require('./parse.js')
1212
const fs = require('fs')
13+
const { O_CREAT, O_TRUNC, O_WRONLY, UV_FS_O_FILEMAP = 0 } = fs.constants
1314
const fsm = require('fs-minipass')
1415
const path = require('path')
1516
const mkdir = require('./mkdir.js')
@@ -92,6 +93,13 @@ const uint32 = (a, b, c) =>
9293
: b === b >>> 0 ? b
9394
: c
9495

96+
/* istanbul ignore next */
97+
const fMapEnabled = process.platform === 'win32' && !!UV_FS_O_FILEMAP
98+
const fMapLimit = 512 * 1024
99+
const fMapFlag = UV_FS_O_FILEMAP | O_TRUNC | O_CREAT | O_WRONLY
100+
/* istanbul ignore next */
101+
const getFlag = size => (fMapEnabled && size < fMapLimit) ? fMapFlag : 'w'
102+
95103
class Unpack extends Parser {
96104
constructor (opt) {
97105
if (!opt)
@@ -325,6 +333,7 @@ class Unpack extends Parser {
325333
[FILE] (entry, fullyDone) {
326334
const mode = entry.mode & 0o7777 || this.fmode
327335
const stream = new fsm.WriteStream(entry.absolute, {
336+
flags: getFlag(entry.size),
328337
mode: mode,
329338
autoClose: false
330339
})
@@ -572,7 +581,7 @@ class UnpackSync extends Unpack {
572581
let stream
573582
let fd
574583
try {
575-
fd = fs.openSync(entry.absolute, 'w', mode)
584+
fd = fs.openSync(entry.absolute, getFlag(entry.size), mode)
576585
} catch (er) {
577586
return oner(er)
578587
}

0 commit comments

Comments
 (0)