Skip to content

Commit 89e7878

Browse files
anonrigtargos
authored andcommitted
fs: improve error performance of symlinkSync
PR-URL: #49962 Refs: nodejs/performance#106 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Stephen Belanger <[email protected]> Reviewed-By: Vinícius Lourenço Claro Cardoso <[email protected]>
1 parent af6a061 commit 89e7878

File tree

4 files changed

+63
-13
lines changed

4 files changed

+63
-13
lines changed

benchmark/fs/bench-symlinkSync.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const fs = require('fs');
5+
const assert = require('assert');
6+
const tmpdir = require('../../test/common/tmpdir');
7+
8+
if (process.platform === 'win32') {
9+
console.log('Skipping: Windows does not play well with `symlink`');
10+
process.exit(0);
11+
}
12+
13+
const bench = common.createBenchmark(main, {
14+
type: ['valid', 'invalid'],
15+
n: [1e3],
16+
});
17+
18+
function main({ n, type }) {
19+
switch (type) {
20+
case 'valid': {
21+
tmpdir.refresh();
22+
bench.start();
23+
for (let i = 0; i < n; i++) {
24+
fs.symlinkSync(tmpdir.resolve('.non-existent-symlink-file'), tmpdir.resolve(`.valid-${i}`), 'file');
25+
}
26+
bench.end(n);
27+
break;
28+
}
29+
30+
case 'invalid': {
31+
let hasError = false;
32+
bench.start();
33+
for (let i = 0; i < n; i++) {
34+
try {
35+
fs.symlinkSync(
36+
tmpdir.resolve('.non-existent-symlink-file'),
37+
__filename,
38+
'file',
39+
);
40+
} catch {
41+
hasError = true;
42+
}
43+
}
44+
bench.end(n);
45+
assert(hasError);
46+
break;
47+
}
48+
default:
49+
new Error('Invalid type');
50+
}
51+
}

lib/fs.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -1802,13 +1802,12 @@ function symlinkSync(target, path, type) {
18021802
}
18031803
target = getValidatedPath(target, 'target');
18041804
path = getValidatedPath(path);
1805-
const flags = stringToSymlinkType(type);
1806-
1807-
const ctx = { path: target, dest: path };
1808-
binding.symlink(preprocessSymlinkDestination(target, type, path),
1809-
pathModule.toNamespacedPath(path), flags, undefined, ctx);
18101805

1811-
handleErrorFromBinding(ctx);
1806+
binding.symlink(
1807+
preprocessSymlinkDestination(target, type, path),
1808+
pathModule.toNamespacedPath(path),
1809+
stringToSymlinkType(type),
1810+
);
18121811
}
18131812

18141813
/**

src/node_file.cc

+6-7
Original file line numberDiff line numberDiff line change
@@ -1333,7 +1333,7 @@ static void Symlink(const FunctionCallbackInfo<Value>& args) {
13331333
Isolate* isolate = env->isolate();
13341334

13351335
const int argc = args.Length();
1336-
CHECK_GE(argc, 4);
1336+
CHECK_GE(argc, 3);
13371337

13381338
BufferValue target(isolate, args[0]);
13391339
CHECK_NOT_NULL(*target);
@@ -1352,8 +1352,8 @@ static void Symlink(const FunctionCallbackInfo<Value>& args) {
13521352
CHECK(args[2]->IsInt32());
13531353
int flags = args[2].As<Int32>()->Value();
13541354

1355-
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
1356-
if (req_wrap_async != nullptr) { // symlink(target, path, flags, req)
1355+
if (argc > 3) { // symlink(target, path, flags, req)
1356+
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
13571357
FS_ASYNC_TRACE_BEGIN2(UV_FS_SYMLINK,
13581358
req_wrap_async,
13591359
"target",
@@ -1363,11 +1363,10 @@ static void Symlink(const FunctionCallbackInfo<Value>& args) {
13631363
AsyncDestCall(env, req_wrap_async, args, "symlink", *path, path.length(),
13641364
UTF8, AfterNoArgs, uv_fs_symlink, *target, *path, flags);
13651365
} else { // symlink(target, path, flags, undefined, ctx)
1366-
CHECK_EQ(argc, 5);
1367-
FSReqWrapSync req_wrap_sync;
1366+
FSReqWrapSync req_wrap_sync("symlink", *target, *path);
13681367
FS_SYNC_TRACE_BEGIN(symlink);
1369-
SyncCall(env, args[4], &req_wrap_sync, "symlink",
1370-
uv_fs_symlink, *target, *path, flags);
1368+
SyncCallAndThrowOnError(
1369+
env, &req_wrap_sync, uv_fs_symlink, *target, *path, flags);
13711370
FS_SYNC_TRACE_END(symlink);
13721371
}
13731372
}

typings/internalBinding/fs.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ declare namespace InternalFSBinding {
206206
function symlink(target: StringOrBuffer, path: StringOrBuffer, type: number, req: FSReqCallback): void;
207207
function symlink(target: StringOrBuffer, path: StringOrBuffer, type: number, req: undefined, ctx: FSSyncContext): void;
208208
function symlink(target: StringOrBuffer, path: StringOrBuffer, type: number, usePromises: typeof kUsePromises): Promise<void>;
209+
function symlink(target: StringOrBuffer, path: StringOrBuffer, type: number): void;
209210

210211
function unlink(path: string, req: FSReqCallback): void;
211212
function unlink(path: string): void;

0 commit comments

Comments
 (0)