Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why memoization is not a language feature?

I was wondering... why memoization is not provided natively as a language feature by any language I know about?

Edit: to clarify, what I mean is that the language provides a keyword to specify a given function as memoizable, not that every function is automatically memoized "by default", unless specified otherwise. For example, fortran provides the keyword PURE to specify a specific function as such. I guess that the compiler can take advantage of this information to memoize the call, but I ignore what happens if you declare PURE a function with side effects.

like image 202
Stefano Borini Avatar asked Dec 19 '09 13:12

Stefano Borini


People also ask

Is memoization the same as caching?

Is memoization same as caching? Yes, kind of. Memoization is actually a specific type of caching. While caching can refer in general to any storing technique (like HTTP caching) for future use, memoizing specifically involves caching the return values of a function .

What is the point of memoization?

In computing, memoization or memoisation is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again.

Is dynamic programming a memoization?

Memoization is a common strategy for dynamic programming problems, which are problems where the solution is composed of solutions to the same problem with smaller inputs (as with the Fibonacci problem, above).

What is memoization in Java?

Memoization is a technique whereby we trade memory for execution speed. Suppose you have a function which. Is costly to execute. Always returns the same output for the same input.


1 Answers

What YOU want from memoization may not be the same as what the compiler memoization option would provide.

You may know that it is only profitable to memoize the last 10 or so distinct values computed, because you know how the function will be used.

You may know that it only makes sense to memoize the last 2 or 3 values, because you will never use values older than that. (Fibonacci's Sequence comes to mind.)

You may be generating a LOT of values on some runs, and just a few on others.

You may want to "throw away" some of the memoized values and start over. (I memoized a random number generator this way, so I could replay the sequence of random numbers that built a certain structure, while some other parameters of the structure had been changed.)

Memoization as an optimization depends on the search for the memoized value being a lot cheaper than recomputation of the value. This in turn depends on the ordering of the input requests. This has implications for the memoization database: Does it use a stack, an array of all possible input values (which may be very large), a bucket hash, or a b-tree?

The memoizing compiler has to either provide a "one size fits all" memoization, or it has to provide lots of possible alternatives, and parameters to control the alternatives. At some point, it becomes easier for everyone to require the user to provide his own memoization.

like image 58
John R. Strohm Avatar answered Sep 21 '22 08:09

John R. Strohm