digitalmars.D.learn - pure functions/methods
- Namespace (6/6) Apr 20 2012 The sense of pure functions isn't clear to me.
- Sean Cavanaugh (5/11) Apr 20 2012 Simplest explanation I can think of is:
- Ary Manzana (9/15) Apr 20 2012 As far as I know pure functions always return the same results given the...
- Timon Gehr (8/15) Apr 20 2012 1. It enables stateless reasoning about program parts.
- Namespace (2/19) Apr 20 2012 So only GDC optimized "pure" functions at all?
- bearophile (13/14) Apr 20 2012 I've seen DMD performs some optimizations with "strongly pure"
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?
Apr 20 2012
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)
Apr 20 2012
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.
Apr 20 2012
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/
Apr 20 2012
On Friday, 20 April 2012 at 09:55:28 UTC, Timon Gehr wrote:On 04/20/2012 10:06 AM, Namespace wrote:So only GDC optimized "pure" functions at all?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/
Apr 20 2012
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
Apr 20 2012