Mike Torrettinni 198 Posted October 28, 2021 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
ConstantGardener 31 Posted October 28, 2021 ....this is more a topic for static code analysis (for example TMS FixInsight) Share this post Link to post
Attila Kovacs 629 Posted October 28, 2021 who gives you the guarantee that the second call is returning the same value? <o>? 5 Share this post Link to post
Mike Torrettinni 198 Posted October 28, 2021 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
Attila Kovacs 629 Posted October 28, 2021 (edited) 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 October 28, 2021 by Attila Kovacs Share this post Link to post
David Heffernan 2345 Posted October 28, 2021 What if the function has side effects? 1 Share this post Link to post
Stefan Glienke 2002 Posted October 28, 2021 (edited) 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 October 28, 2021 by Stefan Glienke Share this post Link to post