Skip to content

Commit

Permalink
fs: improve error performance for rmdirSync
Browse files Browse the repository at this point in the history
PR-URL: #49846
Refs: nodejs/performance#106
Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
  • Loading branch information
CanadaHonk authored and RafaelGSS committed Nov 30, 2023
1 parent e48180b commit d44dd9b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 12 deletions.
42 changes: 42 additions & 0 deletions benchmark/fs/bench-rmdirSync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'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: [1e4],
});

function main({ n, type }) {
switch (type) {
case 'existing': {
for (let i = 0; i < n; i++) {
fs.mkdirSync(tmpdir.resolve(`rmdirsync-bench-dir-${i}`));
}

bench.start();
for (let i = 0; i < n; i++) {
fs.rmdirSync(tmpdir.resolve(`rmdirsync-bench-dir-${i}`));
}
bench.end(n);
break;
}
case 'non-existing': {
bench.start();
for (let i = 0; i < n; i++) {
try {
fs.rmdirSync(tmpdir.resolve(`.non-existent-folder-${i}`));
} catch {
// do nothing
}
}
bench.end(n);
break;
}
default:
new Error('Invalid type');
}
}
4 changes: 1 addition & 3 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1218,9 +1218,7 @@ function rmdirSync(path, options) {
validateRmdirOptions(options);
}

const ctx = { path };
binding.rmdir(pathModule.toNamespacedPath(path), undefined, ctx);
return handleErrorFromBinding(ctx);
binding.rmdir(pathModule.toNamespacedPath(path));
}

/**
Expand Down
14 changes: 6 additions & 8 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1562,25 +1562,23 @@ static void RMDir(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

const int argc = args.Length();
CHECK_GE(argc, 2);
CHECK_GE(argc, 1);

BufferValue path(env->isolate(), args[0]);
CHECK_NOT_NULL(*path);
THROW_IF_INSUFFICIENT_PERMISSIONS(
env, permission::PermissionScope::kFileSystemWrite, path.ToStringView());

FSReqBase* req_wrap_async = GetReqWrap(args, 1); // rmdir(path, req)
if (req_wrap_async != nullptr) {
if (argc > 1) {
FSReqBase* req_wrap_async = GetReqWrap(args, 1); // rmdir(path, req)
FS_ASYNC_TRACE_BEGIN1(
UV_FS_RMDIR, req_wrap_async, "path", TRACE_STR_COPY(*path))
AsyncCall(env, req_wrap_async, args, "rmdir", UTF8, AfterNoArgs,
uv_fs_rmdir, *path);
} else { // rmdir(path, undefined, ctx)
CHECK_EQ(argc, 3);
FSReqWrapSync req_wrap_sync;
} else { // rmdir(path)
FSReqWrapSync req_wrap_sync("rmdir", *path);
FS_SYNC_TRACE_BEGIN(rmdir);
SyncCall(env, args[2], &req_wrap_sync, "rmdir",
uv_fs_rmdir, *path);
SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_rmdir, *path);
FS_SYNC_TRACE_END(rmdir);
}
}
Expand Down
2 changes: 1 addition & 1 deletion typings/internalBinding/fs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ declare namespace InternalFSBinding {
function rename(oldPath: string, newPath: string): void;

function rmdir(path: string, req: FSReqCallback): void;
function rmdir(path: string, req: undefined, ctx: FSSyncContext): void;
function rmdir(path: string): void;
function rmdir(path: string, usePromises: typeof kUsePromises): Promise<void>;

function stat(path: StringOrBuffer, useBigint: boolean, req: FSReqCallback<Float64Array | BigUint64Array>): void;
Expand Down

0 comments on commit d44dd9b

Please sign in to comment.