fs: add c++ fast path for writeFileSync string utf8#49884
Merged
nodejs-github-bot merged 1 commit intonodejs:mainfrom Nov 27, 2023
Merged
fs: add c++ fast path for writeFileSync string utf8#49884nodejs-github-bot merged 1 commit intonodejs:mainfrom
nodejs-github-bot merged 1 commit intonodejs:mainfrom
Conversation
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.
Summary
Added fast path in almost entirely C++ for
writeFileSyncwith UTF8 encoding and string data. Also improvesappendFileSyncas it just useswriteFileSyncunder the hood. Only string data as Buffer seems questionable in benchmarks for this so I'll just leave it for strings for now.TL;DR: This makes
writeFileSync(path, data: string)UTF8 up to ~2.5x faster depending on data size, especially when using file descriptorsBench results
Benchmark in this PR
Note: running locally on Linux/i9/SSD
Benchmark CI (old): https://ci.nodejs.org/view/Node.js%20benchmark/job/benchmark-node-micro-benchmarks/1421/
Alternative benchmark
Alternative benchmark using Bun's bench for
fs.copyFileSync(string data, using paths)Current (main)
This PR
Long ascii:
6.14µs->3.46µs(~1.8x speedup)Long utf8:
40.02µs->18.25µs(~2.2x speedup)