Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node:fs error path performance improvements #49962

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions benchmark/fs/bench-chownSync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict';

const common = require('../common');
const fs = require('fs');
const assert = require('assert');
const tmpdir = require('../../test/common/tmpdir');

if (process.platform === 'win32') {
console.log('Skipping: Windows does not have `getuid` or `getgid`');
process.exit(0);
}

const bench = common.createBenchmark(main, {
type: ['existing', 'non-existing'],
method: ['chownSync', 'lchownSync'],
n: [1e4],
});

function main({ n, type, method }) {
const uid = process.getuid();
const gid = process.getgid();
const fsMethod = fs[method];

switch (type) {
case 'existing': {
tmpdir.refresh();
const tmpfile = tmpdir.resolve(`.existing-file-${process.pid}`);
fs.writeFileSync(tmpfile, 'this-is-for-a-benchmark', 'utf8');
bench.start();
for (let i = 0; i < n; i++) {
fsMethod(tmpfile, uid, gid);
}
bench.end(n);
break;
}
case 'non-existing': {
const path = tmpdir.resolve(`.non-existing-file-${Date.now()}`);
let hasError = false;
bench.start();
for (let i = 0; i < n; i++) {
try {
fs[method](path, uid, gid);
} catch {
hasError = true;
}
}
bench.end(n);
assert(hasError);
break;
}
default:
new Error('Invalid type');
}
}
50 changes: 50 additions & 0 deletions benchmark/fs/bench-linkSync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict';

const common = require('../common');
const fs = require('fs');
const assert = require('assert');
const tmpdir = require('../../test/common/tmpdir');

tmpdir.refresh();
const tmpfile = tmpdir.resolve(`.bench-file-data-${Date.now()}`);
fs.writeFileSync(tmpfile, 'bench-file', 'utf-8');

const bench = common.createBenchmark(main, {
type: ['valid', 'invalid'],
n: [1e3],
});

function main({ n, type }) {
switch (type) {
case 'valid': {
bench.start();
for (let i = 0; i < n; i++) {
fs.linkSync(tmpfile, tmpdir.resolve(`.valid-${i}`), 'file');
}
bench.end(n);

break;
}

case 'invalid': {
let hasError = false;
bench.start();
for (let i = 0; i < n; i++) {
try {
fs.linkSync(
tmpdir.resolve(`.non-existing-file-for-linkSync-${i}`),
__filename,
'file',
);
} catch {
hasError = true;
}
}
bench.end(n);
assert(hasError);
break;
}
default:
new Error('Invalid type');
}
}
54 changes: 54 additions & 0 deletions benchmark/fs/bench-readlinkSync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict';

const common = require('../common');
const fs = require('fs');
const assert = require('assert');
const tmpdir = require('../../test/common/tmpdir');

if (process.platform === 'win32') {
console.log('Skipping: Windows does not play well with `symlinkSync`');
process.exit(0);
}

const bench = common.createBenchmark(main, {
type: ['valid', 'invalid'],
n: [1e3],
});

function main({ n, type }) {
switch (type) {
case 'valid': {
tmpdir.refresh();
const tmpfile = tmpdir.resolve(`.readlink-file-${process.pid}`);
fs.writeFileSync(tmpfile, 'data', 'utf8');
let returnValue;
for (let i = 0; i < n; i++) {
fs.symlinkSync(tmpfile, tmpdir.resolve(`.readlink-sync-${i}`), 'file');
}
bench.start();
for (let i = 0; i < n; i++) {
returnValue = fs.readlinkSync(tmpdir.resolve(`.readlink-sync-${i}`), { encoding: 'utf8' });
}
bench.end(n);
assert(returnValue);
break;
}

case 'invalid': {
let hasError = false;
bench.start();
for (let i = 0; i < n; i++) {
try {
fs.readlinkSync(tmpdir.resolve('.non-existing-file-for-readlinkSync'));
} catch {
hasError = true;
}
}
bench.end(n);
assert(hasError);
break;
}
default:
new Error('Invalid type');
}
}
49 changes: 49 additions & 0 deletions benchmark/fs/bench-renameSync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';

const common = require('../common');
const fs = require('fs');
const assert = require('assert');
const tmpdir = require('../../test/common/tmpdir');

const bench = common.createBenchmark(main, {
type: ['invalid', 'valid'],
n: [2e3],
});

function main({ n, type }) {
switch (type) {
case 'invalid': {
let hasError = false;
bench.start();
for (let i = 0; i < n; i++) {
try {
fs.renameSync(tmpdir.resolve(`.non-existing-file-${i}`), tmpdir.resolve(`.new-file-${i}`));
} catch {
hasError = true;
}
}
bench.end(n);
assert(hasError);
break;
}
case 'valid': {
tmpdir.refresh();
for (let i = 0; i < n; i++) {
fs.writeFileSync(tmpdir.resolve(`.existing-file-${i}`), 'bench', 'utf8');
}

bench.start();
for (let i = 0; i < n; i++) {
fs.renameSync(
tmpdir.resolve(`.existing-file-${i}`),
tmpdir.resolve(`.new-existing-file-${i}`),
);
}

bench.end(n);
break;
}
default:
throw new Error('Invalid type');
}
}
51 changes: 51 additions & 0 deletions benchmark/fs/bench-symlinkSync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';

const common = require('../common');
const fs = require('fs');
const assert = require('assert');
const tmpdir = require('../../test/common/tmpdir');

if (process.platform === 'win32') {
console.log('Skipping: Windows does not play well with `symlink`');
process.exit(0);
}

