Archived

This topic is now archived and is closed to further replies.

CProgrammer

inline command

Recommended Posts

DJSnow    100
well, as ever: this depends on...

but in general, usage of inline functions is often good when calling small functions much times.





DJSnow
---
this post is manually created and therefore legally valid without a signature

Share this post


Link to post
Share on other sites
EdR    117
Inline creates speed and creates code bloat. But like he said, for small, often-called routines it''s alright.

Share this post


Link to post
Share on other sites
Codeloader    100
It is said to make the execution of a program faster... but in my case my code executes fast enough that I don''t think I have any inline functions... or maybe I do!

Share this post


Link to post
Share on other sites
marijnh    182
Actually, for functions with really tiny bodies like
inline int multiply(int x, int y){
return x*y;
}
...inline functions *decrease* executable size. Placing x*y directly in the executable is much smaller calling a function. Just be careful with calling inline function from inline functions from inline functions...
I''ve always wondered what the compiler does if you create a recursive inline function... but i guess it is always free to create a normal function even if you specify ''inline''.

Share this post


Link to post
Share on other sites
felisandria    739
Basically if you have to do it a LOT, because it reduces overhead. In practice, you''re usually going to be putting your Gets and Sets as inlines, and maybe the occassional short operation. The main argument against inlines has historically been that compilers like MSVC6 couldn''t handle stepping into inlines, but I think they fixed that in 7.

-fel

Share this post


Link to post
Share on other sites
__fold    202
There''s sometimes a compiler option that leaves this question for the compiler to decide. Sometimes if the code gets bigger you get more cache misses which slows things down. I think there''s alot of things that should be done before forcing inline.

Share this post


Link to post
Share on other sites
Codeloader    100
quote:
Original post by felisandria
Basically if you have to do it a LOT, because it reduces overhead. In practice, you''re usually going to be putting your Gets and Sets as inlines, and maybe the occassional short operation. The main argument against inlines has historically been that compilers like MSVC6 couldn''t handle stepping into inlines, but I think they fixed that in 7.

-fel


All of my code is said to be non-inline.


I load code! It''s my whole philosophy!

Share this post


Link to post
Share on other sites
Lektrix    106
quote:
Original post by marijnh
I've always wondered what the compiler does if you create a recursive inline function... but i guess it is always free to create a normal function even if you specify 'inline'.

As far as I know, the compiler will ignore your 'recommendation' to inline it, as it is not possible/allowed.

In my opinion, the inline mechanism is of great importance in the support of encapsulation/information hiding and the design of abstract data types.

quote:
It is said to make the execution of a program faster... but in my case my code executes fast enough that I don't think I have any inline functions... or maybe I do!

All of my code is said to be non-inline.


Oh, dear...

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on September 2, 2003 3:43:29 PM]

Share this post


Link to post
Share on other sites
capn_midnight    1707
yeah, ''inline'' as well as ''register'' are just suggestions. there is no guarantee that it will happen.

also, don''t try to inline a function that uses a for loop (or any loop for that matter). You''re compiler will shit a break, which if you are wondering, is not a good thing.


Do you use your powers for good or for awesome?
|My site | Association of Computing Machinery

Share this post


Link to post
Share on other sites
marijnh    182
captn_midnight, can you elaborate on the loop thing, why exactly would that cause a problem? what is this break you talk about? ... i use loops in linine functions quite a lot

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Don''t try to make evry functions inline ! i had bugs in Win32 debug mode when my library was entirely inlined !

Share this post


Link to post
Share on other sites
Cedric    158
quote:
Original post by CProgrammer
When is it generally good to use the inline command?

1. Don''t.
2. (For experts) Not yet.
3. If you really want to, profile first, and look try it for yourself. You can get some marginal gains that way.
quote:
In my opinion, the inline mechanism is of great importance in the support of encapsulation/information hiding and the design of abstract data types

How do you support that opinion? What you wrote doesn''t make sense to me, but I don''t know you...

Cédric

Share this post


Link to post
Share on other sites
haro    502
quote:
Original post by marijnh
Actually, for functions with really tiny bodies like
inline int multiply(int x, int y){
return x*y;
}
...inline functions *decrease* executable size. Placing x*y directly in the executable is much smaller calling a function.


