News for hackers. Stuff that matters.
Re:Functional programming in C++ (2012) by TeMPOraL on Monday December 28, 2020 @07:46PM (#25565259)
> One benefit (with respect to the CPU) of pure functions is that they can be cached

Only if the input space is very small. If your function takes as input two booleans and a string you know is an ID from a relatively static and small set of IDs, memoization is the way to go. If your function takes in two arbitrary floats, trying to memoize anything will only make your program run slower and leak memory.

Re:Functional programming in C++ (2012) by MaulingMonkey on Monday December 28, 2020 @08:10PM (#25565433)
Permanent memoization is not your only option.

Caching the result of common inputs and discarding not-recently-used results can be useful on functions with huge - but nonuniformly distributed - input and output spaces. At the extreme, you might have procedural image or texture generation - perhaps the postprocessing of another image or texture, for example. Impurity is a great way to end up with serious cache invalidation bugs when untracked inputs are changed, and purity a great way to avoid them.

Re:Functional programming in C++ (2012) by Jtsummers on Monday December 28, 2020 @08:23PM (#25565537)
Well, caching is a thing that shouldn't be done "just because" but after some analysis that says it's worth it. And as MaulingMonkey said, a cache doesn't need to keep everything, just enough things to improve performance. And if the cache is costing you performance versus recomputation, then don't do it. I mean, just like anything else in life, if it hurts don't do it. If it costs you performance in a way that you care about, use an alternative.