comp.lang.ada
 help / color / mirror / Atom feed
* When shoul Pragma Inline be used?
@ 2003-02-23 14:33 Preben Randhol
  2003-02-23 17:26 ` Bobby D. Bryant
  2003-02-23 18:07 ` Jeffrey Carter
  0 siblings, 2 replies; 6+ messages in thread
From: Preben Randhol @ 2003-02-23 14:33 UTC (permalink / raw)


Hi

I just wonder when one should use Pragma Inline and when one should not
use it. If somebody could give some tips/links it would be very helpful.

Example:

   package body Settings is

      function Filename
         return String
      is
      begin
         return To_String (Current_File_Settings.File_Name);
      end Filename;

      procedure Set_Filename
         (Name : String)
      is
      begin
         Current_File_Settings.File_Name := To_Unbounded_String (Name);
      end Set_Filename;

   end Settings;

Here is makes sense to add Pragma Inline (Filename); ? But what about
the Set_Filename (Name : String)? Should this also be Inline?

Thanks in advance.
-- 
Preben Randhol ---------------- http://www.pvv.org/~randhol/ --
"Violence is the last refuge of the incompetent", Isaac Asimov



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: When shoul Pragma Inline be used?
  2003-02-23 14:33 When shoul Pragma Inline be used? Preben Randhol
@ 2003-02-23 17:26 ` Bobby D. Bryant
  2003-02-23 21:05   ` tmoran
  2003-02-23 18:07 ` Jeffrey Carter
  1 sibling, 1 reply; 6+ messages in thread
From: Bobby D. Bryant @ 2003-02-23 17:26 UTC (permalink / raw)


On Sun, 23 Feb 2003 14:33:08 +0000, Preben Randhol wrote:

> I just wonder when one should use Pragma Inline and when one should not
> use it. If somebody could give some tips/links it would be very helpful.

IMO the _right_ way to decide is to profile your program and see which way
works out the best.

In practice I don't do that very often, and would probably inline any
single line procedure or function that gets called a lot.

-- 
Bobby Bryant
Austin, Texas




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: When shoul Pragma Inline be used?
  2003-02-23 14:33 When shoul Pragma Inline be used? Preben Randhol
  2003-02-23 17:26 ` Bobby D. Bryant
@ 2003-02-23 18:07 ` Jeffrey Carter
  2003-02-23 22:00   ` Robert A Duff
  2003-02-24  9:29   ` Preben Randhol
  1 sibling, 2 replies; 6+ messages in thread
From: Jeffrey Carter @ 2003-02-23 18:07 UTC (permalink / raw)


Preben Randhol wrote:
> 
> I just wonder when one should use Pragma Inline and when one should not
> use it. If somebody could give some tips/links it would be very helpful.
> 
> Example:
> 
>    package body Settings is
> 
>       function Filename
>          return String
>       is
>       begin
>          return To_String (Current_File_Settings.File_Name);
>       end Filename;
> 
>       procedure Set_Filename
>          (Name : String)
>       is
>       begin
>          Current_File_Settings.File_Name := To_Unbounded_String (Name);
>       end Set_Filename;
> 
>    end Settings;
> 
> Here is makes sense to add Pragma Inline (Filename); ? But what about
> the Set_Filename (Name : String)? Should this also be Inline?

Inline allows you to indicate that you are willing to accept potentially 
larger executable size and increased module coupling in exchange for the 
speed increase of eliminating subprogram calls.

If you inline these subprograms, it's because you are using them, or 
think they may someday be used, in situations where the overhead of the 
subprogram call is unacceptable.

Since you're using Unbounded_String, it's unlikely that these 
subprograms would be acceptable in such situations with or without inlining.

A general rule I learned is: Inline small subprograms if your program 
has tight time constraints. "Small" is not precisely defined; you have 
to decide how many statements is too many. Some compilers will 
automatically inline calls to small subprograms. Such compilers must 
have a precise definition of "small", probably based on the size of the 
object code.

At 1 statement each, I would think these subprograms meet any reasonable 
definition of "small". Whether your program has tight timing constraints 
you don't say. It won't hurt to inline these subprograms, but I doubt it 
will help noticeably, either.

-- 
Jeff Carter
"Spam! Spam! Spam! Spam! Spam! Spam! Spam! Spam!"
Monty Python's Flying Circus




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: When shoul Pragma Inline be used?
  2003-02-23 17:26 ` Bobby D. Bryant
