April 20, 2012 pure functions/methods | |
|---|---|
The sense of pure functions isn't clear to me. What is the advantage of pure functions / methods? I inform the compiler with "const" that this method does not change the current object, and therefore he can optimize (at least in C++) this method. How and what optimized the compiler if i have "pure" or "const pure" functions / methods? | |
April 20, 2012 Re: pure functions/methods | |
|---|---|
Posted in reply to Namespace | On 4/20/2012 3:06 AM, Namespace wrote:
> The sense of pure functions isn't clear to me.
> What is the advantage of pure functions / methods?
> I inform the compiler with "const" that this method does not change the
> current object, and therefore he can optimize (at least in C++) this
> method. How and what optimized the compiler if i have "pure" or "const
> pure" functions / methods?
Simplest explanation I can think of is:
a const function of a class can't modify its own classes data
a pure function can't modify any data, or call other functions that are
not also pure (though there are exceptions)
|
April 20, 2012 Re: pure functions/methods | |
|---|---|
Posted in reply to Namespace | On 4/20/12 4:06 PM, Namespace wrote: > The sense of pure functions isn't clear to me. > What is the advantage of pure functions / methods? > I inform the compiler with "const" that this method does not change the > current object, and therefore he can optimize (at least in C++) this > method. How and what optimized the compiler if i have "pure" or "const > pure" functions / methods? As far as I know pure functions always return the same results given the same arguments. They also don't cause any side effect. http://en.wikipedia.org/wiki/Pure_function Many invocations of a pure function can be executed in parallel because they don't have side effects. There's also a chance of caching their result since it only depends on the value of their arguments (though I doubt what rule the compiler can use to decide to do it). I don't think any of the following benefits are implemented in DMD. |
April 20, 2012 Re: pure functions/methods | |
|---|---|
Posted in reply to Namespace | On 04/20/2012 10:06 AM, Namespace wrote: > The sense of pure functions isn't clear to me. > What is the advantage of pure functions / methods? 1. It enables stateless reasoning about program parts. 2. It enables certain compiler optimizations. > I inform the compiler with "const" that this method does not change the > current object, and therefore he can optimize (at least in C++) this > method. const on a method does not give any guarantees in C++. A C++ compiler cannot perform optimizations based on a const method. > How and what optimized the compiler if i have "pure" or "const > pure" functions / methods? DMD does not do a lot there currently. This article seems to discuss what GCC does with pure functions: http://lwn.net/Articles/285332/ |
April 20, 2012 Re: pure functions/methods | |
|---|---|
Posted in reply to Timon Gehr | On Friday, 20 April 2012 at 09:55:28 UTC, Timon Gehr wrote:
> On 04/20/2012 10:06 AM, Namespace wrote:
>> The sense of pure functions isn't clear to me.
>> What is the advantage of pure functions / methods?
>
> 1. It enables stateless reasoning about program parts.
> 2. It enables certain compiler optimizations.
>
>> I inform the compiler with "const" that this method does not
>> change the
>> current object, and therefore he can optimize (at least in
>> C++) this
>> method.
>
> const on a method does not give any guarantees in C++. A C++
> compiler cannot perform optimizations based on a const method.
>
>> How and what optimized the compiler if i have "pure" or "const
>> pure" functions / methods?
>
> DMD does not do a lot there currently.
>
> This article seems to discuss what GCC does with pure functions:
> http://lwn.net/Articles/285332/
So only GDC optimized "pure" functions at all?
|
April 20, 2012 Re: pure functions/methods | |
|---|---|
Posted in reply to Namespace | Namespace:
> So only GDC optimized "pure" functions at all?
I've seen DMD performs some optimizations with "strongly pure"
functions that return integral values.
If you have code like:
int sqr(in int x) pure nothrow { return x * x; }
int y = ...
auto r = sqr(y) + sqr(y);
I think DMD replaces that with this, even when inlining is
disabled:
int y = ...
auto r = sqr(y) * 2;
Bye,
bearophile
|

Reply