Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Yaron Minsky <yminsky@gmail.com>
To: caml-list@inria.fr
Subject: Inside the mind of the inliner
Date: Thu, 28 Jan 2010 19:31:33 -0500	[thread overview]
Message-ID: <891bd3391001281631j5bee86e1h501aae874d06c1bc@mail.gmail.com> (raw)

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

I've been doing some experiments with the OCaml inliner, and have
walked away from the process very confused.  It seems like inlining
can be prevented by very simple changes to the code of a function.
The main surprise for me is that adding a quite trivial allocation of
a list or a string literal defeats the inliner.

Does anyone have a better understanding of what's going on here?  I
feel like my intuition for this stuff is terrible.

I checked inlining using the following command line:

  ocamlopt -S -inline 10000 z.ml ; egrep 'call.*camlZ__f' z.s

And here are the different variants of z.ml I tried.

(* Simple arithmetic.  f is inlined *)
let f x = x + x
let g x = f x + f x

(* Add in allocation of a list, not inlined *)
let f x = ignore [1]; x + x
let g x = f x + f x

(* allocate a string, not inlined *)
let f x = ignore "foo"; x + x
let g x = f x + f x

(* reference to the empty list, inlined *)
let f x = ignore []; x + x
let g x = f x + f x

(* call a function that iterates over a list, inlined *)
let list = [1;2;3]
let plus x y = x + y
let f x = x * List.fold_left plus 0 list
let g x = f x + f x

(* Call a function that includes an infix operator in prefix form,
   not inlined. *)
let list = [1;2;3]
let f x = x * List.fold_left (+) 0 list
let g x = f x + f x

(* Allocate the list in the function, not inlined *)
let plus x y = x + y
let f x = x * List.fold_left plus 0 [1;2;3]
let g x = f x + f x

(* call a function to allocate your list, inlined *)
let plus x y = x + y
let create_list x = x :: x + 1 :: x + 2 :: []
let f x = x * List.fold_left plus 0 (create_list 1)
let g x = f x + f x

I've tried these experiments with ocaml 3.10.1 and 3.11.1, with similar
results.

y

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

             reply	other threads:[~2010-01-29  0:31 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-01-29  0:31 Yaron Minsky [this message]
2010-02-02 14:19 ` [Caml-list] " Xavier Leroy

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=891bd3391001281631j5bee86e1h501aae874d06c1bc@mail.gmail.com \
    --to=yminsky@gmail.com \
    --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