caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gerd Stolpmann <info@gerd-stolpmann.de>
To: Arnaud Spiwack <aspiwack@lix.polytechnique.fr>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Execution time of class versus record
Date: Sun, 24 Jun 2007 20:29:09 +0200	[thread overview]
Message-ID: <1182709750.20268.22.camel@localhost.localdomain> (raw)
In-Reply-To: <467E9676.50905@lix.polytechnique.fr>

Am Sonntag, den 24.06.2007, 18:06 +0200 schrieb Arnaud Spiwack:
> Which obviously raises the question: What is the motivation for the 
> object to be encoded differently than records? 

Objects support subtyping whereas records do not. And coercion costs
nothing, thanks to the object representation.

E.g.

let print_m2 obj = print_string obj#m2

let x1 = object method m2 = "x1" method m3 = 42 end

let x2 = object method m1 = 98 method m2 = "x2" end

print_m2 x1
print_m2 x2

See that in x1 the called method m2 is the first in the list, and in x2
it is the second? In a record the order of the components in the memory
layout is the same as the order in the type. If we did that the same
with objects, we would have to do rearrangements when we coerce objects
to supertypes (which happens here when print_m2 is called).

Note also that object types are structural and not nominal as for many
object-oriented languages (i.e. you don't have to declare that x1 and x2
will be used as subtypes of <m2:string>).

> I remember reading in 
> Xavier Leroy's technical report about the first ZAM how he prepared the 
> field for extensible records (to be able to implement objects). There 
> the name of the fields were compiled into an adress, resulting no 
> runtime conversion.
> 
> I can imagine a couple of applications to the current situation (though 
> I'm not so sure they would work), but I'm really interested in the 
> original reason of this design choice.
> 
> 
> Arnaud Spiwack
> 
> 
> 
> Till Varoquaux a écrit :
> > Objects in OCaml are dictionary based, which means methods names must
> > be looked up in a table in order to get there addresses. Record fields
> > on the other hand are addressed directly. Take two files test.ml and
> > test2.ml:
> > test.ml:
> > type b=
> > {
> > field:int
> > }
> > let a={field=1};;
> > print_int a.field
> >
> > test2.ml
> > let a=object
> > method field=1
> > end;;
> > print_int a#field
> >
> > and dump there intermediate representation (-dlambda)
> >
> > test.ml
> >
> > (setglobal Test!
> >  (let (a/61 [0: 1])
> >    (seq (apply (field 27 (global Pervasives!)) (field 0 a/61))
> >      (makeblock 0 a/61))))
> >
> > test2.ml
> >
> > (setglobal Test2!
> >  (let
> >    (a/58
> >       (let
> >         (class/72 (apply (field 15 (global CamlinternalOO!)) [0: 
> > #"field"])
> >          obj_init/80
> >            (let
> >              (field/61
> >                 (apply (field 6 (global CamlinternalOO!)) class/72 
> > #"field"))
> >              (seq
> >                (apply (field 10 (global CamlinternalOO!)) class/72
> >                  (makeblock 0 field/61 0a 1))
> >                (function env/74
> >                  (apply (field 23 (global CamlinternalOO!)) 0a 
> > class/72)))))
> >         (seq (apply (field 16 (global CamlinternalOO!)) class/72)
> >           (apply obj_init/80 0a))))
> >    (seq (apply (field 27 (global Pervasives!)) (send a/58 9671866))
> >      (makeblock 0 a/58))))
> >
> > You can now understand where the performance issues comes from.
> >
> > Cheers,
> > Till
> > On 6/24/07, Jon Harrop <jon@ffconsultancy.com> wrote:
> >> On Sunday 24 June 2007 16:14:54 tmp123@menta.net wrote:
> >> > Hello,
> >> >
> >> > I've tried to implement two equivalent small programs, the one using
> >> > class, the other one using records. The resulting execution times says
> >> > that class are 7-8 times slower than record (compiled with ocamlopt 
> >> in a
> >> > Intel machine).
> >> >
> >> > Please, knows someone what I'm doing wrong?
> >>
> >> You aren't doing anything wrong.
> >>
> >> -- 
> >> Dr Jon D Harrop, Flying Frog Consultancy Ltd.
> >> The OCaml Journal
> >> http://www.ffconsultancy.com/products/ocaml_journal/?e
> >>
> >> _______________________________________________
> >> Caml-list mailing list. Subscription management:
> >> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> >> Archives: http://caml.inria.fr
> >> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> >> Bug reports: http://caml.inria.fr/bin/caml-bugs
> >>
> >
> >
> 
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
> 
-- 
------------------------------------------------------------
Gerd Stolpmann * Viktoriastr. 45 * 64293 Darmstadt * Germany 
gerd@gerd-stolpmann.de          http://www.gerd-stolpmann.de
Phone: +49-6151-153855                  Fax: +49-6151-997714
------------------------------------------------------------


  parent reply	other threads:[~2007-06-24 18:29 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-06-24 15:14 tmp123
2007-06-24 15:29 ` [Caml-list] " Jon Harrop
2007-06-24 15:48   ` Till Varoquaux
2007-06-24 16:06     ` Arnaud Spiwack
2007-06-24 18:18       ` skaller
2007-06-24 18:29       ` Gerd Stolpmann [this message]
2007-06-24 18:51         ` Arnaud Spiwack
2007-06-24 19:11           ` Chris King
2007-06-25  3:25           ` Jon Harrop
2007-06-25 11:16             ` Arnaud Spiwack
2007-06-25 12:07               ` Jon Harrop
2007-06-25 23:59                 ` Jonathan Bryant
2007-06-26  0:15                   ` Chris King
2007-06-26  6:53                     ` Loup Vaillant
2007-06-26  7:02                       ` Jon Harrop
2007-06-26 17:07                         ` Loup Vaillant
2007-06-28  1:13                 ` Christian Stork
2007-06-26 13:35 ` Sam Steingold
2007-06-26 16:29   ` [Caml-list] " Quôc Peyrot

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=1182709750.20268.22.camel@localhost.localdomain \
    --to=info@gerd-stolpmann.de \
    --cc=aspiwack@lix.polytechnique.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).