Skip to content

Commit e82e46c

Browse files
CanadaHonkruyadorno
authored andcommitted
fs: improve error performance for unlinkSync
PR-URL: #49856 Refs: nodejs/performance#106 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent f40b5ed commit e82e46c

File tree

4 files changed

+73
-4
lines changed

4 files changed

+73
-4
lines changed

benchmark/fs/bench-unlinkSync.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
n: [1e3],
11+
});
12+
13+
function main({ n, type }) {
14+
let files;
15+
16+
switch (type) {
17+
case 'existing':
18+
files = [];
19+
20+
// Populate tmpdir with mock files
21+
for (let i = 0; i < n; i++) {
22+
const path = tmpdir.resolve(`unlinksync-bench-file-${i}`);
23+
fs.writeFileSync(path, 'bench');
24+
files.push(path);
25+
}
26+
break;
27+
case 'non-existing':
28+
files = new Array(n).fill(tmpdir.resolve(`.non-existing-file-${Date.now()}`));
29+
break;
30+
default:
31+
new Error('Invalid type');
32+
}
33+
34+
bench.start();
35+
for (let i = 0; i < n; i++) {
36+
try {
37+
fs.unlinkSync(files[i]);
38+
} catch {
39+
// do nothing
40+
}
41+
}
42+
bench.end(n);
43+
}

lib/fs.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -1850,10 +1850,7 @@ function unlink(path, callback) {
18501850
* @returns {void}
18511851
*/
18521852
function unlinkSync(path) {
1853-
path = getValidatedPath(path);
1854-
const ctx = { path };
1855-
binding.unlink(pathModule.toNamespacedPath(path), undefined, ctx);
1856-
handleErrorFromBinding(ctx);
1853+
return syncFs.unlink(path);
18571854
}
18581855

18591856
/**

lib/internal/fs/sync.js

+6
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ function close(fd) {
8888
return binding.closeSync(fd);
8989
}
9090

91+
function unlink(path) {
92+
path = pathModule.toNamespacedPath(getValidatedPath(path));
93+
return binding.unlinkSync(path);
94+
}
95+
9196
module.exports = {
9297
readFileUtf8,
9398
exists,
@@ -97,4 +102,5 @@ module.exports = {
97102
statfs,
98103
open,
99104
close,
105+
unlink,
100106
};

src/node_file.cc

+23
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,27 @@ static void Unlink(const FunctionCallbackInfo<Value>& args) {
17021702
}
17031703
}
17041704

1705+
static void UnlinkSync(const FunctionCallbackInfo<Value>& args) {
1706+
Environment* env = Environment::GetCurrent(args);
1707+
1708+
const int argc = args.Length();
1709+
CHECK_GE(argc, 1);
1710+
1711+
BufferValue path(env->isolate(), args[0]);
1712+
CHECK_NOT_NULL(*path);
1713+
THROW_IF_INSUFFICIENT_PERMISSIONS(
1714+
env, permission::PermissionScope::kFileSystemWrite, path.ToStringView());
1715+
1716+
uv_fs_t req;
1717+
auto make = OnScopeLeave([&req]() { uv_fs_req_cleanup(&req); });
1718+
FS_SYNC_TRACE_BEGIN(unlink);
1719+
int err = uv_fs_unlink(nullptr, &req, *path, nullptr);
1720+
FS_SYNC_TRACE_END(unlink);
1721+
if (err < 0) {
1722+
return env->ThrowUVException(err, "unlink", nullptr, *path);
1723+
}
1724+
}
1725+
17051726
static void RMDir(const FunctionCallbackInfo<Value>& args) {
17061727
Environment* env = Environment::GetCurrent(args);
17071728

@@ -3425,6 +3446,7 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data,
34253446
SetMethod(isolate, target, "symlink", Symlink);
34263447
SetMethod(isolate, target, "readlink", ReadLink);
34273448
SetMethod(isolate, target, "unlink", Unlink);
3449+
SetMethod(isolate, target, "unlinkSync", UnlinkSync);
34283450
SetMethod(isolate, target, "writeBuffer", WriteBuffer);
34293451
SetMethod(isolate, target, "writeBuffers", WriteBuffers);
34303452
SetMethod(isolate, target, "writeString", WriteString);
@@ -3550,6 +3572,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
35503572
registry->Register(Symlink);
35513573
registry->Register(ReadLink);
35523574
registry->Register(Unlink);
3575+
registry->Register(UnlinkSync);
35533576
registry->Register(WriteBuffer);
35543577
registry->Register(WriteBuffers);
35553578
registry->Register(WriteString);

0 commit comments

Comments
 (0)