[noise] Zero-allocation

sopium sopium at mysterious.site
Tue Feb 21 18:02:01 PST 2017


 On Tuesday, Feb 21 Trevor Perrin <trevp at trevp.net> wrote:
 > Hi Sopium,
 > I've been slow to improve Screech, so I'm happy to see other people
 > doing better rust libraries.  At first glance your code looks pretty
 > nice.
Good to know. Thanks!

 > I sort of wanted Screech to be a zero-allocation library, but probably
 > the code is cleaner when you use vectors.

 Well using `Vec` is certainly an opinionated choice. It might
have some run time performance cost as well. So I would also like
to avoid it if possible.

I have to admit that I don't have a whole lot of experience with
zero-allocation code. That said, many of the `Vec`s used now can
be simply replaced with arrays once this issue[0] is fixed
(and associated-constants stabilizes), though that probably won't
come any time soon. The rest `Vec`s in user-facing APIs should
then probably live behind a feature flag.

(Off topic, but it might also make sense to put the crypto
wrappers behind a feature flag, becuase there seems to be
currently no clear winner in Rust crypto libraries for now.)

[0]: https://github.com/rust-lang/rust/issues/34344

Another possible alternative is to use associated types for those
variable length input/output buffers, and use an `Array` trait to
generically access them, something like:

trait Array {
    fn len() -> usize;
    fn as_slice(&self) -> &[u8];
    fn as_mut(&mut self) -> &mut [u8];

This seems the most practical for now, and I will explore to see
if it could actually work.

Yet another option is to use some `alloca`, should it become
available in Rust.

 > Anyways, you should link your library in the Noise Wiki:
 > https://github.com/noiseprotocol/noise_wiki/wiki
Will do.

 > I'd like to see other people test and review it.  If we're feeling
 > good about your library in a few weeks I'd be happy to link it from
 > the Noise homepage (and remove the link to Screech, unless I'm able to
 > spend time on it).
 > Trevor


More information about the Noise mailing list