[m-rev.] io.{read,write}_binary

Zoltan Somogyi zoltan.somogyi at runbox.com
Tue Apr 25 01:59:50 AEST 2023


These two predicates are implemented using a form of type
punning: they take the text_{in,out}put_stream wrapper off a stream
and put a binary_{in,out}put_stream wrapper around it.
This works because at the moment, these wrappers all wrap
the same type, but this won't be the case soon. We therefore
need a new way to implement these two predicates.

The approach I propose is that

- write_binary should call string.string to convert the term
  to be written to a string,

- it should could how many code units (utf 8 or 16 depending
  on the target) the string has

- write out the length as a binary integer, followed by the
  code units of the string, again as binary data

read_binary would then reverse the process.

This should work. It should even work for Java, which the
comments on these predicates say current code does not.

Opinions? Objections?

Note that I think the obvious size of the length prefix is 64 bits.
32 would work in virtually all cases, but it is not futureproof, and
the savings are as tiny as the chance that anyone will ever want
to invoke these preds on a >4Gb term.

Zoltan.


More information about the reviews mailing list