Avoid copy of output using Seek
commit49d7b63aff2284befec698323c34c49ffc8eccf9
authorJake Bailey <jakebailey@fb.com>
Tue, 13 Sep 2022 17:48:11 +0000 (13 10:48 -0700)
committerFacebook GitHub Bot <facebook-github-bot@users.noreply.github.com>
Tue, 13 Sep 2022 17:48:11 +0000 (13 10:48 -0700)
tree6efb4439242b2cc9b2a25c7ee09f47f287c4a94b
parent1733854432361742e5665fdc9268ed6fc3f6af4e
Avoid copy of output using Seek

Summary: Instead of outputting to an in-memory buffer, then copying the header and output buffer into some value implementing `Write`, accept a value implementing `Read + Write + Seek`. Output into the writer directly by writing a dummy header of 20 zero bytes at the start. If, after writing the output, we find we only need to write a small (20 byte) header, we can just seek back to the start and overwrite it. If we guess incorrectly and need to write a big (32 byte) header, read the output back into a buffer, write it back out at offset 32, then write the large header at offset 0. [extern.c does something similar here](https://github.com/ocaml/ocaml/blob/4.14/runtime/extern.c#L955).

Reviewed By: shayne-fletcher

Differential Revision: D39451054

fbshipit-source-id: 00564d69cb6e8bee42229deaf29f03c460306020
hphp/hack/src/ocamlrep_marshal/ocamlrep_marshal_ffi_bindings.rs
hphp/hack/src/ocamlrep_marshal/ser.rs