r/ProgrammingLanguages • • Sep 02 '26

Discussion Auto-memoization for pure functions – how to decide when it pays off?

Im currently working on a compiler for my own programming language. I want the compiler to automatically memoize pure function calls, but only when it actually improves performance. The challenge: how does the compiler decide whether caching a specific recursive call (e.g., self(x-1) and self(x-2) in fibonacci) will save more time than the memory overhead? tracking how many times a function recieves the same input isnt an option as this requires all recieved inputs to be saved. too many saved calculations can cause finding the right result for a function call to be slower than the actual calculation. so the memoization table shouldnt get to big. naive fibonacci should be memoized but simple addition for an example should not be memoized. do you have any ideas?

13 Upvotes

57 comments sorted by

View all comments

Show parent comment

1

u/flatfinger Sep 04 '26

Suppose a function has no side effects beyond returning a value, and logging arguments and return values. I can think of at least two ways that logs from such functions might be used:

  1. Every different combination of argument values might be checked to ensure that the return value was computed correctly. In this scenario, having repeated calls with the same arguments generate repeated log entries may be tolerable but not desirable.
  2. The logs might be used to validate that callers were calling the function appropriately. In this case, the generation of repeated logs with the same arguments may be essential.

How is a compiler supposed to know whether program requirements would be better served by generating or suppressing repeated log entries if the programmer doesn't indicate that?

1

u/david-1-1 Sep 05 '26

By computing and using statistics.

1

u/flatfinger Sep 08 '26

How would a even a runtime profiler determine the uses to which the generated logs are going to be put?

1

u/david-1-1 Sep 08 '26

It could use AI to make such judgements, if such a general assessment is really needed. Most compiler optimizations don't need that much general information.

1

u/flatfinger Sep 08 '26

How is an AI going to know what a person is going to do with files after they are produced?

1

u/david-1-1 Sep 09 '26

I think we are getting far away from type checking. But one way current LLMs can do this is through its history of co-working with the programmer. We are now living in an age where even compiler optimizations can be partially controlled by the long-term context of working with an engineer to program a project. A simple example is knowing that the current stage is local development. In this stage, few optimizations, if any, are needed, while in building for live production deployment, all safe optimizations are needed.

That raises the question of whether a local optimization with global dependencies is safe. An LLM can help here if it works with the compiler to help keep track of such dependencies and their checking for correctness. This is beyond the ability of a typical current compiler, which produces binary modules to be linked together later, after most local context is lost.

1

u/flatfinger Sep 09 '26

I thought we were talking about computer languages. If a compiler has to ask a programmer "Is anyone going to want to use the logs to determine exactly when this function was called", then the source code language is no longer a complete specification of the program.

1

u/david-1-1 Sep 09 '26

True. Any interaction with AI for the purpose of determining the safety of optimizations has to respect the semantics of the program. That's obvious.