caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: David Fox <dsf@hci.ucsd.edu>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] ocaml and named constants
Date: Mon, 28 May 2001 14:32:38 +0200	[thread overview]
Message-ID: <20010528143238.D19801@pauillac.inria.fr> (raw)
In-Reply-To: <luitis571w.fsf@hci.ucsd.edu>; from dsf@hci.ucsd.edu on Wed, May 23, 2001 at 10:06:19AM -0700

> I've noticed that there is no notion of a named constant in ML.  I
> sort of assumed that the compiler could determine that uses of a
> variable after a declaration like
> 
>   let mpg_joint_stereo = 1
> 
> could be compiled as a constant provided the value was never altered
> and no references to the variable were created.  Does the compiler
> actually do this?

Yes, ocamlopt does this for integer constants (and "integer-like"
constants such as characters, booleans, constant constructors)
as part of its integer constant propagation phase.

> I can't see how it could do it for a variable that
> wasn't module local.

The information "mpg_joint_stereo is equal to 1" is stored in the .cmx
file associated with the module, along with other cross-module
optimization information.

> I ask because it seems odd that camlp4 includes
> a feature for creating real defined constants that are textually
> substituted before compilation begins.

One motivation for this is to be able to put named constants in
patterns, e.g.

     match get_next_byte() with
       mpg_joint_stereo -> ...
     | mpg_78rpm -> ...
     | _ -> ...

which cannot be done in plain ML.

Another motivation could be to get "constant inlining" in bytecode
and for non-integer constants.

> Would it be a better bet to use a function like
> 
>   let is_joint_stereo head = (mpg_mode head) = 1
> 
> and hope the compiler inlines the function?

Inlining and integer constant propagation are performed
simultaneously, so this would be at best equivalent
(if both the function and the constant are inlined) 
or less efficient (if neither is inlined).

- Xavier Leroy
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


  reply	other threads:[~2001-05-28 12:36 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-05-23 17:06 David Fox
2001-05-28 12:32 ` Xavier Leroy [this message]
2001-05-29  1:07   ` John Max Skaller
2001-05-29 12:12     ` Andreas Rossberg
2001-05-29 17:16       ` John Max Skaller
     [not found]         ` <skaller@ozemail.com.au>
2001-05-30  9:46           ` Wolfgang Lux
2001-05-29 13:50     ` Luc Maranget
2001-05-30 16:50       ` Brian Rogoff
2001-05-31  9:22         ` Luc Maranget
2001-05-31 16:34           ` Brian Rogoff
2001-06-01  4:39             ` David Fox
2001-06-01  1:45           ` John Max Skaller

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=20010528143238.D19801@pauillac.inria.fr \
    --to=xavier.leroy@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=dsf@hci.ucsd.edu \
    /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).