That''s actually wrong. An inline function will almost invariably increase the size of code. Let''s say the function for multiply takes 7 instructions, and x*y takes 3 instructions (register, mul, store). That means if you put "x*y" in your code 4 times it will take 12 instructions, while if you had used it as a function call takes only 7+3 instructions = 10.

As a side note I feel its generally just about useless to manually inline functions with modern compilers. Optimize for speed and your compiler will generally inline where appropriate. Its just a hint anyways. Profile the compiler optimized code and if it appears that you''re bottle necking from code that could be inlined then see if it helps, otherwise inlining adds significant mess to code and will often provide negligable if any speed.

Share this post


Link to post
Share on other sites
Pipo DeClown    804
quote:
Original post by Lektrix
quote:
Original post by Pipo DeClown
Don''t use it if a functioncall is just used once.

That''s not really correct, I''m afraid.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]


K whatever. Bluh =P Hehehe.. I think I meant the opposite.. did I.. ?

.lick

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
quote:
Original post by Cedric
quote:
Original post by CProgrammer
When is it generally good to use the inline command?

1. Don''t.
2. (For experts) Not yet.
3. If you really want to, profile first, and look try it for yourself. You can get some marginal gains that way.
quote:
In my opinion, the inline mechanism is of great importance in the support of encapsulation/information hiding and the design of abstract data types

How do you support that opinion? What you wrote doesn''t make sense to me, but I don''t know you...

Cédric


Wrong. Using the inline keyword isn''t necessarily an optimization. It''s a way of telling the compiler "yes, this function is in the header file. Don''t complain about it being present int multiple object files."

Having small member functions like getters/setters in your header without the inline keyword might work for you on your compiler anyways, but don''t take it for granted.

Share this post


Link to post
Share on other sites
Cedric    158
quote:
Original post by Anonymous Poster
Wrong. Using the inline keyword isn't necessarily an optimization. It's a way of telling the compiler "yes, this function is in the header file. Don't complain about it being present int multiple object files."

Yeah, but apart from lazyness, the goal of putting a function in a header file (that is, away from the rest of the class' implementation) is , AFAIK, optimization. I don't know of any design argument in favor of putting the body of the function in the header that doesn't involve optimization.

In fact, I don't remember ever writing the inline keyword at all, since I've hardly ever gone that far into optimization.
quote:
Having small member functions like getters/setters in your header without the inline keyword might work for you on your compiler anyways, but don't take it for granted.

I don't have the Standard, but I would be surprised if the inline keyword is supposed to be mandatory. Can anyone confirm/infirm?

Cédric

[edited by - Cedric on September 3, 2003 10:50:02 PM]

Share this post


Link to post
Share on other sites
random_acts    144
I'm pretty sure that all compilers must recognize the keyword(s), but they do not have to implement them. The standard says that the inline keyword is only a suggestion. The compiler is free to ignore you. Like MSVC does a "cost/benefit" analysis, if it's worth it it inlines it, if not than it doesn't.

I've heard the inline keyword likened to a macro (sort of). Wherever that function is called, the body of it is placed directly into the code, instead of calling the function. Obvious differences/benefits are that it's in C++, so you have the benefit of type safety, encapsulation, yada yada. It's also done in the object code, not the source code. (I think)

Bottom line is that's what it does. You're all (or should be) analytical thinkers. It doesn't take a rocket scientist to know that sometimes this is good - sometimes bad. The good news is that you don't really have to know when it's which. As I stated most compilers determine that for you, so you can inline everything and it will only do it on what works best. Conversely, some compilers (MSVC, Intel, Borland I think) will inline functions that should be, but you didn't specify.

[edited by - random_acts on September 3, 2003 11:14:01 PM]

Share this post


Link to post
Share on other sites
Raloth    379
I believe there is a __forceinline keyword (it was given to me on this board). That could be fun to play with when using recursive functions .

Share this post


Link to post
Share on other sites
civguy    308
quote:
Original post by Cedric
I don''t have the Standard, but I would be surprised if the inline keyword is supposed to be mandatory. Can anyone confirm/infirm?

Inline is part of the standard that must be implemented in compilers. Inlining the function with ''inline''-keyword isn''t mandatory, but the behaviour described by AP is.

Share this post


Link to post
Share on other sites