caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Damien Doligez <damien.doligez@inria.fr>
To: OCaml mailing list <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] obj magic performance and other questions
Date: Thu, 17 Mar 2011 17:36:47 +0100	[thread overview]
Message-ID: <0B1C84C5-3C4A-41B9-862B-FF913DEAA8C0@inria.fr> (raw)
In-Reply-To: <057400AC-9E39-45CC-AB7A-35C67C339F83@math.harvard.edu>


On 2011-03-15, at 17:05, Nicolas Ojeda Bar wrote:

> let rec2_b x =
> match x.children with
> | Rec2 r -> r.b
> | _ -> assert false
> 
> This is not very efficient. I can use Obj.magic to access that same field as
> follows:
> 
> let rec2_b x =
> Obj.obj (Obj.field (Obj.field (Obj.repr x.children 0) 0))
> 
> My question is: is this use of Obj safe? and is this compiled to (two) simple
> array accesses? Or is Obj.obj doing something behind the scenes?


Not safe.

Your first version of rec2_b is safe: it throws an exception when the "downcast"
fails.  With your second version, anything can happen if the argument is not a
rec2 (program crash, wrong result, silent failure, strange side effects, correct
result, whatever).

Also, as others have noted, the Obj version might actually do more tests than the
proper one (I can't be bothered to check exactly).

-- Damien



      parent reply	other threads:[~2011-03-17 16:36 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-15 16:05 Nicolas Ojeda Bar
2011-03-15 17:17 ` Dario Teixeira
2011-03-16  2:21 ` Guillaume Yziquel
     [not found] ` <325023989.96091.1300242147328.JavaMail.root@zmbs4.inria.fr>
2011-03-16  8:46   ` Fabrice Le Fessant
2011-03-17 16:36 ` Damien Doligez [this message]

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=0B1C84C5-3C4A-41B9-862B-FF913DEAA8C0@inria.fr \
    --to=damien.doligez@inria.fr \
    --cc=caml-list@yquem.inria.fr \
    /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).