Skip to content

Commit 8413670

Browse files
CanadaHonktargos
authored andcommitted
fs: improve error perf of sync *times
PR-URL: #49864 Refs: nodejs/performance#106 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Stephen Belanger <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent bd5d533 commit 8413670

File tree

4 files changed

+97
-41
lines changed

4 files changed

+97
-41
lines changed

benchmark/fs/bench-timesSync.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const fs = require('fs');
5+
const tmpdir = require('../../test/common/tmpdir');
6+
tmpdir.refresh();
7+
8+
const bench = common.createBenchmark(main, {
9+
type: ['existing', 'non-existing'],
10+
func: [ 'utimes', 'futimes', 'lutimes' ],
11+
n: [1e3],
12+
});
13+
14+
function main({ n, type, func }) {
15+
const useFds = func === 'futimes';
16+
const fsFunc = fs[func + 'Sync'];
17+
18+
switch (type) {
19+
case 'existing': {
20+
const files = [];
21+
22+
// Populate tmpdir with mock files
23+
for (let i = 0; i < n; i++) {
24+
const path = tmpdir.resolve(`timessync-bench-file-${i}`);
25+
fs.writeFileSync(path, 'bench');
26+
files.push(useFds ? fs.openSync(path, 'r+') : path);
27+
}
28+
29+
bench.start();
30+
for (let i = 0; i < n; i++) {
31+
fsFunc(files[i], i, i);
32+
}
33+
bench.end(n);
34+
35+
if (useFds) files.forEach((x) => {
36+
try {
37+
fs.closeSync(x);
38+
} catch {
39+
// do nothing
40+
}
41+
});
42+
43+
break;
44+
}
45+
case 'non-existing': {
46+
bench.start();
47+
for (let i = 0; i < n; i++) {
48+
try {
49+
fsFunc(useFds ? (1 << 30) : tmpdir.resolve(`.non-existing-file-${Date.now()}`), i, i);
50+
} catch {
51+
// do nothing
52+
}
53+
}
54+
bench.end(n);
55+
56+
break;
57+
}
58+
default:
59+
new Error('Invalid type');
60+
}
61+
}

lib/fs.js

