Jump to content
Mike Torrettinni

Compiler detecting repeated lines?

Recommended Posts

Can we expect future Delphi compiler be so smart to detect repeated calls and just do the once, like:

 

if (CustomerNet(C) = 0) and CustomerIsNotActive(C) or 
   (CustomerNet(C) > 0) then
    

To avoid 2 calls, I can add variable and store CustomerNet(c) and use it in the condition, but wouldn't it be cool for compiler to detect such cases on its own and optimize so that just 1 call gets executed?

 

Are other compilers smarter than Delphi and can handle such cases better?

Share this post


Link to post
2 minutes ago, Attila Kovacs said:

who gives you the guarantee that the second call is returning the same value? <o>?

I guess it can change, yes, but it my example it doesn't. But you are right, it's not guaranteed. Still, maybe it can detect if the called method can return different result... if not, then it's the same call.

Share this post


Link to post
7 minutes ago, Mike Torrettinni said:

it can detect if the called method can return different result.

You would not wait for the compiler to finish 😉 But maybe in the future there will be such things. Why not. However, as @ConstantGardener mentioned, it would be a task for static code analysis as you would lost too much time on every compile vs. the cases you have in your code.

Edited by Attila Kovacs

Share this post


Link to post
2 hours ago, Mike Torrettinni said:

Can we expect future Delphi compiler be so smart

No

 

On a more serious note: This would require the compiler to know that the repeated call is to a pure function - GCC for example has such an optimization, clang on the other hand as far as I could find (I did not spend much time) does not.

Edited by Stefan Glienke

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×