caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Goswin von Brederlow <goswin-v-b@web.de>
To: caml-list@inria.fr
Subject: Re: [Caml-list] How much optimized is the 'a option type ?
Date: Mon, 27 Jan 2014 16:29:44 +0100	[thread overview]
Message-ID: <20140127152944.GA29326@frosties> (raw)
In-Reply-To: <030501cf1925$45380fa0$cfa82ee0$@ffconsultancy.com>

On Fri, Jan 24, 2014 at 04:56:46PM -0000, Jon Harrop wrote:
> On 24.01.2014 08:34, Andreas Rossberg wrote:
> > On Jan 24, 2014, at 09:22 , Jon Harrop <jon@ffconsultancy.com> wrote:
> >> With value types you can almost completely avoid the garbage 
> >> collector by replacing pointers with indices into an array of value 
> >> types that acts as a pool allocator. This can be useful for avoiding 
> >> GC latency and for optimizing for collections that violate the 
> >> generational hypothesis (e.g.
> >> long-lived Sets and Maps).
> >
> > As you well know, though, pervasive unboxing as for value types is 
> > incompatible with a uniform representation approach like used by 
> > OCaml. And non-uniform representation requires either static 
> > monomorphisation (not possible in OCaml, because of first-class 
> > polymorphism and separate compilation), or dynamic monomorphisation 
> > (not possible without just-in-time compilation), or runtime type 
> > passing and switching (expensive).
> 
> I think there are halfway houses as well...
> 
> Options could keep the same uniform representation when on the heap but be
> unboxed when they are stored locally (arguments, local variables and return
> values). You could either carry the pointer to the original option (if any)
> or re-allocate when it was needed.

They should certainly be unboxed when in registers. So

let t = (23, 42.0) in

would basically become

let t_0 = 23 in   (* r0 *)
let t_1 = 42.0 in (* fpu0 *)

Unless the tuple escapes the scope (either up or down) it should never
be allocated.

But you can't just put a float 42.0 on the heap or even stack when the
GC might get called. That needs to be boxed in some way to avoid it
getting misread as pointer.
 
> If run-time type passing and switching is expensive because of virtual
> dispatch then you could replace the dynamic jump with a test to see if the
> location is the same as it was for the last jump and, if so, use a static
> jump and then update just the static jump using JIT compilation. 
> That requires JIT compilation but, perhaps, so little JIT compilation that
> the technique is of interest?

Or compile code poly-monomorphic. Meaning for a function
taking/returning a tuple compile 2 (or more) flavours. One that
allocates the tuple and one that passes the tuple in registers.

Obviously higher level function would need a lot of flavours to cover
all combinations. And closure would need multiple flavours too. But
when there are too many cases, or when it is undecidable, the compiler
can simply use the base case of allocaating everything like it does
now. But all of this would be done static at compile time.
 
> The required run-time type information could just be the number of
> (uniformly-represented) words in each value and the "switch" could be a loop
> (that usually does only one iteration). For example, the "A of 'a | B of 'b
> * 'b | C" could be represented using 3 words per value instead of one: one
> for the tag A=1|B=2|C=3 and two for the two potential arguments.
> 
> On a related note, doesn't OCaml box multiple return values as well? Could
> it use sret form and elide the write barrier instead?

There are never multiple return values. You can only return tuples.

let f x y z = (x, y, z)
let (x,y,z) = f 1 2 3

It would be nice not to allocate that tuple. Does ocaml allocate it?
Or does inlining optimize the tuple away?

> Even if such approaches don't improve overall performance they should shift
> the burden off the GC and onto the generated code which would make it easier
> to obtain good performance for alternative GC implementations (like OC4MC).
> 
> Cheers,
> Jon.

MfG
	Goswin

  reply	other threads:[~2014-01-27 15:29 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-17  7:35 Damien Guichard
2014-01-17  7:55 ` David House
2014-01-17  8:16   ` Julien Blond
2014-01-17  8:40     ` David House
2014-01-17  9:10       ` Gabriel Scherer
2014-01-17  9:22         ` Simon Cruanes
2014-01-17 17:57           ` Gerd Stolpmann
2014-01-18  1:35             ` Jon Harrop
2014-01-19  6:19               ` oleg
2014-01-21  1:51                 ` Francois Berenger
2014-01-18  1:01         ` Jon Harrop
2014-01-24 10:06         ` Alain Frisch
2014-01-24 10:16           ` Alain Frisch
2014-01-24 13:32             ` Yaron Minsky
     [not found]       ` <CAK=fH+jfi=GsMYBZzmuo=V5UAWimyxiiamY2+DkLg6F0i8XHGw@mail.gmail.com>
2014-01-17  9:11         ` David House
2014-01-17 11:23           ` Jonathan Kimmitt
2014-01-17 13:46             ` Nicolas Braud-Santoni
2014-01-17 13:56               ` Frédéric Bour
2014-01-17 14:02               ` Yaron Minsky
2014-01-17 14:09                 ` Simon Cruanes
2014-01-17 22:52                   ` Yaron Minsky
2014-01-18  1:37                   ` Jon Harrop
2014-01-17 14:24                 ` Gabriel Scherer
2014-01-17 22:29                   ` Yaron Minsky
2014-01-18  1:27                 ` Jon Harrop
2014-01-18  1:18             ` Jon Harrop
2014-01-20 10:16             ` Goswin von Brederlow
2014-01-20 11:23               ` Jonathan Kimmitt
2014-01-21  2:05                 ` Francois Berenger
2014-01-22 21:22                   ` Jon Harrop
2014-01-22 21:26               ` Jon Harrop
2014-01-23  9:29                 ` Goswin von Brederlow
2014-01-23 23:20                   ` Jon Harrop
2014-01-23 23:28                     ` Yotam Barnoy
2014-01-24  8:22                       ` Jon Harrop
2014-01-24  8:34                         ` Andreas Rossberg
2014-01-24 16:56                           ` Jon Harrop
2014-01-27 15:29                             ` Goswin von Brederlow [this message]
2014-01-27 16:18                               ` Yotam Barnoy
2014-01-29  7:56                                 ` Goswin von Brederlow
2014-01-29  8:32                                 ` Jon Harrop
2014-01-29 16:11                                   ` Yotam Barnoy
2014-01-30 18:43                                     ` Yotam Barnoy
2014-02-01 15:58                                       ` Goswin von Brederlow
2014-01-30 21:31                                     ` Jon Harrop
2014-01-30 21:43                                       ` Yotam Barnoy
2014-01-31  8:26                                         ` Jon Harrop
2014-02-01 15:40                                 ` Goswin von Brederlow
2014-01-27 10:03                         ` Goswin von Brederlow
2014-01-17 14:36 ` Markus Mottl
2014-01-17 15:49   ` Yotam Barnoy
2014-01-17 16:22     ` Markus Mottl
2014-01-20 10:09   ` Goswin von Brederlow

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=20140127152944.GA29326@frosties \
    --to=goswin-v-b@web.de \
    --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).