caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Brian Hurt <bhurt@spnz.org>
To: Markus Mottl <markus@oefai.at>
Cc: Jean-Marie Gaillourdert <jmg@gaillourdet.net>, <caml-list@inria.fr>
Subject: Re: [Caml-list] const equivalent for mutable types?
Date: Sat, 31 Jul 2004 09:31:01 -0500 (CDT)	[thread overview]
Message-ID: <Pine.LNX.4.44.0407310913240.6739-100000@localhost.localdomain> (raw)
In-Reply-To: <20040731105002.GB11964@fichte.ai.univie.ac.at>

On Sat, 31 Jul 2004, Markus Mottl wrote:

> On Sat, 31 Jul 2004, Jean-Marie Gaillourdert wrote:
> > There is a very simple way to do so: Just don't pass the references
> > around.
> 
> For references this is certainly usually the preferred case, but for
> mutable records this would be plain awkward.  You don't always want to
> copy all data in a structure to an otherwise equivalent constant record.
> 
> Even in the case of references you might have a SE-problem: what do
> you do if you suddenly discover that it is necessary to overwrite a
> reference in a large function which only used the plain value before?
> This might require tons of changes.  If you want to deliberately leave
> this option open, just pass the reference with an additional "read-only"
> type constraint.
> 

This way lies hell.

The problem is that if the called function *can* modify the argument, 
there is an extra, uncheckable, dependency between the caller and called 
functions.  The depedency can work in both ways- with the caller depending 
on the called function changing the argument in some known way, or with 
the caller depending upon the called function not changing the argument.  
If you violate these constraints, you can easily wind up with a bug.

Another thing to note: const in C/C++ isn't.  I can always type cast 
around the const and modify the memory anyways.  Even ignoring wild 
pointers.

Worrying about how long it takes to allocate a new structure is being
pennywise and pound foolish- and committing premature optimization.  
Especially considering you're most likely copying from L1 cache back to L1
cache, which is real cheap.  Copying a small (3-8 element) structure from
L1 cache to a different place in L1 cache is 10-20 clock cycles.  A single
cache miss that goes to main memory is 100-350 clock cycles.  Which means
I can copy that structure 5-35 times in the time it takes me to just load 
it the first time.

Take a gander at:
http://citeseer.ist.psu.edu/goncalves95cache.html

First, get your program working.  Then measure it, and see if it's fast 
enough.

-- 
"Usenet is like a herd of performing elephants with diarrhea -- massive,
difficult to redirect, awe-inspiring, entertaining, and a source of
mind-boggling amounts of excrement when you least expect it."
                                - Gene Spafford 
Brian

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  reply	other threads:[~2004-07-31 14:23 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-07-31  8:56 Christopher A. Gorski
2004-07-31  9:24 ` Jean-Marie Gaillourdert
2004-07-31 10:24   ` Jean-Marie Gaillourdert
2004-07-31 10:50   ` Markus Mottl
2004-07-31 14:31     ` Brian Hurt [this message]
2004-07-31 15:51       ` Markus Mottl
2004-07-31 17:05       ` skaller
2004-07-31 10:34 ` Markus Mottl
2004-07-31 13:44   ` Jon Harrop
2004-07-31 16:31     ` [Caml-list] Phantom types Markus Mottl
2004-08-23  9:49       ` Jon Harrop
2004-08-23 12:25         ` [Caml-list] Why does ocaml use custom buffering? Daan Leijen
2004-08-23 15:16         ` [Caml-list] Phantom types Jon Harrop
2004-08-27  9:03           ` Jacques GARRIGUE
2004-08-25 21:03         ` brogoff
2004-07-31 16:35     ` [Caml-list] const equivalent for mutable types? skaller
2004-07-31 17:23       ` [Caml-list] Functional arrays Jon Harrop
2004-07-31 18:45         ` skaller
2004-08-02  5:07           ` brogoff
2004-08-02  7:45         ` Diego Olivier Fernandez Pons
2004-08-05 16:42           ` Daniel Ortmann
2004-08-05 17:02             ` Diego Olivier Fernandez Pons
2004-08-05 17:16             ` Diego Olivier Fernandez Pons
2004-07-31 17:45   ` [Caml-list] const equivalent for mutable types? Chris Gorski
2004-07-31 14:11 ` Brian Hurt

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=Pine.LNX.4.44.0407310913240.6739-100000@localhost.localdomain \
    --to=bhurt@spnz.org \
    --cc=caml-list@inria.fr \
    --cc=jmg@gaillourdet.net \
    --cc=markus@oefai.at \
    /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).