bytecodealliance / wasm-tools

CLI and Rust libraries for low-level manipulation of WebAssembly modules
Apache License 2.0
1.28k stars 230 forks source link

Streaming writing/encoding with io::Write #778

Open danleh opened 1 year ago

danleh commented 1 year ago

Similar to this prior question on streaming reading with wasmparser, I am wondering if it is possible to do streaming encoding with wasm-encoder?

Right now, the central trait seems to be wasm_encoder::Encode, which takes the sink as a &mut Vec<u8>. Would it be possible to get rid of that trait and instead serialize to any impl io::Write? That should strictly be more general, as io::Write is already implemented in the standard library for Vec<u8> anyway.

Thanks a lot for the great selection of crates!

alexcrichton commented 1 year ago

At this time there's no support for using io::Write, it's required to use a Vec<u8>. Would it work for your use case to consider the Vec<u8> a form of BufWriter where it's periodically flushed, e.g. between sections or between functions?

danleh commented 1 year ago

Sure, that would work for me. I was more wondering what the reason for the separate Encode trait is, and whether it could be replaced by the standard io::Write.

alexcrichton commented 1 year ago

The Encode trait is necessary to indicate "write yourself into this thing with the wasm encoding" and the choice of taking Vec<u8> vs io::Write was mostly a result of me to cut down on the size of compiled and monomorphized code. Dealing with errors in io::Write can cause a lot of code bloat if used everywhere and not much in wasm is so big it can't fit in memory (was my thinking)

danleh commented 1 year ago

Thanks a lot, that makes sense and answers my question! I hadn't thought about code size before.