biniou icon indicating copy to clipboard operation
biniou copied to clipboard

Portability issue with float serialization

Open vouillon opened this issue 12 years ago • 4 comments

Several people have tried to use Yojson, which depends on Biniou, with Js_of_ocaml. However, Biniou uses an unportable hack to serialize floats. In particular, the expression "String.unsafe_get (Obj.magic 1.0) 0" is unconditionnally executed and fails when compiled to Javascript. Maybe the function Int64.of_float could be used instead to access the binary representation of floats?

vouillon avatar Aug 12 '12 12:08 vouillon

The reason for this choice was performance, but maybe the gain is not that significant. Some benchmarks are needed.

FYI, I submitted a feature request some time ago without much of a conclusion; feel free to add your insights: http://caml.inria.fr/mantis/view.php?id=5494

mjambon avatar Aug 13 '12 18:08 mjambon

I made the following lazification:

let float_endianness = lazy (
  match String.unsafe_get (Obj.magic 1.0) 0 with
      '\x3f' -> `Big
    | '\x00' -> `Little
    | _ -> assert false
)

It's the simplest solution I can think of. I hope it solves the Yojson/Js_of_ocaml problem.

mjambon avatar Aug 15 '12 01:08 mjambon

Thanks a lot!

Unfortunately, this is still not quite enough, as there is a piece of code a bit later in the same file (around line 256) that calls read_untagged_float64 and write_untagged_float64 unconditionally to test whether they work properly...

vouillon avatar Sep 03 '12 15:09 vouillon

Oops. This one is now fixed (tag v.1.0.5). Hopefully it's the last one.

mjambon avatar Sep 04 '12 20:09 mjambon