caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gabriel Scherer <gabriel.scherer@gmail.com>
To: "Christoph Höger" <christoph.hoeger@celeraone.com>
Cc: OCaml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] classes not optimized?
Date: Wed, 25 Oct 2017 11:44:15 +0200	[thread overview]
Message-ID: <CAPFanBFSRdX7UmsXMDTqN0CGD18P1h95a8MCjSjFYY1=Unp-Wg@mail.gmail.com> (raw)
In-Reply-To: <CAOazmvvewP3z7d2JMd6YT16mEmtfpE_Dpn_srJbYd2GfFkNi5w@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2592 bytes --]

Object-oriented code tends to generate large intermediate representations,
too large to be inlined and too complex to be partially evaluated at
compile-time. I wouldn't expect this sort of optimization to work.

You might have better luck with first-class modules if you want some sort
of tuple subtyping.
(Performance model: with modules, subtyping coercions are compiled as a
field-reordering copy,
so the runtime cost is on the coercion rather than the field access.)

> Also as a related question, is there a way to have the lookup semantics
of methods without the open recursion part?

We could add row-typed extensible records to the language. Given how little
the object-oriented layer is used in practice, I am not sure that this
highly work-demanding addition would be a good use of a contributor's time
-- and its utility would have to be weighted against the complexity cost of
adding yet another kind of product structure.

On Wed, Oct 25, 2017 at 11:31 AM, Christoph Höger <
christoph.hoeger@celeraone.com> wrote:

> Dear OCaml users,
>
> consider the following microbenchmark:
>
> <snip>
> class s (z : string)  (y : int)  (x : int) =
>   object method z = z method y = y method x = x
> end
>
> type t = { x : int; y : int; z : string}
>
> let foo_s _ =
>  (new s) "Example" 0 1
>
> let foo_t _ = {x=1; y=0; z="Example"}
>
> let one_s _ = (foo_s ())#x
>
> let one_t _ = (foo_t ()).x
>
> let fac =
>   let rec fac n =
>     let f =
>       let rec f n a = if n <= 1 then a else f (n - (one_s ())) (n * a)  in
> f (* change one_t to one_s or vice-versa *)
>        in
>     f n 1  in
>   fac
> let bench =
>   let rec bench n a =
>     if n <= 0
>     then a
>     else (let x = a && ((fac 20) == (20 * (fac 19)))  in bench (n - 1) x)
> in
>   bench
> let test = bench 10000000 true
> let main _ = test
> </snip>
>
> If I run it with ocamlopt 4.05.0+flambda and -O3, the version that uses
> one_s takes about 7.5s whereas the one with one_t uses 0.35s. I know that
> object method lookup is more costly than records, of course. This
> particular case baffles me, though. Why is the class not completely inlined?
>
> Also as a related question, is there a way to have the lookup semantics of
> methods without the open recursion part? That is, can I have a class that
> consists of values, not methods? It would love to have open tuples in some
> cases. For example, I'd like to write a function that takes a tuple of any
> length, because it only needs the first element.
>
> thanks,
>
> Christoph
>
>

[-- Attachment #2: Type: text/html, Size: 3186 bytes --]

  reply	other threads:[~2017-10-25  9:44 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-10-25  9:31 Christoph Höger
2017-10-25  9:44 ` Gabriel Scherer [this message]
2017-10-25 13:27   ` vadim
2017-10-25 13:48     ` Ivan Gotovchits
2017-10-25 13:49       ` Ivan Gotovchits
2017-10-25 14:35         ` vadim
     [not found]           ` <3e0f1001-b730-18b2-670d-cec4e0e89ef4@frisch.fr>
2017-10-25 15:23             ` vadim
2017-11-08 21:42 ` Mr. Herr

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='CAPFanBFSRdX7UmsXMDTqN0CGD18P1h95a8MCjSjFYY1=Unp-Wg@mail.gmail.com' \
    --to=gabriel.scherer@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=christoph.hoeger@celeraone.com \
    /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).