fs: write files in one thread pool round trip - #65489
Open
codebytere wants to merge 1 commit into
Open
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #65489 +/- ##
==========================================
+ Coverage 90.12% 90.14% +0.02%
==========================================
Files 752 751 -1
Lines 252315 252677 +362
Branches 47444 47540 +96
==========================================
+ Hits 227395 227781 +386
+ Misses 16217 16186 -31
- Partials 8703 8710 +7
🚀 New features to boost your workflow:
|
fs.writeFile(path, data) took three libuv thread pool round trips (open, write, close), each its own request with its own queue wait, completion callback and JS/C++ crossing, and fs.promises.writeFile() did the same through a FileHandle. For the small files applications write most, the round trips are the cost, and each occupies a pool slot that concurrent fs, dns.lookup() and crypto work is also queueing for. Add WriteFileJob next to ReadFileJob: an AsyncWrap + ThreadPoolWork that opens, writes the whole buffer (looping on short writes) and closes as one pool task, keeping the buffer alive until it is done. fs.writeFile() uses it for path arguments without flush; fs.promises.writeFile() additionally keeps data above one write chunk (and iterables) on the FileHandle path, so large writes stay abortable between chunks as before. File descriptors, FileHandles, flush: true and an active VFS keep their existing paths. Behavior is otherwise kept: open failures report syscall 'open' with the path, write failures 'write'; permission errors are delivered through the callback/promise; an abort signalled while the write is in flight is still reported as an AbortError; the job is an FSREQCALLBACK resource for async_hooks and emits the 'write' fs trace event. Tests that used fs.writeFile() as a proxy for open/close trace events, or injected FileHandle faults for path-based writes, are adjusted to keep testing what they test. Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com>
codebytere
force-pushed
the
perf/fs-writefile-one-roundtrip
branch
from
August 22, 2026 18:17
9fb687f to
4c3fcd6
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Makes
fs.writeFile(path, data)andfsPromises.writeFile(path, data)do open + write + close as one thread pool request (WriteFileJob, next to theReadFileJobfrom #65327) instead of one request per step.benchmark/fs/writefile-promises.js(buf,duration=2), 20 runs, this branch vsmain;fs.writeFile()measured the same way with an equivalent callback loop:fsPromises.writeFile, 2 BfsPromises.writeFile, 1 KiBfsPromises.writeFile, 64 KiBfs.writeFile, 1 KiB(1 MiB stays on the chunked
FileHandlepath and is unchanged.)fs.writeFile()took three libuv round trips (open, write, close), each its own request with its own queue wait, completion callback and JS/C++ crossing, andfsPromises.writeFile()did the same through aFileHandle. For the small files applications write most (settings, state, caches) the round trips are the cost, and each one occupies a pool slot that concurrent fs,dns.lookup()and crypto work is also queueing for.WriteFileJob(anAsyncWrap+ThreadPoolWork) opens, writes the whole buffer (looping on short writes) and closes as one pool task, keeping the buffer alive until it is done.fs.writeFile()uses it for path arguments withoutflush;fsPromises.writeFile()additionally keeps data above one write chunk (512 KiB) and iterables on theFileHandlepath, so large writes stay abortable between chunks as before. File descriptors,FileHandles,flush: trueand an active VFS keep their existing paths. Open failures report syscallopenwith the path and write failureswrite, permission errors arrive through the callback/promise, an abort signalled while the write is in flight is still reported as anAbortError, and the job is anFSREQCALLBACKasync resource that emits thewritefs trace event.Refs: #65327
Tests: new
test-fs-writefile-one-roundtrip.js(flagsa/wx, mode, failing syscall and path, everyArrayBufferViewkind, a multi-megabyte buffer, both APIs); tests that usedfs.writeFile()as a proxy foropen/closetrace events or injectedFileHandlefaults into path-based writes are adjusted to keep testing what they test;test-fs-*write*,*append*,test-fs-promises*, permission and async-hooks fs tests pass.Disclosure: the code, test, measurements and this description were written by Claude Code, directed and reviewed by @codebytere.