mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
fs: improve error performance of symlinkSync
PR-URL: https://github.com/nodejs/node/pull/49962 Refs: https://github.com/nodejs/performance/issues/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>
This commit is contained in:
committed by
Node.js GitHub Bot
parent
7603bf5b81
commit
449dc45691
51
benchmark/fs/bench-symlinkSync.js
Normal file
51
benchmark/fs/bench-symlinkSync.js
Normal file
@@ -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
lib/fs.js
11
lib/fs.js
@@ -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),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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);
|
||||
@@ -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",
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
1
typings/internalBinding/fs.d.ts
vendored
1
typings/internalBinding/fs.d.ts
vendored
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user