Skip to content

Commit

Permalink
fs: improve error performance of symlinkSync
Browse files Browse the repository at this point in the history
PR-URL: nodejs#49962
Refs: nodejs/performance#106
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
  • Loading branch information
anonrig authored and alexfernandez committed Nov 1, 2023
1 parent c7123f4 commit 05f1858
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 13 deletions.
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');
}
}
11 changes: 5 additions & 6 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1804,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
13 changes: 6 additions & 7 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1298,7 +1298,7 @@ static void Symlink(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = env->isolate();

const int argc = args.Length();
CHECK_GE(argc, 4);
CHECK_GE(argc, 3);

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

FSReqBase* req_wrap_async = GetReqWrap(args, 3);
if (req_wrap_async != nullptr) { // symlink(target, path, flags, req)
if (argc > 3) { // symlink(target, path, flags, req)
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
FS_ASYNC_TRACE_BEGIN2(UV_FS_SYMLINK,
req_wrap_async,
"target",
Expand All @@ -1328,11 +1328,10 @@ static void Symlink(const FunctionCallbackInfo<Value>& args) {
AsyncDestCall(env, req_wrap_async, args, "symlink", *path, path.length(),
UTF8, AfterNoArgs, uv_fs_symlink, *target, *path, flags);
} else { // symlink(target, path, flags, undefined, ctx)
CHECK_EQ(argc, 5);
FSReqWrapSync req_wrap_sync;
FSReqWrapSync req_wrap_sync("symlink", *target, *path);
FS_SYNC_TRACE_BEGIN(symlink);
SyncCall(env, args[4], &req_wrap_sync, "symlink",
uv_fs_symlink, *target, *path, flags);
SyncCallAndThrowOnError(
env, &req_wrap_sync, uv_fs_symlink, *target, *path, flags);
FS_SYNC_TRACE_END(symlink);
}
}
Expand Down
1 change: 1 addition & 0 deletions typings/internalBinding/fs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ declare namespace InternalFSBinding {
function symlink(target: StringOrBuffer, path: StringOrBuffer, type: number, req: FSReqCallback): void;
function symlink(target: StringOrBuffer, path: StringOrBuffer, type: number, req: undefined, ctx: FSSyncContext): void;
function symlink(target: StringOrBuffer, path: StringOrBuffer, type: number, usePromises: typeof kUsePromises): Promise<void>;
function symlink(target: StringOrBuffer, path: StringOrBuffer, type: number): void;

function unlink(path: string, req: FSReqCallback): void;
function unlink(path: string): void;
Expand Down

0 comments on commit 05f1858

Please sign in to comment.