binread icon indicating copy to clipboard operation
binread copied to clipboard

Document memory behaviour

Open Apanatshka opened this issue 4 years ago • 1 comments

I just read your blog post and bit of the docs. This crate looks really cool!

Can you document a bit about the memory behaviour of the generated code? For example, you mention nom in your blogpost, and how it has this "zero-copy" property, which is made more concrete in the README of nom as:

if a parser returns a subset of its input data, it will return a slice of that input, without copying

I suppose you cannot make the exact same guarantees, especially given the escape hatch of parse_with, but I imagine binread will typically have a similar zero-copy property, right?

Apanatshka avatar May 29 '20 09:05 Apanatshka

While in theory binread could be zero-copy for a very small subset of its features (repr(C) or repr(packed)), it would not be possible to implement currently as:

  1. BinRead is std::io::Read based, which by design prevents it from being zero-copy (albeit this also allows parsing directly from a stream for data not already sitting in memory, however I cannot speak to the performance differences caused by that, but I imagine it’s consistently preferable for large files).
  2. BinRead currently has no means of specifying the lifetime of the returned struct, without this zero-copy would be unsound.

While I absolutely see the value brought by zero-copy, I believe it’d be hard to fit into the current design of binread.

I do agree that documenting the differences from other approaches would be beneficial. Probably in the form of a comparison table.

jam1garner avatar May 29 '20 10:05 jam1garner