On Mon, 19 May 2014 05:16:13 +0000 via Digitalmars-d <[email protected]> wrote:
> On Monday, 19 May 2014 at 01:19:29 UTC, Jonathan M Davis via > Digitalmars-d wrote: > > It's already so rare that memoization of a function call can > > occur, that I'm > > pretty much convinced that memoization is useless as an > > optimization - at > > least as long as the compiler is doing it. After all, how often > > does a > > function get called with same the arguments within a single > > function let alone > > a single expression (and as I understand it, memoization only > > ever occurs at > > this point within either a single expression or statement - I > > don't remember > > which - but regardless, it's not even within a whole function)? > > Memoization is valid throughout the program. Opportunities occur > frequently with generic programming and inlining. I seriously question that assertion. How often do you really call the same function with the same arguments? And given that for the memoization to be done by the compiler without saving the result somewhere (which could actually _harm_ efficiency in many cases and certainly isn't something that the compiler does right now), the most that it could possibly memoize would be within a single function, that makes it all the less likely that it could memoize any calls. And given that the most it currently memoizes would be within a single expression (or possibly a single statement - I can't remember which), that makes it so that about the only time that it can memoize function calls is when you do something like foo(2) * foo(2), and how often does _that_ happen? Sure, it happens from time to time, but in my experience, it's very rare to make the same function call with the same arguments within a single expression or statement. > > Regardless, we should error on the side of not memoizing in > > order to avoid > > Then you don't need strict pure functions. As far as I'm concerned the two big gains from pure are 1. it makes it easier to reason about code, because it guarantees that the function didn't access any global or static variables. 2. it allows us to implicitly convert to different levels of mutability for the return type of pure functions where the compiler can guarantee that the return value was allocated within the function. Any other benefits we get from pure are great, but they're incidental in comparison. And in particular, in my experience, memoization opportunities are so rare that there's really no point in worrying about them. They're just a nice bonus when they do happen. - Jonathan M Davis
