interledger / interledger-rs

An easy-to-use, high-performance Interledger implementation written in Rust
http://interledger.rs
Other
198 stars 70 forks source link

Investigate whether we can avoid copying the incoming ILP packet from the incoming HTTP request #554

Open emschwartz opened 4 years ago

emschwartz commented 4 years ago

Most of Interledger.rs supports zero-copy ILP packet forwarding and manipulation. However, we are currently copying the packet as soon as it arrives from the HTTP request handled by warp. There may be a way to avoid having it copy the bytes in most cases.

warp doesn't expose the internal way the HTTP body is stored and instead provides the FullBody struct. Looking through the implementation a bit, it seems like there may a way to try turning the FullBody into the BytesMut needed by:

https://github.com/interledger-rs/interledger-rs/blob/ab94e2736eb616f40cb17d1eee672f899759a0ac/crates/interledger-http/src/server.rs#L61-L62

I haven't tried this but it looks like something along the following lines might work:

let chunk: hyper::Chunk = body.into_chunk();
let bytes: bytes::Bytes = chunk.into_bytes();
let body: bytes::BytesMut = bytes.try_mut().unwrap_or_else(BytesMut::from);

As described here, try_mut will turn the Bytes into a BytesMut without copying if there are no other references to the underlying data.

emschwartz commented 4 years ago

This is blocked until we upgrade to a later version of warp that uses bytes >= v0.5.0