emscripten-core / emscripten-core/emscripten
FetchFS: Consider obtaining file size and server capabilities from first chunk request
- Dominant language
- C++
- Stars
- 27.6k
- Forks
- 3.6k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 105
Description
**Version of emscripten/emsdk:**
```
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 4.0.9 (bbf1caa6e24f64fca9eb6a13a9e02d3f42123e77)
clang version 21.0.0git (https:/github.com/llvm/llvm-project 2f05451198e2f222ec66cec4892ada0509519290)
Target: wasm32-unknown-emscripten
Thread model: posix
```
The [`getFileRange` function](https://github.com/emscripten-core/emscripten/blob/1d35dfbd0a48a48c203da01bb5c1f8ec3a6e2047/src/lib/libwasmfs_fetch.js#L23) in FetchFS currently issues an initial `HEAD` request when file contents are requested for the first time, in order to obtain file length, server support for `Accept-Ranges` and enabling an optimization of getting the whole file if `length <= chunkSize*2`.
I think a better trade-off would be to immediately attempt to send the first `Range` request. It is possible to obtain the full file size from the response header `Content-Range` (i.e. `content-range: bytes 8388608-9437183/9946615`; in case ranges are supported).
This saves a full request/network roundtrip and may significantly improve performance when you frequently request streaming files. It improved filesystem "boot" performance in my Emscripten app by ~20-30%.
Contributor guide
Assessment
This issue has not been assessed yet.