Skip to content

Commit 7eda099

Browse files
committed
fs: improve mkdtemp performance for buffer prefix
1 parent 5ac6581 commit 7eda099

File tree

3 files changed

+50
-17
lines changed

3 files changed

+50
-17
lines changed

benchmark/fs/bench-mkdtempSync.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 assert = require('assert');
6+
const tmpdir = require('../../test/common/tmpdir');
7+
8+
const bench = common.createBenchmark(main, {
9+
type: ['valid-string', 'valid-buffer', 'invalid'],
10+
n: [1e4],
11+
});
12+
13+
function main({ n, type }) {
14+
tmpdir.refresh();
15+
const options = { encoding: 'utf8' };
16+
let prefix;
17+
let out = true;
18+
19+
switch (type) {
20+
case 'valid-string':
21+
prefix = tmpdir.resolve(`${Date.now()}`);
22+
break;
23+
case 'valid-buffer':
24+
prefix = Buffer.from(tmpdir.resolve(`${Date.now()}`));
25+
break;
26+
case 'invalid':
27+
prefix = tmpdir.resolve('non-existent', 'foo', 'bar');
28+
break;
29+
default:
30+
new Error('Invalid type');
31+
}
32+
33+
bench.start();
34+
for (let i = 0; i < n; i++) {
35+
try {
36+
out = fs.mkdtempSync(prefix, options);
37+
} catch {
38+
// do nothing
39+
}
40+
}
41+
bench.end(n);
42+
assert.ok(out);
43+
}

lib/fs.js

+2-17
Original file line numberDiff line numberDiff line change
@@ -2948,16 +2948,9 @@ function mkdtemp(prefix, options, callback) {
29482948
prefix = getValidatedPath(prefix, 'prefix');
29492949
warnOnNonPortableTemplate(prefix);
29502950

2951-
let path;
2952-
if (typeof prefix === 'string') {
2953-
path = `${prefix}XXXXXX`;
2954-
} else {
2955-
path = Buffer.concat([prefix, Buffer.from('XXXXXX')]);
2956-
}
2957-
29582951
const req = new FSReqCallback();
29592952
req.oncomplete = callback;
2960-
binding.mkdtemp(path, options.encoding, req);
2953+
binding.mkdtemp(prefix, options.encoding, req);
29612954
}
29622955

29632956
/**
@@ -2971,15 +2964,7 @@ function mkdtempSync(prefix, options) {
29712964

29722965
prefix = getValidatedPath(prefix, 'prefix');
29732966
warnOnNonPortableTemplate(prefix);
2974-
2975-
let path;
2976-
if (typeof prefix === 'string') {
2977-
path = `${prefix}XXXXXX`;
2978-
} else {
2979-
path = Buffer.concat([prefix, Buffer.from('XXXXXX')]);
2980-
}
2981-
2982-
return binding.mkdtemp(path, options.encoding);
2967+
return binding.mkdtemp(prefix, options.encoding);
29832968
}
29842969

29852970
/**

src/node_file.cc

+5
Original file line numberDiff line numberDiff line change
@@ -2737,6 +2737,11 @@ static void Mkdtemp(const FunctionCallbackInfo<Value>& args) {
27372737
CHECK_GE(argc, 2);
27382738

27392739
BufferValue tmpl(isolate, args[0]);
2740+
static constexpr const char* const suffix = "XXXXXX";
2741+
const auto length = tmpl.length();
2742+
tmpl.AllocateSufficientStorage(length + strlen(suffix));
2743+
snprintf(tmpl.out() + length, tmpl.length(), "%s", suffix);
2744+
27402745
CHECK_NOT_NULL(*tmpl);
27412746
THROW_IF_INSUFFICIENT_PERMISSIONS(
27422747
env, permission::PermissionScope::kFileSystemWrite, tmpl.ToStringView());

0 commit comments

Comments
 (0)