Optimize fd_write for Performance Improvements #205
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.
Problem Statement:
File IO benchmarks show low throughput in the
fd_write
function. This issue is partially caused by frequent buffer allocations and data copying when appending data to a file atvscode-wasm/wasm-wasi-core/src/common/vscodeFileSystemDriver.ts:544
. Each write allocates a new buffer and copies the entire array, resulting in significant performance degradation.Proposed Solution:
Utilizing ECMAScript 2024 In-Place Resizable ArrayBuffers, instead of allocating a new buffer with each append, we can allocate double the necessary space, similar to the C++
std::vector
approach.Outcome:
The following C program compiled by wasi-sdk, which sequentially writes random data to a file. The total runtime for creating an 8MB file dropped from 60 seconds to 15 seconds on an older platform.
Future Improvements:
There is still an IO bottleneck due to non-negligible thread switching overhead. Further improvements can be made by implementing caching for read operations and batching write requests.