Procedure Inline keyword

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Dreglor
Enthusiast
Enthusiast
Posts: 759
Joined: Sat Aug 02, 2003 11:22 pm
Location: OR, USA

Procedure Inline keyword

Post by Dreglor »

it has proably been asked before but i didn't find it while searching :\

i think a inline function would be nice.
it would improve speed in some cases and improve readability

there just some times when a procedure call would be nice but the code inside is so small its slower than having inlined.
my raytracer uses alot of this inlining to improve speed becasue of the vector math procedures like DotProducts are tiny but are hardto read and understand in the soup of code when inlined.

so maybe a special procedure keyword such as

Code: Select all

ProcedureInline.f DotProduct(ax.f,ay.f,az.f,bx.f,by.f,bz.f)
  ProcedureReturn ax * bx + ay * by + az * bz
EndProcedure
i don't think it would too difficult to implement just replace all procedures with there code
~Dreglor
Dräc
Enthusiast
Enthusiast
Posts: 150
Joined: Sat Oct 09, 2004 12:10 am
Location: Toulouse (France)
Contact:

Post by Dräc »

Wait for PB v4 and keyword "Macro" :wink:
Fred
Administrator
Administrator
Posts: 16681
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

yes, macros will be perfects for such stuffs
remi_meier
Enthusiast
Enthusiast
Posts: 468
Joined: Sat Dec 20, 2003 6:19 pm
Location: Switzerland

Post by remi_meier »

And what about local variables in macros? That's the difference between
inline-procedures and macros!
Fred: Do you plan to implement them like #define or like inline procedures?
Athlon64 3700+, 1024MB Ram, Radeon X1600
Fred
Administrator
Administrator
Posts: 16681
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

mostky like #define (with some more flexibility)
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Good stuff.

In the meantime, Dreglor, why not write the "inline" code in an include file and include whereever needed? That means only one place to make a change rather than hunting through all places where the particular sequence of code is used.
@}--`--,-- A rose by any other name ..
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post by tinman »

Are there any plans to add inline functions (either forced as Dreglor requested or automatic based on something like executable size) or are we stuck with macros?
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

What do you need an inline function for that a macro can't provide?
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post by tinman »

Mistrel wrote:What do you need an inline function for that a macro can't provide?
Local variables that do not interfere with the variables already defined everywhere you use the macro.

You can take the address of a procedure (although not sure how that would work if it was forced inline).

A macro can not have a return value (you need to use a fixed known variable name or your macro can only be a single statement).
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

Ahh, I see. I agree that it would be a nice feature then.
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Procedure Inline keyword

Post by luis »

Continuing what tinman was saying:

- macro's shortcomings (no return values AND side effects) removed

- ability to validate params at compile time

- easier to debug


I was looking to see if a request for inline procedures was already made and found this one.

So, even if we have macros, I would like to have inline procedures as well.
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Procedure Inline keyword

Post by Shield »

Inline Procedures should be up to the compiler, imho. That means that the compiler
should decide when to inline a function and when not.

In C/C++ for example, the keyword 'inline' is just a hint to the compiler that this
function might be a case for inlining, but it's entirely up to the compiler.

While I'm usually in favor of such additions I think PB should just decide on its own when it makes sense
to inline a function and when it doesn't. Programmers should just always use a procedure. :)
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Procedure Inline keyword

Post by netmaestro »

Ha, got me. I read this post over twice and was about to reply, 'how is a macro different from what you're describing?' and then I saw all the replies and realized this thread is 8 years old! :oops:
BERESHEIT
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Procedure Inline keyword

Post by luis »

... and yet a macro is still different from an inline procedure even if 8 years are passed, some differences are outlined in the posts above

or read here -> http://en.wikipedia.org/wiki/Inline_function
"Have you tried turning it off and on again ?"
A little PureBasic review
Rinzwind
Enthusiast
Enthusiast
Posts: 638
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: Procedure Inline keyword

Post by Rinzwind »

Why stil no inline procedure?

Very different from macro's... Inline procedures are perfect for short small read/check functions

Short macOS example:

Code: Select all

Procedure kOS(constant.s)
  Protected t
  t = dlsym_(#RTLD_DEFAULT, constant)
  If t
    t = PeekI(t)
  EndIf
  ProcedureReturn t
EndProcedure
How to do this with a macro?
Post Reply