GeezerVison is showing github.com — fs: improve error perf of sync `*times` · nodejs/node@8413670
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 +61Lines changed: 61 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 | + func: [ 'utimes', 'futimes', 'lutimes' ], | | 11 | + n: [1e3], | | 12 | +}); | | 13 | + | | 14 | +function main({ n, type, func }) { | | 15 | + const useFds = func === 'futimes'; | | 16 | + const fsFunc = fs[func + 'Sync']; | | 17 | + | | 18 | + switch (type) { | | 19 | + case 'existing': { | | 20 | + const files = []; | | 21 | + | | 22 | + // Populate tmpdir with mock files | | 23 | + for (let i = 0; i < n; i++) { | | 24 | + const path = tmpdir.resolve(`timessync-bench-file-${i}`); | | 25 | + fs.writeFileSync(path, 'bench'); | | 26 | + files.push(useFds ? fs.openSync(path, 'r+') : path); | | 27 | + } | | 28 | + | | 29 | + bench.start(); | | 30 | + for (let i = 0; i < n; i++) { | | 31 | + fsFunc(files[i], i, i); | | 32 | + } | | 33 | + bench.end(n); | | 34 | + | | 35 | + if (useFds) files.forEach((x) => { | | 36 | + try { | | 37 | + fs.closeSync(x); | | 38 | + } catch { | | 39 | + // do nothing | | 40 | + } | | 41 | + }); | | 42 | + | | 43 | + break; | | 44 | + } | | 45 | + case 'non-existing': { | | 46 | + bench.start(); | | 47 | + for (let i = 0; i < n; i++) { | | 48 | + try { | | 49 | + fsFunc(useFds ? (1 << 30) : tmpdir.resolve(`.non-existing-file-${Date.now()}`), i, i); | | 50 | + } catch { | | 51 | + // do nothing | | 52 | + } | | 53 | + } | | 54 | + bench.end(n); | | 55 | + | | 56 | + break; | | 57 | + } | | 58 | + default: | | 59 | + new Error('Invalid type'); | | 60 | + } | | 61 | +} |
Collapse file Copy file name to clipboardExpand all lines: lib/fs.js +15-17Lines changed: 15 additions & 17 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2123,11 +2123,11 @@ function utimes(path, atime, mtime, callback) { | 2123 | 2123 | */ | 2124 | 2124 | function utimesSync(path, atime, mtime) { | 2125 | 2125 | path = getValidatedPath(path); | 2126 | | - const ctx = { path }; | 2127 | | - binding.utimes(pathModule.toNamespacedPath(path), | 2128 | | - toUnixTimestamp(atime), toUnixTimestamp(mtime), | 2129 | | - undefined, ctx); | 2130 | | - handleErrorFromBinding(ctx); | | 2126 | + binding.utimes( | | 2127 | + pathModule.toNamespacedPath(path), | | 2128 | + toUnixTimestamp(atime), | | 2129 | + toUnixTimestamp(mtime), | | 2130 | + ); | 2131 | 2131 | } | 2132 | 2132 |
| 2133 | 2133 | /** | @@ -2160,12 +2160,11 @@ function futimes(fd, atime, mtime, callback) { | 2160 | 2160 | * @returns {void} | 2161 | 2161 | */ | 2162 | 2162 | function futimesSync(fd, atime, mtime) { | 2163 | | - fd = getValidatedFd(fd); | 2164 | | - atime = toUnixTimestamp(atime, 'atime'); | 2165 | | - mtime = toUnixTimestamp(mtime, 'mtime'); | 2166 | | - const ctx = {}; | 2167 | | - binding.futimes(fd, atime, mtime, undefined, ctx); | 2168 | | - handleErrorFromBinding(ctx); | | 2163 | + binding.futimes( | | 2164 | + getValidatedFd(fd), | | 2165 | + toUnixTimestamp(atime, 'atime'), | | 2166 | + toUnixTimestamp(mtime, 'mtime'), | | 2167 | + ); | 2169 | 2168 | } | 2170 | 2169 |
| 2171 | 2170 | /** | @@ -2199,12 +2198,11 @@ function lutimes(path, atime, mtime, callback) { | 2199 | 2198 | */ | 2200 | 2199 | function lutimesSync(path, atime, mtime) { | 2201 | 2200 | path = getValidatedPath(path); | 2202 | | - const ctx = { path }; | 2203 | | - binding.lutimes(pathModule.toNamespacedPath(path), | 2204 | | - toUnixTimestamp(atime), | 2205 | | - toUnixTimestamp(mtime), | 2206 | | - undefined, ctx); | 2207 | | - handleErrorFromBinding(ctx); | | 2201 | + binding.lutimes( | | 2202 | + pathModule.toNamespacedPath(path), | | 2203 | + toUnixTimestamp(atime), | | 2204 | + toUnixTimestamp(mtime), | | 2205 | + ); | 2208 | 2206 | } | 2209 | 2207 |
| 2210 | 2208 | function writeAll(fd, isUserFd, buffer, offset, length, signal, flush, callback) { | |
Collapse file Copy file name to clipboardExpand all lines: src/node_file.cc +18-21Lines changed: 18 additions & 21 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2721,18 +2721,17 @@ static void UTimes(const FunctionCallbackInfo<Value>& args) { | 2721 | 2721 | CHECK(args[2]->IsNumber()); | 2722 | 2722 | const double mtime = args[2].As<Number>()->Value(); | 2723 | 2723 |
| 2724 | | - FSReqBase* req_wrap_async = GetReqWrap(args, 3); | 2725 | | - if (req_wrap_async != nullptr) { // utimes(path, atime, mtime, req) | | 2724 | + if (argc > 3) { // utimes(path, atime, mtime, req) | | 2725 | + FSReqBase* req_wrap_async = GetReqWrap(args, 3); | 2726 | 2726 | FS_ASYNC_TRACE_BEGIN1( | 2727 | 2727 | UV_FS_UTIME, req_wrap_async, "path", TRACE_STR_COPY(*path)) | 2728 | 2728 | AsyncCall(env, req_wrap_async, args, "utime", UTF8, AfterNoArgs, | 2729 | 2729 | uv_fs_utime, *path, atime, mtime); | 2730 | | - } else { // utimes(path, atime, mtime, undefined, ctx) | 2731 | | - CHECK_EQ(argc, 5); | 2732 | | - FSReqWrapSync req_wrap_sync; | | 2730 | + } else { // utimes(path, atime, mtime) | | 2731 | + FSReqWrapSync req_wrap_sync("utime", *path); | 2733 | 2732 | FS_SYNC_TRACE_BEGIN(utimes); | 2734 | | - SyncCall(env, args[4], &req_wrap_sync, "utime", | 2735 | | - uv_fs_utime, *path, atime, mtime); | | 2733 | + SyncCallAndThrowOnError( | | 2734 | + env, &req_wrap_sync, uv_fs_utime, *path, atime, mtime); | 2736 | 2735 | FS_SYNC_TRACE_END(utimes); | 2737 | 2736 | } | 2738 | 2737 | } | @@ -2752,17 +2751,16 @@ static void FUTimes(const FunctionCallbackInfo<Value>& args) { | 2752 | 2751 | CHECK(args[2]->IsNumber()); | 2753 | 2752 | const double mtime = args[2].As<Number>()->Value(); | 2754 | 2753 |
| 2755 | | - FSReqBase* req_wrap_async = GetReqWrap(args, 3); | 2756 | | - if (req_wrap_async != nullptr) { // futimes(fd, atime, mtime, req) | | 2754 | + if (argc > 3) { // futimes(fd, atime, mtime, req) | | 2755 | + FSReqBase* req_wrap_async = GetReqWrap(args, 3); | 2757 | 2756 | FS_ASYNC_TRACE_BEGIN0(UV_FS_FUTIME, req_wrap_async) | 2758 | 2757 | AsyncCall(env, req_wrap_async, args, "futime", UTF8, AfterNoArgs, | 2759 | 2758 | uv_fs_futime, fd, atime, mtime); | 2760 | | - } else { // futimes(fd, atime, mtime, undefined, ctx) | 2761 | | - CHECK_EQ(argc, 5); | 2762 | | - FSReqWrapSync req_wrap_sync; | | 2759 | + } else { // futimes(fd, atime, mtime) | | 2760 | + FSReqWrapSync req_wrap_sync("futime"); | 2763 | 2761 | FS_SYNC_TRACE_BEGIN(futimes); | 2764 | | - SyncCall(env, args[4], &req_wrap_sync, "futime", | 2765 | | - uv_fs_futime, fd, atime, mtime); | | 2762 | + SyncCallAndThrowOnError( | | 2763 | + env, &req_wrap_sync, uv_fs_futime, fd, atime, mtime); | 2766 | 2764 | FS_SYNC_TRACE_END(futimes); | 2767 | 2765 | } | 2768 | 2766 | } | @@ -2784,18 +2782,17 @@ static void LUTimes(const FunctionCallbackInfo<Value>& args) { | 2784 | 2782 | CHECK(args[2]->IsNumber()); | 2785 | 2783 | const double mtime = args[2].As<Number>()->Value(); | 2786 | 2784 |
| 2787 | | - FSReqBase* req_wrap_async = GetReqWrap(args, 3); | 2788 | | - if (req_wrap_async != nullptr) { // lutimes(path, atime, mtime, req) | | 2785 | + if (argc > 3) { // lutimes(path, atime, mtime, req) | | 2786 | + FSReqBase* req_wrap_async = GetReqWrap(args, 3); | 2789 | 2787 | FS_ASYNC_TRACE_BEGIN1( | 2790 | 2788 | UV_FS_LUTIME, req_wrap_async, "path", TRACE_STR_COPY(*path)) | 2791 | 2789 | AsyncCall(env, req_wrap_async, args, "lutime", UTF8, AfterNoArgs, | 2792 | 2790 | uv_fs_lutime, *path, atime, mtime); | 2793 | | - } else { // lutimes(path, atime, mtime, undefined, ctx) | 2794 | | - CHECK_EQ(argc, 5); | 2795 | | - FSReqWrapSync req_wrap_sync; | | 2791 | + } else { // lutimes(path, atime, mtime) | | 2792 | + FSReqWrapSync req_wrap_sync("lutime", *path); | 2796 | 2793 | FS_SYNC_TRACE_BEGIN(lutimes); | 2797 | | - SyncCall(env, args[4], &req_wrap_sync, "lutime", | 2798 | | - uv_fs_lutime, *path, atime, mtime); | | 2794 | + SyncCallAndThrowOnError( | | 2795 | + env, &req_wrap_sync, uv_fs_lutime, *path, atime, mtime); | 2799 | 2796 | FS_SYNC_TRACE_END(lutimes); | 2800 | 2797 | } | 2801 | 2798 | } | |
Collapse file Copy file name to clipboardExpand all lines: typings/internalBinding/fs.d.ts +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -107,7 +107,7 @@ declare namespace InternalFSBinding { | 107 | 107 | function ftruncate(fd: number, len: number, usePromises: typeof kUsePromises): Promise<void>; | 108 | 108 |
| 109 | 109 | function futimes(fd: number, atime: number, mtime: number, req: FSReqCallback): void; | 110 | | - function futimes(fd: number, atime: number, mtime: number, req: undefined, ctx: FSSyncContext): void; | | 110 | + function futimes(fd: number, atime: number, mtime: number): void; | 111 | 111 | function futimes(fd: number, atime: number, mtime: number, usePromises: typeof kUsePromises): Promise<void>; | 112 | 112 |
| 113 | 113 | function internalModuleReadJSON(path: string): [] | [string, boolean]; | @@ -132,7 +132,7 @@ declare namespace InternalFSBinding { | 132 | 132 | function lstat(path: StringOrBuffer, useBigint: false, usePromises: typeof kUsePromises): Promise<Float64Array>; | 133 | 133 |
| 134 | 134 | function lutimes(path: string, atime: number, mtime: number, req: FSReqCallback): void; | 135 | | - function lutimes(path: string, atime: number, mtime: number, req: undefined, ctx: FSSyncContext): void; | | 135 | + function lutimes(path: string, atime: number, mtime: number): void; | 136 | 136 | function lutimes(path: string, atime: number, mtime: number, usePromises: typeof kUsePromises): Promise<void>; | 137 | 137 |
| 138 | 138 | function mkdtemp(prefix: string, encoding: unknown, req: FSReqCallback<string>): void; | @@ -207,7 +207,7 @@ declare namespace InternalFSBinding { | 207 | 207 | function unlink(path: string, usePromises: typeof kUsePromises): Promise<void>; | 208 | 208 |
| 209 | 209 | function utimes(path: string, atime: number, mtime: number, req: FSReqCallback): void; | 210 | | - function utimes(path: string, atime: number, mtime: number, req: undefined, ctx: FSSyncContext): void; | | 210 | + function utimes(path: string, atime: number, mtime: number): void; | 211 | 211 | function utimes(path: string, atime: number, mtime: number, usePromises: typeof kUsePromises): Promise<void>; | 212 | 212 |
| 213 | 213 | function writeBuffer(fd: number, buffer: ArrayBufferView, offset: number, length: number, position: number | null, req: FSReqCallback<number>): void; | |
|