caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: William Chesters <williamc@paneris.org>
To: caml-list@inria.fr
Subject: [Caml-list] RFC: get/set vs get/ref
Date: Sat, 14 Jul 2001 10:52:46 +0200 (CEST)	[thread overview]
Message-ID: <15184.2142.889640.404096@beertje.william.bogus> (raw)
In-Reply-To: <3B4D020D.4039A62F@maxtal.com.au>

John Max Skaller writes:
 > The difference is exemplified by the following techniques
 > for incrementing a character:
 > 
 > 	s.set ((s.get(pos) + 1),pos)  // get/set method
 > 	s.ref(pos).++                 // ref method
 > 
 > Clearly, ref methods are more powerful and more efficient,
 > but on the other hand they expose the underlying implementation
 > and prevent hooking changes to the mutable state.

I was a little disappointed that while ocaml compiles the "bare"
increment case right,

    a.(i) <- a.(i) + 1   =>   addl $2, -2(%eax, %ebx, 2)

it doesn't do such a good job on 

    let set s i y = s.a.(i) <- y and get s i = s.a.(i)
    in
    fun s i -> set s i ((get s i) + 1)

      =>

      movl    (%eax), %ecx
      movl    -2(%ecx, %ebx, 2), %ecx
      addl    $2, %ecx
      movl    (%eax), %eax
      movl    %ecx, -2(%eax, %ebx, 2)

apparently because of the lack of common subexpression elimination
(the CS here being `s.a').  The argument against CSE seems to be that
it doesn't do anything the programmer can't do for themselves,
probably with a net gain in readability, if they really care.  But
I have seen several examples like Max's where it would actually help
in reducing the cost of crossing an abstraction barriers.

(By the way, KAI's famous optimising C++ actually INTRODUCES common
subexpressions and leaves them for the platform C backend to
eliminate!  If you define a variable using a const expression and
never modify it, it goes through substituting the expression wherever
the variable appears.  The idea I suppose is to create opportunities
for constant folding etc.  But it is extremely frustrating with gcc,
which doesn't always do the expected CSE completely---there is
absolutely no way to work around it short of introducing a spurious
global reference to the variable.  This is an example of over-complex
and unpredictable optimisation making trouble for the programmer.

Nevertheless I do think a little more support in ocaml for cost-free
abstractions would be a win.)

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


      parent reply	other threads:[~2001-07-14  8:51 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <Pine.LNX.4.33.0107110922490.1683-100000@localhost.localdomain>
     [not found] ` <0107111558020P.12210@baxter>
     [not found]   ` <00a701c10a15$9b1db190$6701a8c0@abeast1.com>
2001-07-12  1:49     ` John Max Skaller
2001-07-14  3:10       ` Bruce Hoult
2001-07-14  8:52       ` William Chesters [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=15184.2142.889640.404096@beertje.william.bogus \
    --to=williamc@paneris.org \
    --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).