r/lisp • • 7d ago

Scheme Memoization for Pure Functions

https://web.xiaolong-hosting.com/blog/post/memoization-for-pure-functions
14 Upvotes

1 comment sorted by

3

u/arthurno1 5d ago

Lets imagine we wanted to have concurrency. If our function is written in CPS, we could at any point at which a continuation would be called pause the evaluation of the function, keep the continuation around somewhere, and continue it at a later time.

Have you done any measurements? I would be interested to see methodology and results.

I am afraid that memoization as you present it up to the end, is basically working on sequential execution, but when you introduce multiple threads I think the idea breaks. I am perhaps blind and don't understand really what you are doing, but as I understand you, you use memoization to remove the duplicate work. If each thread gets its own copy of the past, what happens when they are finished, or when they compute in parallel? Your old thread which was perhaps waiting for another one, or continuing to compute in parallel, will have its own copy of the computed data, and the other one will have its own. How will they re-use their knowledge so to say to save computing it in twice in the parallel?

If your multiple threads would also like to take the advantage of the previously computed results, you will have to synchronize somehow that data, either merge it together or somehow share the results, which I understand you don't want in a purse functional solution. But at least it appears to me they can't longer have their own unique copy, or do I miss something? I think it would an important problem to solve, so I am interested.

And forgive me if I just don't understand it.