signalapp / libsignal

Home to the Signal Protocol as well as other cryptographic primitives which make Signal possible.
GNU Affero General Public License v3.0
3.55k stars 415 forks source link

bridge: Remove the 'Env' abstraction for avoiding buffer copies #390

Closed jrose-signal closed 2 years ago

jrose-signal commented 2 years ago

The purpose of Env was to avoid copying bytes from a slice &[u8] into a Vec and then again from a Vec into a Java byte[] or TypeScript Buffer. However, that's only relevant if the operation being bridged didn't already produce a Vec. Additionally, if it produces a slice but that slice is kept alive by one of the parameters, it can just be returned directly. The only place where we were actually saving a copy was when the function looked like this:

let intermediate = input.derive_intermediate();
let result: &[u8] = intermediate.access_result();
Ok(env.buffer(result))

And in practice, there were only two of these, one of which was used only for testing. That doesn't justify the complexity of Env. Look how much code got deleted!

This PR also renames bridge_get_bytearray to bridge_get_buffer to bring it in line with bridge_fn_buffer. The new bridge_get_buffer requires you to specify the return type so that slices can be returned.