Skip to content

Commit

Permalink
fs: include more fs.stat*() optimizations
Browse files Browse the repository at this point in the history
Including:

* Move async *stat() functions to FillStatsArray() now used by the
sync *stat() functions

* Avoid creating fs.Stats instances for implicit async/sync *stat()
calls used in various fs functions

* Store reference to Float64Array data on C++ side for easier/faster
access, instead of passing from JS to C++ on every async/sync *stat()
call

Backport-PR-URL: nodejs#11665
Fixes: nodejs#16496
  • Loading branch information
JLHwung committed Nov 1, 2017
1 parent b0fadbe commit b82c613
Show file tree
Hide file tree
Showing 10 changed files with 290 additions and 291 deletions.
22 changes: 16 additions & 6 deletions benchmark/fs/bench-stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,30 @@ const common = require('../common');
const fs = require('fs');

const bench = common.createBenchmark(main, {
n: [1e4],
kind: ['lstat', 'stat']
n: [20e4],
kind: ['fstat', 'lstat', 'stat']
});


function main(conf) {
const n = conf.n >>> 0;
const kind = conf.kind;
var arg;
if (kind === 'fstat')
arg = fs.openSync(__filename, 'r');
else
arg = __filename;

bench.start();
(function r(cntr, fn) {
if (cntr-- <= 0)
return bench.end(n);
fn(__filename, function() {
if (cntr-- <= 0) {
bench.end(n);
if (kind === 'fstat')
fs.closeSync(arg);
return;
}
fn(arg, function() {
r(cntr, fn);
});
}(n, fs[conf.kind]));
}(n, fs[kind]));
}
37 changes: 12 additions & 25 deletions benchmark/fs/bench-statSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,23 @@ const common = require('../common');
const fs = require('fs');

const bench = common.createBenchmark(main, {
n: [1e4],
n: [1e6],
kind: ['fstatSync', 'lstatSync', 'statSync']
});


function main(conf) {
const n = conf.n >>> 0;
var fn;
var i;
switch (conf.kind) {
case 'statSync':
case 'lstatSync':
fn = fs[conf.kind];
bench.start();
for (i = 0; i < n; i++) {
fn(__filename);
}
bench.end(n);
break;
case 'fstatSync':
fn = fs.fstatSync;
const fd = fs.openSync(__filename, 'r');
bench.start();
for (i = 0; i < n; i++) {
fn(fd);
}
bench.end(n);
fs.closeSync(fd);
break;
default:
throw new Error('Invalid kind argument');
const kind = conf.kind;
const arg = (kind === 'fstatSync' ? fs.openSync(__filename, 'r') : __dirname);
const fn = fs[conf.kind];

bench.start();
for (var i = 0; i < n; i++) {
fn(arg);
}
bench.end(n);

if (kind === 'fstat')
fs.closeSync(arg);
}
17 changes: 17 additions & 0 deletions benchmark/fs/readFileSync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

var common = require('../common.js');
var fs = require('fs');

var bench = common.createBenchmark(main, {
n: [60e4]
});

function main(conf) {
var n = +conf.n;

bench.start();
for (var i = 0; i < n; ++i)
fs.readFileSync(__filename);
bench.end(n);
}
Loading

0 comments on commit b82c613

Please sign in to comment.