Skip to content

Commit 44ee0a1

Browse files
CanadaHonkanonrig
authored andcommitted
fs: improve error perf of sync lstat+fstat
1 parent 00de2fa commit 44ee0a1

File tree

6 files changed

+78
-76
lines changed

6 files changed

+78
-76
lines changed

benchmark/fs/bench-statSync-failure.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,29 @@ const fs = require('fs');
55
const path = require('path');
66

77
const bench = common.createBenchmark(main, {
8-
n: [1e6],
9-
statSyncType: ['throw', 'noThrow'],
8+
n: [1e4],
9+
statSyncType: ['fstatSync', 'lstatSync', 'statSync'],
10+
throwType: [ 'throw', 'noThrow' ],
11+
}, {
12+
// fstatSync does not support throwIfNoEntry
13+
combinationFilter: ({ statSyncType, throwType }) => !(statSyncType === 'fstatSync' && throwType === 'noThrow'),
1014
});
1115

1216

13-
function main({ n, statSyncType }) {
14-
const arg = path.join(__dirname, 'non.existent');
17+
function main({ n, statSyncType, throwType }) {
18+
const arg = (statSyncType === 'fstatSync' ?
19+
(1 << 30) :
20+
path.join(__dirname, 'non.existent'));
21+
22+
const fn = fs[statSyncType];
1523

1624
bench.start();
1725
for (let i = 0; i < n; i++) {
18-
if (statSyncType === 'noThrow') {
19-
fs.statSync(arg, { throwIfNoEntry: false });
26+
if (throwType === 'noThrow') {
27+
fn(arg, { throwIfNoEntry: false });
2028
} else {
2129
try {
22-
fs.statSync(arg);
30+
fn(arg);
2331
} catch {
2432
// Continue regardless of error.
2533
}

benchmark/fs/bench-statSync.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const common = require('../common');
44
const fs = require('fs');
55

66
const bench = common.createBenchmark(main, {
7-
n: [1e6],
7+
n: [1e4],
88
statSyncType: ['fstatSync', 'lstatSync', 'statSync'],
99
});
1010

lib/fs.js

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ const {
7272
ERR_INVALID_ARG_VALUE,
7373
},
7474
AbortError,
75-
uvErrmapGet,
76-
uvException,
7775
} = require('internal/errors');
7876

7977
const {
@@ -398,11 +396,9 @@ function readFile(path, options, callback) {
398396
}
399397

400398
function tryStatSync(fd, isUserFd) {
401-
const ctx = {};
402-
const stats = binding.fstat(fd, false, undefined, ctx);
403-
if (ctx.errno !== undefined && !isUserFd) {
399+
const stats = binding.fstat(fd, false, undefined, true /* shouldNotThrow */);
400+
if (stats === undefined && !isUserFd) {
404401
fs.closeSync(fd);
405-
throw uvException(ctx);
406402
}
407403
return stats;
408404
}
@@ -1616,33 +1612,21 @@ function statfs(path, options = { bigint: false }, callback) {
16161612
binding.statfs(pathModule.toNamespacedPath(path), options.bigint, req);
16171613
}
16181614

1619-
function hasNoEntryError(ctx) {
1620-
if (ctx.errno) {
1621-
const uvErr = uvErrmapGet(ctx.errno);
1622-
return uvErr?.[0] === 'ENOENT';
1623-
}
1624-
1625-
if (ctx.error) {
1626-
return ctx.error.code === 'ENOENT';
1627-
}
1628-
1629-
return false;
1630-
}
1631-
16321615
/**
16331616
* Synchronously retrieves the `fs.Stats` for
16341617
* the file descriptor.
16351618
* @param {number} fd
16361619
* @param {{
16371620
* bigint?: boolean;
16381621
* }} [options]
1639-
* @returns {Stats}
1622+
* @returns {Stats | undefined}
16401623
*/
16411624
function fstatSync(fd, options = { bigint: false }) {
16421625
fd = getValidatedFd(fd);
1643-
const ctx = { fd };
1644-
const stats = binding.fstat(fd, options.bigint, undefined, ctx);
1645-
handleErrorFromBinding(ctx);
1626+
const stats = binding.fstat(fd, options.bigint, undefined, false);
1627+
if (stats === undefined) {
1628+
return;
1629+
}
16461630
return getStatsFromBinding(stats);
16471631
}
16481632

@@ -1654,17 +1638,20 @@ function fstatSync(fd, options = { bigint: false }) {
16541638
* bigint?: boolean;
16551639
* throwIfNoEntry?: boolean;
16561640
* }} [options]
1657-
* @returns {Stats}
1641+
* @returns {Stats | undefined}
16581642
*/
16591643
function lstatSync(path, options = { bigint: false, throwIfNoEntry: true }) {
16601644
path = getValidatedPath(path);
1661-
const ctx = { path };
1662-
const stats = binding.lstat(pathModule.toNamespacedPath(path),
1663-
options.bigint, undefined, ctx);
1664-
if (options.throwIfNoEntry === false && hasNoEntryError(ctx)) {
1665-
return undefined;
1645+
const stats = binding.lstat(
1646+
pathModule.toNamespacedPath(path),
1647+
options.bigint,
1648+
undefined,
1649+
options.throwIfNoEntry,
1650+
);
1651+
1652+
if (stats === undefined) {
1653+
return;
16661654
}
1667-
handleErrorFromBinding(ctx);
16681655
return getStatsFromBinding(stats);
16691656
}
16701657

@@ -2649,9 +2636,10 @@ function realpathSync(p, options) {
26492636

26502637
// On windows, check that the root exists. On unix there is no need.
26512638
if (isWindows) {
2652-
const ctx = { path: base };
2653-
binding.lstat(pathModule.toNamespacedPath(base), false, undefined, ctx);
2654-
handleErrorFromBinding(ctx);
2639+
const out = binding.lstat(pathModule.toNamespacedPath(base), false, undefined, true /* throwIfNoEntry */);
2640+
if (out === undefined) {
2641+
return;
2642+
}
26552643
knownHard.add(base);
26562644
}
26572645

@@ -2691,9 +2679,10 @@ function realpathSync(p, options) {
26912679
// for our internal use.
26922680

26932681
const baseLong = pathModule.toNamespacedPath(base);
2694-
const ctx = { path: base };
2695-
const stats = binding.lstat(baseLong, true, undefined, ctx);
2696-
handleErrorFromBinding(ctx);
2682+
const stats = binding.lstat(baseLong, true, undefined, true /* throwIfNoEntry */);
2683+
if (stats === undefined) {
2684+
return;
2685+
}
26972686

26982687
if (!isFileType(stats, S_IFLNK)) {
26992688
knownHard.add(base);
@@ -2734,9 +2723,10 @@ function realpathSync(p, options) {
27342723

27352724
// On windows, check that the root exists. On unix there is no need.
27362725
if (isWindows && !knownHard.has(base)) {
2737-
const ctx = { path: base };
2738-
binding.lstat(pathModule.toNamespacedPath(base), false, undefined, ctx);
2739-
handleErrorFromBinding(ctx);
2726+
const out = binding.lstat(pathModule.toNamespacedPath(base), false, undefined, true /* throwIfNoEntry */);
2727+
if (out === undefined) {
2728+
return;
2729+
}
27402730
knownHard.add(base);
27412731
}
27422732
}

src/node_file.cc

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,21 +1192,26 @@ static void LStat(const FunctionCallbackInfo<Value>& args) {
11921192
CHECK_NOT_NULL(*path);
11931193

11941194
bool use_bigint = args[1]->IsTrue();
1195-
FSReqBase* req_wrap_async = GetReqWrap(args, 2, use_bigint);
1196-
if (req_wrap_async != nullptr) { // lstat(path, use_bigint, req)
1195+
if (!args[2]->IsUndefined()) { // lstat(path, use_bigint, req)
1196+
FSReqBase* req_wrap_async = GetReqWrap(args, 2, use_bigint);
11971197
FS_ASYNC_TRACE_BEGIN1(
11981198
UV_FS_LSTAT, req_wrap_async, "path", TRACE_STR_COPY(*path))
11991199
AsyncCall(env, req_wrap_async, args, "lstat", UTF8, AfterStat,
12001200
uv_fs_lstat, *path);
1201-
} else { // lstat(path, use_bigint, undefined, ctx)
1202-
CHECK_EQ(argc, 4);
1203-
FSReqWrapSync req_wrap_sync;
1201+
} else { // lstat(path, use_bigint, undefined, throw_if_no_entry)
1202+
bool do_not_throw_if_no_entry = args[3]->IsFalse();
1203+
FSReqWrapSync req_wrap_sync("lstat", *path);
12041204
FS_SYNC_TRACE_BEGIN(lstat);
1205-
int err = SyncCall(env, args[3], &req_wrap_sync, "lstat", uv_fs_lstat,
1206-
*path);
1205+
int result;
1206+
if (do_not_throw_if_no_entry) {
1207+
result = SyncCallAndThrowIf(
1208+
is_uv_error_except_no_entry, env, &req_wrap_sync, uv_fs_lstat, *path);
1209+
} else {
1210+
result = SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_lstat, *path);
1211+
}
12071212
FS_SYNC_TRACE_END(lstat);
1208-
if (err != 0) {
1209-
return; // error info is in ctx
1213+
if (is_uv_error(result)) {
1214+
return;
12101215
}
12111216

12121217
Local<Value> arr = FillGlobalStatsArray(binding_data, use_bigint,
@@ -1227,19 +1232,22 @@ static void FStat(const FunctionCallbackInfo<Value>& args) {
12271232
int fd = args[0].As<Int32>()->Value();
12281233

12291234
bool use_bigint = args[1]->IsTrue();
1230-
FSReqBase* req_wrap_async = GetReqWrap(args, 2, use_bigint);
1231-
if (req_wrap_async != nullptr) { // fstat(fd, use_bigint, req)
1235+
if (!args[2]->IsUndefined()) { // fstat(fd, use_bigint, req)
1236+
FSReqBase* req_wrap_async = GetReqWrap(args, 2, use_bigint);
12321237
FS_ASYNC_TRACE_BEGIN0(UV_FS_FSTAT, req_wrap_async)
12331238
AsyncCall(env, req_wrap_async, args, "fstat", UTF8, AfterStat,
12341239
uv_fs_fstat, fd);
1235-
} else { // fstat(fd, use_bigint, undefined, ctx)
1236-
CHECK_EQ(argc, 4);
1237-
FSReqWrapSync req_wrap_sync;
1240+
} else { // fstat(fd, use_bigint, undefined, do_not_throw_error)
1241+
bool do_not_throw_error = args[2]->IsTrue();
1242+
const auto should_throw = [do_not_throw_error](int result) {
1243+
return is_uv_error(result) && !do_not_throw_error;
1244+
};
1245+
FSReqWrapSync req_wrap_sync("fstat");
12381246
FS_SYNC_TRACE_BEGIN(fstat);
1239-
int err = SyncCall(env, args[3], &req_wrap_sync, "fstat", uv_fs_fstat, fd);
1247+
int err = SyncCallAndThrowIf(should_throw, env, &req_wrap_sync, uv_fs_fstat, fd);
12401248
FS_SYNC_TRACE_END(fstat);
1241-
if (err != 0) {
1242-
return; // error info is in ctx
1249+
if (is_uv_error(err)) {
1250+
return;
12431251
}
12441252

12451253
Local<Value> arr = FillGlobalStatsArray(binding_data, use_bigint,

test/parallel/test-fs-sync-fd-leak.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,11 @@ fs.writeSync = function() {
4242
throw new Error('BAM');
4343
};
4444

45-
internalBinding('fs').fstat = function(fd, bigint, _, ctx) {
46-
ctx.errno = UV_EBADF;
47-
ctx.syscall = 'fstat';
45+
internalBinding('fs').fstat = function() {
46+
throw new Error('EBADF: bad file descriptor, fstat');
4847
};
4948

5049
let close_called = 0;
51-
ensureThrows(function() {
52-
fs.readFileSync('dummy');
53-
}, 'EBADF: bad file descriptor, fstat');
5450
ensureThrows(function() {
5551
fs.writeFileSync('dummy', 'xxx');
5652
}, 'BAM');

typings/internalBinding/fs.d.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ declare namespace InternalFSBinding {
9191
function fstat(fd: number, useBigint: boolean, req: FSReqCallback<Float64Array | BigUint64Array>): void;
9292
function fstat(fd: number, useBigint: true, req: FSReqCallback<BigUint64Array>): void;
9393
function fstat(fd: number, useBigint: false, req: FSReqCallback<Float64Array>): void;
94-
function fstat(fd: number, useBigint: boolean, req: undefined, ctx: FSSyncContext): Float64Array | BigUint64Array;
95-
function fstat(fd: number, useBigint: true, req: undefined, ctx: FSSyncContext): BigUint64Array;
96-
function fstat(fd: number, useBigint: false, req: undefined, ctx: FSSyncContext): Float64Array;
94+
function fstat(fd: number, useBigint: boolean, req: undefined, shouldNotThrow: boolean): Float64Array | BigUint64Array;
95+
function fstat(fd: number, useBigint: true, req: undefined, shouldNotThrow: boolean): BigUint64Array;
96+
function fstat(fd: number, useBigint: false, req: undefined, shouldNotThrow: boolean): Float64Array;
9797
function fstat(fd: number, useBigint: boolean, usePromises: typeof kUsePromises): Promise<Float64Array | BigUint64Array>;
9898
function fstat(fd: number, useBigint: true, usePromises: typeof kUsePromises): Promise<BigUint64Array>;
9999
function fstat(fd: number, useBigint: false, usePromises: typeof kUsePromises): Promise<Float64Array>;
@@ -124,9 +124,9 @@ declare namespace InternalFSBinding {
124124
function lstat(path: StringOrBuffer, useBigint: boolean, req: FSReqCallback<Float64Array | BigUint64Array>): void;
125125
function lstat(path: StringOrBuffer, useBigint: true, req: FSReqCallback<BigUint64Array>): void;
126126
function lstat(path: StringOrBuffer, useBigint: false, req: FSReqCallback<Float64Array>): void;
127-
function lstat(path: StringOrBuffer, useBigint: boolean, req: undefined, ctx: FSSyncContext): Float64Array | BigUint64Array;
128-
function lstat(path: StringOrBuffer, useBigint: true, req: undefined, ctx: FSSyncContext): BigUint64Array;
129-
function lstat(path: StringOrBuffer, useBigint: false, req: undefined, ctx: FSSyncContext): Float64Array;
127+
function lstat(path: StringOrBuffer, useBigint: boolean, req: undefined, throwIfNoEntry: boolean): Float64Array | BigUint64Array;
128+
function lstat(path: StringOrBuffer, useBigint: true, req: undefined, throwIfNoEntry: boolean): BigUint64Array;
129+
function lstat(path: StringOrBuffer, useBigint: false, req: undefined, throwIfNoEntry: boolean): Float64Array;
130130
function lstat(path: StringOrBuffer, useBigint: boolean, usePromises: typeof kUsePromises): Promise<Float64Array | BigUint64Array>;
131131
function lstat(path: StringOrBuffer, useBigint: true, usePromises: typeof kUsePromises): Promise<BigUint64Array>;
132132
function lstat(path: StringOrBuffer, useBigint: false, usePromises: typeof kUsePromises): Promise<Float64Array>;

0 commit comments

Comments
 (0)