heroku / libcnb.rs

A framework for writing Cloud Native Buildpacks in Rust
BSD 3-Clause "New" or "Revised" License
34 stars 6 forks source link

Improve implementation of `MappedWrite` #779

Open edmorley opened 6 months ago

edmorley commented 6 months ago

MappedWrite currently manually appends a single element at a time to its buffer in its io::Write implementation (rather than a split and push several at once approach, or similar): https://github.com/heroku/libcnb.rs/blob/fbf391909cc0eacdad459654b5afe99e6a0fd3c1/libherokubuildpack/src/write.rs#L107-L114

This seems suboptimal from a performance point of view (given MappedWrite's primary use-case is to process the stdout of a process, which could be large in volume).

I happened to spot that the Rust stdlib actually has std::io::LineWriter which does something similar to what MappedWrite does.

Whilst we can't use LineWriter for our use-case (since it doesn't support a way to map the lines), it's possible we could take inspiration from its implementation: https://github.com/rust-lang/rust/blob/master/library/std/src/io/buffered/linewriter.rs https://github.com/rust-lang/rust/blob/master/library/std/src/io/buffered/linewritershim.rs