caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "David Allsopp" <dra-news@metastack.com>
To: "OCaml List" <caml-list@inria.fr>
Subject: [Caml-list] Obj question
Date: Tue, 10 May 2011 16:15:42 +0100	[thread overview]
Message-ID: <000001cc0f25$22be86a0$683b93e0$@metastack.com> (raw)

I have need of the following type in some code:

	type 'a tbc = Confirmed of 'a | TBC of 'a

When values of this type are used, one usually wants to extract either the
tag or the value (i.e. matching on both is unusual). An alternative would be
to have:

	type 'a tbc = 'a * bool

but the "problem" is that this version requires two blocks for each value
where the first type only requires one block. It turns out that the compiler
automatically optimises this just to retrieve field 0 of the block:

	let get_tbc_value = function Confirmed x -> x | TBC x -> x

so there's no need to resort to Obj there. The same is not true for this:

	let safe_is_tbc = function TBC _ -> true | Confirmed _ -> false

which generates a branch. This is "better":

	external is_tbc : 'a tbc -> bool = "caml_obj_tag"

which takes advantage that the tag values (once converted to OCaml ints by
caml_obj_tag) are the same as the representations for [true] and [false].

So I have two questions:

a) Is that subtle use of caml_obj_tag "safe" (caml_obj_tag, btw, is
otherwise known as Obj.tag) ... bearing in mind the usual caveats that this
is buried well within a module, clearly commented and right next to the type
definition, etc. etc.
b) Is there a way of writing is_tbc without resorting to Obj which doesn't
generate a branch?

I should add that I'm posing this more from curiosity than because I'm
particularly worried about micro-managing performance... I expect my
application spends considerably more time waiting for the database query
actually to return the results than it does processing them using either
representation!


David


                 reply	other threads:[~2011-05-10 15:17 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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='000001cc0f25$22be86a0$683b93e0$@metastack.com' \
    --to=dra-news@metastack.com \
    --cc=caml-list@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).