Skip to content

Commit 8df3dfe

Browse files
committed
fs: improve mkdtemp performance for buffer prefix
1 parent 727dd28 commit 8df3dfe

File tree

3 files changed

+46
-17
lines changed

3 files changed

+46
-17
lines changed

benchmark/fs/bench-mkdtempSync.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const fs = require('fs');
5+
const tmpdir = require('../../test/common/tmpdir');
6+
7+
const bench = common.createBenchmark(main, {
8+
type: ['valid-string', 'valid-buffer', 'invalid'],
9+
n: [1e3],
10+
});
11+
12+
function main({ n, type }) {
13+
tmpdir.refresh();
14+
let prefix;
15+
16+
switch (type) {
17+
case 'valid-string':
18+
prefix = tmpdir.resolve(`${Date.now()}`);
19+
break;
20+
case 'valid-buffer':
21+
prefix = Buffer.from(tmpdir.resolve(`${Date.now()}`));
22+
break;
23+
case 'invalid':
24+
prefix = tmpdir.resolve('non-existent', 'foo', 'bar');
25+
break;
26+
default:
27+
new Error('Invalid type');
28+
}
29+
30+
bench.start();
31+
for (let i = 0; i < n; i++) {
32+
try {
33+
fs.mkdtempSync(prefix, { encoding: 'utf8' });
34+
} catch {
35+
// do nothing
36+
}
37+
}
38+
bench.end(n);
39+
}

lib/fs.js

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2950,16 +2950,9 @@ function mkdtemp(prefix, options, callback) {
29502950
prefix = getValidatedPath(prefix, 'prefix');
29512951
warnOnNonPortableTemplate(prefix);
29522952

2953-
let path;
2954-
if (typeof prefix === 'string') {
2955-
path = `${prefix}XXXXXX`;
2956-
} else {
2957-
path = Buffer.concat([prefix, Buffer.from('XXXXXX')]);
2958-
}
2959-
29602953
const req = new FSReqCallback();
29612954
req.oncomplete = callback;
2962-
binding.mkdtemp(path, options.encoding, req);
2955+
binding.mkdtemp(prefix, options.encoding, req);
29632956
}
29642957

29652958
/**
@@ -2973,15 +2966,7 @@ function mkdtempSync(prefix, options) {
29732966

29742967
prefix = getValidatedPath(prefix, 'prefix');
29752968
warnOnNonPortableTemplate(prefix);
2976-
2977-
let path;
2978-
if (typeof prefix === 'string') {
2979-
path = `${prefix}XXXXXX`;
2980-
} else {
2981-
path = Buffer.concat([prefix, Buffer.from('XXXXXX')]);
2982-
}
2983-
2984-
return binding.mkdtemp(path, options.encoding);
2969+
return binding.mkdtemp(prefix, options.encoding);
29852970
}
29862971

29872972
/**

src/node_file.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2748,6 +2748,11 @@ static void Mkdtemp(const FunctionCallbackInfo<Value>& args) {
27482748
CHECK_GE(argc, 2);
27492749

27502750
BufferValue tmpl(isolate, args[0]);
2751+
const char* ex = "XXXXXX";
2752+
const auto length = tmpl.length();
2753+
tmpl.AllocateSufficientStorage(length + strlen(ex));
2754+
snprintf(tmpl.out() + length, length, "%s", ex);
2755+
27512756
CHECK_NOT_NULL(*tmpl);
27522757
THROW_IF_INSUFFICIENT_PERMISSIONS(
27532758
env, permission::PermissionScope::kFileSystemWrite, tmpl.ToStringView());

0 commit comments

Comments
 (0)