Skip to content

Commit 1e854fe

Browse files
committed
perf(fmt): disable fsync for atomic writes
1 parent e3959bf commit 1e854fe

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

‎packages/rstack/src/fmt/worker.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ import { format } from 'prettier';
55
import { getPrettierPlugins } from './prettierPlugins.ts';
66
import type { FmtFileRequest } from './types.ts';
77

8+
/**
9+
* Formatting output can be regenerated, so avoid waiting for a durability sync
10+
* after every file, which is especially expensive during parallel formatting.
11+
* `atomically` still uses a temporary file and rename for atomic replacement.
12+
*/
13+
const atomicWriteOptions = {
14+
encoding: 'utf8',
15+
fsync: false,
16+
} as const;
17+
818
const formatFile = async (
919
{ path, options }: FmtFileRequest,
1020
shouldWrite: boolean,
@@ -20,7 +30,7 @@ const formatFile = async (
2030
}
2131

2232
if (shouldWrite) {
23-
await writeFile(path, formatted, 'utf8');
33+
await writeFile(path, formatted, atomicWriteOptions);
2434
}
2535

2636
return true;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { expect, rs, test } from 'rstack/test';
2+
import { formatFile } from '../../src/fmt/worker.ts';
3+
4+
const mocks = rs.hoisted(() => ({
5+
writeFileCalls: [] as [string, string, unknown][],
6+
}));
7+
8+
rs.mock('atomically', () => ({
9+
readFile: () => Promise.resolve('const value=1'),
10+
writeFile: (path: string, data: string, options: unknown) => {
11+
mocks.writeFileCalls.push([path, data, options]);
12+
return Promise.resolve();
13+
},
14+
}));
15+
16+
test('disables fsync for atomic writes', async () => {
17+
const filePath = '/virtual/example.ts';
18+
19+
await expect(
20+
formatFile(
21+
{
22+
path: filePath,
23+
options: {
24+
filepath: filePath,
25+
parser: 'typescript',
26+
},
27+
},
28+
true,
29+
),
30+
).resolves.toBe(true);
31+
32+
expect(mocks.writeFileCalls).toEqual([
33+
[filePath, 'const value = 1;\n', { encoding: 'utf8', fsync: false }],
34+
]);
35+
});

0 commit comments

Comments
 (0)