why memoization is not a language feature?

后端 未结 11 619
无人及你
无人及你 2021-01-31 09:00

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 th

11条回答
  •  清歌不尽
    2021-01-31 09:05

    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.

提交回复
热议问题