GeezerVison is showing github.com — fs: improve error performance for `rmdirSync` · nodejs/node@f94a24c
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +42Lines changed: 42 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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: [1e4], | | 11 | +}); | | 12 | + | | 13 | +function main({ n, type }) { | | 14 | + switch (type) { | | 15 | + case 'existing': { | | 16 | + for (let i = 0; i < n; i++) { | | 17 | + fs.mkdirSync(tmpdir.resolve(`rmdirsync-bench-dir-${i}`)); | | 18 | + } | | 19 | + | | 20 | + bench.start(); | | 21 | + for (let i = 0; i < n; i++) { | | 22 | + fs.rmdirSync(tmpdir.resolve(`rmdirsync-bench-dir-${i}`)); | | 23 | + } | | 24 | + bench.end(n); | | 25 | + break; | | 26 | + } | | 27 | + case 'non-existing': { | | 28 | + bench.start(); | | 29 | + for (let i = 0; i < n; i++) { | | 30 | + try { | | 31 | + fs.rmdirSync(tmpdir.resolve(`.non-existent-folder-${i}`)); | | 32 | + } catch { | | 33 | + // do nothing | | 34 | + } | | 35 | + } | | 36 | + bench.end(n); | | 37 | + break; | | 38 | + } | | 39 | + default: | | 40 | + new Error('Invalid type'); | | 41 | + } | | 42 | +} |
Collapse file Copy file name to clipboardExpand all lines: lib/fs.js +1-3Lines changed: 1 addition & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1220,9 +1220,7 @@ function rmdirSync(path, options) { | 1220 | 1220 | validateRmdirOptions(options); | 1221 | 1221 | } | 1222 | 1222 |
| 1223 | | - const ctx = { path }; | 1224 | | - binding.rmdir(pathModule.toNamespacedPath(path), undefined, ctx); | 1225 | | - return handleErrorFromBinding(ctx); | | 1223 | + binding.rmdir(pathModule.toNamespacedPath(path)); | 1226 | 1224 | } | 1227 | 1225 |
| 1228 | 1226 | /** | |
Collapse file Copy file name to clipboardExpand all lines: src/node_file.cc +6-8Lines changed: 6 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1588,25 +1588,23 @@ static void RMDir(const FunctionCallbackInfo<Value>& args) { | 1588 | 1588 | Environment* env = Environment::GetCurrent(args); | 1589 | 1589 |
| 1590 | 1590 | const int argc = args.Length(); | 1591 | | - CHECK_GE(argc, 2); | | 1591 | + CHECK_GE(argc, 1); | 1592 | 1592 |
| 1593 | 1593 | BufferValue path(env->isolate(), args[0]); | 1594 | 1594 | CHECK_NOT_NULL(*path); | 1595 | 1595 | THROW_IF_INSUFFICIENT_PERMISSIONS( | 1596 | 1596 | env, permission::PermissionScope::kFileSystemWrite, path.ToStringView()); | 1597 | 1597 |
| 1598 | | - FSReqBase* req_wrap_async = GetReqWrap(args, 1); // rmdir(path, req) | 1599 | | - if (req_wrap_async != nullptr) { | | 1598 | + if (argc > 1) { | | 1599 | + FSReqBase* req_wrap_async = GetReqWrap(args, 1); // rmdir(path, req) | 1600 | 1600 | FS_ASYNC_TRACE_BEGIN1( | 1601 | 1601 | UV_FS_RMDIR, req_wrap_async, "path", TRACE_STR_COPY(*path)) | 1602 | 1602 | AsyncCall(env, req_wrap_async, args, "rmdir", UTF8, AfterNoArgs, | 1603 | 1603 | uv_fs_rmdir, *path); | 1604 | | - } else { // rmdir(path, undefined, ctx) | 1605 | | - CHECK_EQ(argc, 3); | 1606 | | - FSReqWrapSync req_wrap_sync; | | 1604 | + } else { // rmdir(path) | | 1605 | + FSReqWrapSync req_wrap_sync("rmdir", *path); | 1607 | 1606 | FS_SYNC_TRACE_BEGIN(rmdir); | 1608 | | - SyncCall(env, args[2], &req_wrap_sync, "rmdir", | 1609 | | - uv_fs_rmdir, *path); | | 1607 | + SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_rmdir, *path); | 1610 | 1608 | FS_SYNC_TRACE_END(rmdir); | 1611 | 1609 | } | 1612 | 1610 | } | |
Collapse file Copy file name to clipboardExpand all lines: typings/internalBinding/fs.d.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -192,7 +192,7 @@ declare namespace InternalFSBinding { | 192 | 192 | function rename(oldPath: string, newPath: string): void; | 193 | 193 |
| 194 | 194 | function rmdir(path: string, req: FSReqCallback): void; | 195 | | - function rmdir(path: string, req: undefined, ctx: FSSyncContext): void; | | 195 | + function rmdir(path: string): void; | 196 | 196 | function rmdir(path: string, usePromises: typeof kUsePromises): Promise<void>; | 197 | 197 |
| 198 | 198 | function stat(path: StringOrBuffer, useBigint: boolean, req: FSReqCallback<Float64Array | BigUint64Array>): void; | |
|