From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: yminsky@gmail.com
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Inside the mind of the inliner
Date: Tue, 02 Feb 2010 15:19:37 +0100 [thread overview]
Message-ID: <4B683479.40908@inria.fr> (raw)
In-Reply-To: <891bd3391001281631j5bee86e1h501aae874d06c1bc@mail.gmail.com>
> 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.
The algorithm is very simple: a function is inlinable if
1- its code size (approximate) is below a certain threshold
(governed by the -inline option)
2- and its body doesn't contain a function definition
(fun x -> ..., let rec f x = ..., etc) nor a structured constant
(string literal, [1;2;3], etc).
The reason for 2- is that the inliner is too stupid to inline a
function without duplicating the function definitions/structured
constants contained within. Such a duplication can be very wasteful
in code and static data size. (Cue the replies "but not if small
enough!" in 3...2...1...now.)
For your specific examples:
> (* Add in allocation of a list, not inlined *)
> let f x = ignore [1]; x + x
> let g x = f x + f x
"[1]" is not a run-time allocation: its a structured constant, built
at compile-time. Hence you run into case 2 above.
> (* allocate a string, not inlined *)
> let f x = ignore "foo"; x + x
> let g x = f x + f x
Likewise (no allocation, but case 2).
> (* 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
Because (+) is really fun x y -> x + y, therefore case 2 again.
- Xavier Leroy
prev parent reply other threads:[~2010-02-02 14:19 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-01-29 0:31 Yaron Minsky
2010-02-02 14:19 ` Xavier Leroy [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=4B683479.40908@inria.fr \
--to=xavier.leroy@inria.fr \
--cc=caml-list@inria.fr \
--cc=yminsky@gmail.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