@ 2003-02-23 21:05   ` tmoran
  0 siblings, 0 replies; 6+ messages in thread
From: tmoran @ 2003-02-23 21:05 UTC (permalink / raw)


"Premature optimization is the root of all evil" Donald Knuth

> would probably inline any
> single line procedure or function that gets called a lot.
  Called a lot dynamically.  If it gets called from lots of places, you
will be adding bloat that may have consequences for program load time,
cache misses, etc.  Note also that a "single line" can generate a lot
more code than multiple lines, depending on what the lines do.

> IMO the _right_ way to decide is to profile your program and see which way
> works out the best.
  Agreed.  After the program works correctly, but isn't fast enough.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: When shoul Pragma Inline be used?
  2003-02-23 18:07 ` Jeffrey Carter
@ 2003-02-23 22:00   ` Robert A Duff
  2003-02-24  9:29   ` Preben Randhol
  1 sibling, 0 replies; 6+ messages in thread
From: Robert A Duff @ 2003-02-23 22:00 UTC (permalink / raw)


Jeffrey Carter <jrcarter@acm.org> writes:

> A general rule I learned is: Inline small subprograms if your program
> has tight time constraints. "Small" is not precisely defined; you have
> to decide how many statements is too many. Some compilers will
> automatically inline calls to small subprograms. Such compilers must
> have a precise definition of "small", probably based on the size of the
> object code.

I think such compilers usually measure the size of some low-level
intermediate code, rather than the object code itself.  It amounts to
more-or-less the same thing.

But note that in some cases the size of the object code can change
dramatically due to inlining.  For example:

    function F(X: Some_Enum_Type) return Integer is
    begin
        case X is
            when Red => return 17;
            when Green => ...
            when ...
            ... -- 100 more lines of code
        end case;
    end F;

While F is probably too big for inlining just looking at the amount of
code in the body of F, a call like "F(Red)", if inlined, will boil down
to just the literal number "17" (assuming the optimizer is smart
enough).

The advice given elsewhere in this thread, to measure the speed-effects
of pragma Inline, is good.  If your compiler doesn't inline
automatically, guess where to put pragmas Inline (based on size of code
and other issues), and measure the effect.

- Bob



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: When shoul Pragma Inline be used?
  2003-02-23 18:07 ` Jeffrey Carter
  2003-02-23 22:00   ` Robert A Duff
@ 2003-02-24  9:29   ` Preben Randhol
  1 sibling, 0 replies; 6+ messages in thread
From: Preben Randhol @ 2003-02-24  9:29 UTC (permalink / raw)


Jeffrey Carter wrote:
> Inline allows you to indicate that you are willing to accept potentially 
> larger executable size and increased module coupling in exchange for the 
> speed increase of eliminating subprogram calls.
> 
> If you inline these subprograms, it's because you are using them, or 
> think they may someday be used, in situations where the overhead of the 
> subprogram call is unacceptable.
> 

I see. Thanks for the info. I had misunderstood the inlining a bit. I'll
try profiling, but I don't think I need Inline at all for this program
:-)

-- 
Preben Randhol ---------------- http://www.pvv.org/~randhol/ --
"Violence is the last refuge of the incompetent", Isaac Asimov



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2003-02-24  9:29 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-02-23 14:33 When shoul Pragma Inline be used? Preben Randhol
2003-02-23 17:26 ` Bobby D. Bryant
2003-02-23 21:05   ` tmoran
2003-02-23 18:07 ` Jeffrey Carter
2003-02-23 22:00   ` Robert A Duff
2003-02-24  9:29   ` Preben Randhol

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox