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

PR-URL: #11665
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
mscdex authored and jasnell committed Mar 15, 2017
1 parent 3d7245c commit 6a5ab5d
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 244 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 6a5ab5d

Please sign in to comment.