mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
fs: improve error performance for unlinkSync
PR-URL: https://github.com/nodejs/node/pull/49856 Refs: https://github.com/nodejs/performance/issues/106 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
This commit is contained in:
parent
a6ad048b89
commit
7e0b6a5939
43
benchmark/fs/bench-unlinkSync.js
Normal file
43
benchmark/fs/bench-unlinkSync.js
Normal file
@ -0,0 +1,43 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common');
|
||||
const fs = require('fs');
|
||||
const tmpdir = require('../../test/common/tmpdir');
|
||||
tmpdir.refresh();
|
||||
|
||||
const bench = common.createBenchmark(main, {
|
||||
type: ['existing', 'non-existing'],
|
||||
n: [1e3],
|
||||
});
|
||||
|
||||
function main({ n, type }) {
|
||||
let files;
|
||||
|
||||
switch (type) {
|
||||
case 'existing':
|
||||
files = [];
|
||||
|
||||
// Populate tmpdir with mock files
|
||||
for (let i = 0; i < n; i++) {
|
||||
const path = tmpdir.resolve(`unlinksync-bench-file-${i}`);
|
||||
fs.writeFileSync(path, 'bench');
|
||||
files.push(path);
|
||||
}
|
||||
break;
|
||||
case 'non-existing':
|
||||
files = new Array(n).fill(tmpdir.resolve(`.non-existing-file-${Date.now()}`));
|
||||
break;
|
||||
default:
|
||||
new Error('Invalid type');
|
||||
}
|
||||
|
||||
bench.start();
|
||||
for (let i = 0; i < n; i++) {
|
||||
try {
|
||||
fs.unlinkSync(files[i]);
|
||||
} catch {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
bench.end(n);
|
||||
}
|
@ -1850,10 +1850,7 @@ function unlink(path, callback) {
|
||||
* @returns {void}
|
||||
*/
|
||||
function unlinkSync(path) {
|
||||
path = getValidatedPath(path);
|
||||
const ctx = { path };
|
||||
binding.unlink(pathModule.toNamespacedPath(path), undefined, ctx);
|
||||
handleErrorFromBinding(ctx);
|
||||
return syncFs.unlink(path);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -88,6 +88,11 @@ function close(fd) {
|
||||
return binding.closeSync(fd);
|
||||
}
|
||||
|
||||
function unlink(path) {
|
||||
path = pathModule.toNamespacedPath(getValidatedPath(path));
|
||||
return binding.unlinkSync(path);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
readFileUtf8,
|
||||
exists,
|
||||
@ -97,4 +102,5 @@ module.exports = {
|
||||
statfs,
|
||||
open,
|
||||
close,
|
||||
unlink,
|
||||
};
|
||||
|
@ -1667,6 +1667,27 @@ static void Unlink(const FunctionCallbackInfo<Value>& args) {
|
||||
}
|
||||
}
|
||||
|
||||
static void UnlinkSync(const FunctionCallbackInfo<Value>& args) {
|
||||
Environment* env = Environment::GetCurrent(args);
|
||||
|
||||
const int argc = args.Length();
|
||||
CHECK_GE(argc, 1);
|
||||
|
||||
BufferValue path(env->isolate(), args[0]);
|
||||
CHECK_NOT_NULL(*path);
|
||||
THROW_IF_INSUFFICIENT_PERMISSIONS(
|
||||
env, permission::PermissionScope::kFileSystemWrite, path.ToStringView());
|
||||
|
||||
uv_fs_t req;
|
||||
auto make = OnScopeLeave([&req]() { uv_fs_req_cleanup(&req); });
|
||||
FS_SYNC_TRACE_BEGIN(unlink);
|
||||
int err = uv_fs_unlink(nullptr, &req, *path, nullptr);
|
||||
FS_SYNC_TRACE_END(unlink);
|
||||
if (err < 0) {
|
||||
return env->ThrowUVException(err, "unlink", nullptr, *path);
|
||||
}
|
||||
}
|
||||
|
||||
static void RMDir(const FunctionCallbackInfo<Value>& args) {
|
||||
Environment* env = Environment::GetCurrent(args);
|
||||
|
||||
@ -3390,6 +3411,7 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data,
|
||||
SetMethod(isolate, target, "symlink", Symlink);
|
||||
SetMethod(isolate, target, "readlink", ReadLink);
|
||||
SetMethod(isolate, target, "unlink", Unlink);
|
||||
SetMethod(isolate, target, "unlinkSync", UnlinkSync);
|
||||
SetMethod(isolate, target, "writeBuffer", WriteBuffer);
|
||||
SetMethod(isolate, target, "writeBuffers", WriteBuffers);
|
||||
SetMethod(isolate, target, "writeString", WriteString);
|
||||
@ -3515,6 +3537,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
|
||||
registry->Register(Symlink);
|
||||
registry->Register(ReadLink);
|
||||
registry->Register(Unlink);
|
||||
registry->Register(UnlinkSync);
|
||||
registry->Register(WriteBuffer);
|
||||
registry->Register(WriteBuffers);
|
||||
registry->Register(WriteString);
|
||||
|
Loading…
Reference in New Issue
Block a user