On 15 October 2011 09:10, Martin Jambon <[email protected]> wrote:
> Purpose: deserializing a record efficiently, i.e. creating a record
> whose field values are available one after another in an unpredictable
> order.
>
> Problem: Obj.set_field does the job in most cases but not in the example
> below. How to make it work?

> (we don't want to use pure OCaml option refs to store field values
> before putting them into the record because that's too slow)
>
> Requirements:
> - performance is critical
> - code will be machine-generated
> - immutable record fields must be supported

The way I've approached this in the past is to to construct the record
at a mutable version of the type, then convert the value to the
immutable type at the last moment.  That is, for your type

> type t = {
>  foo : int option;
>  bar : int;
> }

I'd have the serialisation code generate a second type, not exposed to
user code:

  module Mutable = struct
    type mutable_t = {
      mutable foo : int option;
      mutable bar : int;
    }
  end

The generated deserialisation code would deal only with values of type
Mutable.t, concluding with a final coercion (Obj.magic) to the exposed
type t.


-- 
Caml-list mailing list.  Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

Reply via email to