+15-17
Original file line numberDiff line numberDiff line change
@@ -2123,11 +2123,11 @@ function utimes(path, atime, mtime, callback) {
21232123
*/
21242124
function utimesSync(path, atime, mtime) {
21252125
path = getValidatedPath(path);
2126-
const ctx = { path };
2127-
binding.utimes(pathModule.toNamespacedPath(path),
2128-
toUnixTimestamp(atime), toUnixTimestamp(mtime),
2129-
undefined, ctx);
2130-
handleErrorFromBinding(ctx);
2126+
binding.utimes(
2127+
pathModule.toNamespacedPath(path),
2128+
toUnixTimestamp(atime),
2129+
toUnixTimestamp(mtime),
2130+
);
21312131
}
21322132

21332133
/**
@@ -2160,12 +2160,11 @@ function futimes(fd, atime, mtime, callback) {
21602160
* @returns {void}
21612161
*/
21622162
function futimesSync(fd, atime, mtime) {
2163-
fd = getValidatedFd(fd);
2164-
atime = toUnixTimestamp(atime, 'atime');
2165-
mtime = toUnixTimestamp(mtime, 'mtime');
2166-
const ctx = {};
2167-
binding.futimes(fd, atime, mtime, undefined, ctx);
2168-
handleErrorFromBinding(ctx);
2163+
binding.futimes(
2164+
getValidatedFd(fd),
2165+
toUnixTimestamp(atime, 'atime'),
2166+
toUnixTimestamp(mtime, 'mtime'),
2167+
);
21692168
}
21702169

21712170
/**
@@ -2199,12 +2198,11 @@ function lutimes(path, atime, mtime, callback) {
21992198
*/
22002199
function lutimesSync(path, atime, mtime) {
22012200
path = getValidatedPath(path);
2202-
const ctx = { path };
2203-
binding.lutimes(pathModule.toNamespacedPath(path),
2204-
toUnixTimestamp(atime),
2205-
toUnixTimestamp(mtime),
2206-
undefined, ctx);
2207-
handleErrorFromBinding(ctx);
2201+
binding.lutimes(
2202+
pathModule.toNamespacedPath(path),
2203+
toUnixTimestamp(atime),
2204+
toUnixTimestamp(mtime),
2205+
);
22082206
}
22092207

22102208
function writeAll(fd, isUserFd, buffer, offset, length, signal, flush, callback) {

src/node_file.cc

+18-21
Original file line numberDiff line numberDiff line change
@@ -2721,18 +2721,17 @@ static void UTimes(const FunctionCallbackInfo<Value>& args) {
27212721
CHECK(args[2]->IsNumber());
27222722
const double mtime = args[2].As<Number>()->Value();
27232723

2724-
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
2725-
if (req_wrap_async != nullptr) { // utimes(path, atime, mtime, req)
2724+
if (argc > 3) { // utimes(path, atime, mtime, req)
2725+
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
27262726
FS_ASYNC_TRACE_BEGIN1(
27272727
UV_FS_UTIME, req_wrap_async, "path", TRACE_STR_COPY(*path))
27282728
AsyncCall(env, req_wrap_async, args, "utime", UTF8, AfterNoArgs,
27292729
uv_fs_utime, *path, atime, mtime);
2730-
} else { // utimes(path, atime, mtime, undefined, ctx)
2731-
CHECK_EQ(argc, 5);
2732-
FSReqWrapSync req_wrap_sync;
2730+
} else { // utimes(path, atime, mtime)
2731+
FSReqWrapSync req_wrap_sync("utime", *path);
27332732
FS_SYNC_TRACE_BEGIN(utimes);
2734-
SyncCall(env, args[4], &req_wrap_sync, "utime",
2735-
uv_fs_utime, *path, atime, mtime);
2733+
SyncCallAndThrowOnError(
2734+
env, &req_wrap_sync, uv_fs_utime, *path, atime, mtime);
27362735
FS_SYNC_TRACE_END(utimes);
27372736
}
27382737
}
@@ -2752,17 +2751,16 @@ static void FUTimes(const FunctionCallbackInfo<Value>& args) {
27522751
CHECK(args[2]->IsNumber());
27532752
const double mtime = args[2].As<Number>()->Value();
27542753

2755-
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
2756-
if (req_wrap_async != nullptr) { // futimes(fd, atime, mtime, req)
2754+
if (argc > 3) { // futimes(fd, atime, mtime, req)
2755+
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
27572756
FS_ASYNC_TRACE_BEGIN0(UV_FS_FUTIME, req_wrap_async)
27582757
AsyncCall(env, req_wrap_async, args, "futime", UTF8, AfterNoArgs,
27592758
uv_fs_futime, fd, atime, mtime);
2760-
} else { // futimes(fd, atime, mtime, undefined, ctx)
2761-
CHECK_EQ(argc, 5);
2762-
FSReqWrapSync req_wrap_sync;
2759+
} else { // futimes(fd, atime, mtime)
2760+
FSReqWrapSync req_wrap_sync("futime");
27632761
FS_SYNC_TRACE_BEGIN(futimes);
2764-
SyncCall(env, args[4], &req_wrap_sync, "futime",
2765-
uv_fs_futime, fd, atime, mtime);
2762+
SyncCallAndThrowOnError(
2763+
env, &req_wrap_sync, uv_fs_futime, fd, atime, mtime);
27662764
FS_SYNC_TRACE_END(futimes);
27672765
}
27682766
}
@@ -2784,18 +2782,17 @@ static void LUTimes(const FunctionCallbackInfo<Value>& args) {
27842782
CHECK(args[2]->IsNumber());
27852783
const double mtime = args[2].As<Number>()->Value();
27862784

2787-
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
2788-
if (req_wrap_async != nullptr) { // lutimes(path, atime, mtime, req)
2785+
if (argc > 3) { // lutimes(path, atime, mtime, req)
2786+
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
27892787
FS_ASYNC_TRACE_BEGIN1(
27902788
UV_FS_LUTIME, req_wrap_async, "path", TRACE_STR_COPY(*path))
27912789
AsyncCall(env, req_wrap_async, args, "lutime", UTF8, AfterNoArgs,
27922790
uv_fs_lutime, *path, atime, mtime);
2793-
} else { // lutimes(path, atime, mtime, undefined, ctx)
2794-
CHECK_EQ(argc, 5);
2795-
FSReqWrapSync req_wrap_sync;
2791+
} else { // lutimes(path, atime, mtime)
2792+
FSReqWrapSync req_wrap_sync("lutime", *path);
27962793
FS_SYNC_TRACE_BEGIN(lutimes);
2797-
SyncCall(env, args[4], &req_wrap_sync, "lutime",
2798-
uv_fs_lutime, *path, atime, mtime);
2794+
SyncCallAndThrowOnError(
2795+
env, &req_wrap_sync, uv_fs_lutime, *path, atime, mtime);
27992796
FS_SYNC_TRACE_END(lutimes);
28002797
}
28012798
}

typings/internalBinding/fs.d.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ declare namespace InternalFSBinding {
107107
function ftruncate(fd: number, len: number, usePromises: typeof kUsePromises): Promise<void>;
108108

109109
function futimes(fd: number, atime: number, mtime: number, req: FSReqCallback): void;
110-
function futimes(fd: number, atime: number, mtime: number, req: undefined, ctx: FSSyncContext): void;
110+
function futimes(fd: number, atime: number, mtime: number): void;
111111
function futimes(fd: number, atime: number, mtime: number, usePromises: typeof kUsePromises): Promise<void>;
112112

113113
function internalModuleReadJSON(path: string): [] | [string, boolean];
@@ -132,7 +132,7 @@ declare namespace InternalFSBinding {
132132
function lstat(path: StringOrBuffer, useBigint: false, usePromises: typeof kUsePromises): Promise<Float64Array>;
133133

134134
function lutimes(path: string, atime: number, mtime: number, req: FSReqCallback): void;
135-
function lutimes(path: string, atime: number, mtime: number, req: undefined, ctx: FSSyncContext): void;
135+
function lutimes(path: string, atime: number, mtime: number): void;
136136
function lutimes(path: string, atime: number, mtime: number, usePromises: typeof kUsePromises): Promise<void>;
137137

138138
function mkdtemp(prefix: string, encoding: unknown, req: FSReqCallback<string>): void;
@@ -207,7 +207,7 @@ declare namespace InternalFSBinding {
207207
function unlink(path: string, usePromises: typeof kUsePromises): Promise<void>;
208208

209209
function utimes(path: string, atime: number, mtime: number, req: FSReqCallback): void;
210-
function utimes(path: string, atime: number, mtime: number, req: undefined, ctx: FSSyncContext): void;
210+
function utimes(path: string, atime: number, mtime: number): void;
211211
function utimes(path: string, atime: number, mtime: number, usePromises: typeof kUsePromises): Promise<void>;
212212

213213
function writeBuffer(fd: number, buffer: ArrayBufferView, offset: number, length: number, position: number | null, req: FSReqCallback<number>): void;

0 commit comments

Comments
 (0)