public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Timothy J. Wood" <tjw@omnigroup.com>
To: Daniel Berlin <dan@cgsoftware.com>
Cc: gcc@gcc.gnu.org
Subject: Re: Inlining heuristics for C++
Date: Mon, 09 Jul 2001 20:05:00 -0000	[thread overview]
Message-ID: <200107100305.UAA21297@omnigroup.com> (raw)
In-Reply-To: <87r8vpo8rw.fsf@cgsoftware.com>

On Monday, July 9, 2001, at 06:46  PM, Daniel Berlin wrote:
[...]
> We proceed to recursively inline 128 functions into this simple main 
> function
> operator call. (Bet you didn't think there were 128 functions you could
> inline here)

   IMO, the bug here is that the library you are using declares too many 
inlines.  I (personally) believe that 'inline' should be a command, not 
a request, and that a warning should be issued if inlining fails for 
some reason.  If you have the compiler decide to not inline stuff that 
is marked inline, then someone is going to come up with a case where 
they really need the inline where you didn't let them have it.  Then 
there will ensue a bunch of flags for controlling the inline limits and 
you're back to the programmer deciding when things should be inlined, 
but through a clumsier interface.  Or worse yet, they'll start using 
macros to get around the limits.



[...]
> This is effectively expressing the rule: "Small functions should be
> inlined into larger ones.  Larger functions should not be inlined into
> small ones".


   This rule is not true in general.  Just one example off the top of my 
head would be a case where you define a general inline function for 
performing some algorithm where some of the arguments are constants and 
you then define other 'real' functions that effectively pass 
configuration parameters (expecting the optimizer to treat them as 
constants and toss out different dead code in each case).


   A silly little example might be:

static inline void generalCase(enum type t, enum operation op, ...)
{
     if (t == type1 || t == type3) {
	if (op == op1)
		block1;
	else
		block2;
     } else if (t == type2 && op == op1) {
	block3;
     }
	....
}


void t1op1(...)
{
	generalCase(type1, op1, ...);
}

void t2op2(...)
{
	generalCase(type2, op2, ...);
}


   With this approach, you can often avoid needless duplication of code 
in the general case and can 'instantiate' the general case in each 
specific context and should be able to expect the compiler to blow away 
cases that can't be executed based on the inputs.

   Not only can make your code simpler, but you might have a couple 
common case that will perform better by having removed many of the 
branches from the general case.

   But, it is definitely the case, that the inlined function can be 
bigger than the function it is getting inlined into (which could just be 
a call to the inline!)


-tim

  parent reply	other threads:[~2001-07-09 20:05 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-07-09 18:47 Daniel Berlin
2001-07-09 19:22 ` Fergus Henderson
2001-07-09 20:18   ` Daniel Berlin
2001-07-09 23:30   ` Mark Mitchell
2001-07-09 19:56 ` Carlo Wood
2001-07-09 20:31   ` Daniel Berlin
2001-07-09 21:05     ` Carlo Wood
2001-07-09 21:26       ` Daniel Berlin
2001-07-09 21:45         ` Carlo Wood
2001-07-09 20:05 ` Timothy J. Wood [this message]
2001-07-09 20:34   ` Daniel Berlin
2001-07-09 23:26 ` Mark Mitchell
2001-07-09 23:37   ` Daniel Jacobowitz
2001-07-10  0:00   ` Fergus Henderson
2001-07-10  0:13     ` Daniel Berlin
2001-07-10  0:09   ` Daniel Berlin
2001-07-10  0:20   ` Daniel Berlin
2001-07-10  0:30   ` Daniel Berlin
2001-07-10  2:47   ` Nathan Sidwell
2001-07-09 20:13 dewar
2001-07-09 20:23 ` Joe Buck
2001-07-09 21:49 dewar
2001-07-09 22:17 ` Daniel Berlin
     [not found] <87r8vpo8rw.fsf@cgsoftware.com.suse.lists.egcs>
     [not found] ` <20010710122244.A14584@hg.cs.mu.oz.au.suse.lists.egcs>
2001-07-10  2:42   ` Andi Kleen
2001-07-10 14:16     ` Hartmut Schirmer
2001-07-11 14:27       ` Fergus Henderson
2001-07-10 14:53 mike stump
2001-07-10 15:04 ` Daniel Berlin

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=200107100305.UAA21297@omnigroup.com \
    --to=tjw@omnigroup.com \
    --cc=dan@cgsoftware.com \
    --cc=gcc@gcc.gnu.org \
    /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 read-only IMAP folder(s) and NNTP newsgroup(s).