caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jeremy Yallop <yallop@gmail.com>
To: Martin Jambon <martin.jambon@ens-lyon.org>
Cc: OCaml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] How to mutate immutable record fields?
Date: Sat, 15 Oct 2011 10:13:01 +0100	[thread overview]
Message-ID: <CAAxsn=Go+=FPCnWOv3NHJ1runia0Sc82gaiXsXLc6iOoPk83vw@mail.gmail.com> (raw)
In-Reply-To: <4E993FE5.7080409@ens-lyon.org>

On 15 October 2011 09:10, Martin Jambon <martin.jambon@ens-lyon.org> 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.


  parent reply	other threads:[~2011-10-15  9:13 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-10-15  8:10 Martin Jambon
2011-10-15  9:07 ` Nicolas Pouillard
2011-10-15  9:13 ` Jeremy Yallop [this message]
2011-10-15  9:15 ` Gabriel Scherer
2011-10-18 21:45   ` Martin Jambon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAAxsn=Go+=FPCnWOv3NHJ1runia0Sc82gaiXsXLc6iOoPk83vw@mail.gmail.com' \
    --to=yallop@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=martin.jambon@ens-lyon.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).