const bench = common.createBenchmark(main, {
type: ['valid', 'invalid'],
n: [1e3],
});

function main({ n, type }) {
switch (type) {
case 'valid': {
tmpdir.refresh();
bench.start();
for (let i = 0; i < n; i++) {
fs.symlinkSync(tmpdir.resolve('.non-existent-symlink-file'), tmpdir.resolve(`.valid-${i}`), 'file');
}
bench.end(n);
break;
}

case 'invalid': {
let hasError = false;
bench.start();
for (let i = 0; i < n; i++) {
try {
fs.symlinkSync(
tmpdir.resolve('.non-existent-symlink-file'),
__filename,
'file',
);
} catch {
hasError = true;
}
}
bench.end(n);
assert(hasError);
break;
}
default:
new Error('Invalid type');
}
}
72 changes: 33 additions & 39 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1034,10 +1034,10 @@ function rename(oldPath, newPath, callback) {
function renameSync(oldPath, newPath) {
oldPath = getValidatedPath(oldPath, 'oldPath');
newPath = getValidatedPath(newPath, 'newPath');
const ctx = { path: oldPath, dest: newPath };
binding.rename(pathModule.toNamespacedPath(oldPath),
pathModule.toNamespacedPath(newPath), undefined, ctx);
handleErrorFromBinding(ctx);
binding.rename(
pathModule.toNamespacedPath(oldPath),
pathModule.toNamespacedPath(newPath),
);
}

/**
Expand Down Expand Up @@ -1728,11 +1728,10 @@ function readlink(path, options, callback) {
function readlinkSync(path, options) {
options = getOptions(options);
path = getValidatedPath(path, 'oldPath');
const ctx = { path };
const result = binding.readlink(pathModule.toNamespacedPath(path),
options.encoding, undefined, ctx);
handleErrorFromBinding(ctx);
return result;
return binding.readlink(
pathModule.toNamespacedPath(path),
options.encoding,
);
}

/**
Expand Down Expand Up @@ -1805,13 +1804,12 @@ function symlinkSync(target, path, type) {
}
target = getValidatedPath(target, 'target');
path = getValidatedPath(path);
const flags = stringToSymlinkType(type);

const ctx = { path: target, dest: path };
binding.symlink(preprocessSymlinkDestination(target, type, path),
pathModule.toNamespacedPath(path), flags, undefined, ctx);

handleErrorFromBinding(ctx);
binding.symlink(
preprocessSymlinkDestination(target, type, path),
pathModule.toNamespacedPath(path),
stringToSymlinkType(type),
);
}

/**
Expand Down Expand Up @@ -1847,12 +1845,10 @@ function linkSync(existingPath, newPath) {
existingPath = getValidatedPath(existingPath, 'existingPath');
newPath = getValidatedPath(newPath, 'newPath');

const ctx = { path: existingPath, dest: newPath };
const result = binding.link(pathModule.toNamespacedPath(existingPath),
pathModule.toNamespacedPath(newPath),
undefined, ctx);
handleErrorFromBinding(ctx);
return result;
binding.link(
pathModule.toNamespacedPath(existingPath),
pathModule.toNamespacedPath(newPath),
);
}

/**
Expand Down Expand Up @@ -2016,9 +2012,11 @@ function lchownSync(path, uid, gid) {
path = getValidatedPath(path);
validateInteger(uid, 'uid', -1, kMaxUserId);
validateInteger(gid, 'gid', -1, kMaxUserId);
const ctx = { path };
binding.lchown(pathModule.toNamespacedPath(path), uid, gid, undefined, ctx);
handleErrorFromBinding(ctx);
binding.lchown(
pathModule.toNamespacedPath(path),
uid,
gid,
);
}

/**
Expand Down Expand Up @@ -2089,9 +2087,11 @@ function chownSync(path, uid, gid) {
path = getValidatedPath(path);
validateInteger(uid, 'uid', -1, kMaxUserId);
validateInteger(gid, 'gid', -1, kMaxUserId);
const ctx = { path };
binding.chown(pathModule.toNamespacedPath(path), uid, gid, undefined, ctx);
handleErrorFromBinding(ctx);
binding.chown(
pathModule.toNamespacedPath(path),
uid,
gid,
);
}

/**
Expand Down Expand Up @@ -2714,10 +2714,8 @@ function realpathSync(p, options) {
}
}
if (linkTarget === null) {
const ctx = { path: base };
binding.stat(baseLong, false, undefined, true);
linkTarget = binding.readlink(baseLong, undefined, undefined, ctx);
handleErrorFromBinding(ctx);
linkTarget = binding.readlink(baseLong, undefined);
}
resolvedLink = pathModule.resolve(previous, linkTarget);

Expand Down Expand Up @@ -2754,10 +2752,10 @@ function realpathSync(p, options) {
realpathSync.native = (path, options) => {
options = getOptions(options);
path = getValidatedPath(path);
const ctx = { path };
const result = binding.realpath(pathModule.toNamespacedPath(path), options.encoding, undefined, ctx);
handleErrorFromBinding(ctx);
return result;
return binding.realpath(
pathModule.toNamespacedPath(path),
options.encoding,
);
};

/**
Expand Down Expand Up @@ -2966,11 +2964,7 @@ function mkdtempSync(prefix, options) {
path = Buffer.concat([prefix, Buffer.from('XXXXXX')]);
}

const ctx = { path };
const result = binding.mkdtemp(path, options.encoding,
undefined, ctx);
handleErrorFromBinding(ctx);
return result;
return binding.mkdtemp(path, options.encoding);
}

/**
Expand Down
Loading