It’s not impossible, but also not quite simple. This would require heavy heuristic pruning because each session generates hundreds of MB of machine code, so the cost of relinking might quickly outweigh the benefits. As a very rough point of comparison: I use a heavily-templated C++ application with ~1GB runtime image, ~600 shared libraries (don’t ask), and 10s of thousands of symbols. Startup time is on the order of one minute, with something like 70% of that time spent simply
strcmping symbol names during coalescing.
The keywords to start with if you want to look at prior related work are “incremental linking” and “profile guided optimization”.
I believe the more likely approaches to improved REPL responsiveness and compilation overhead involve moving the frontend to Julia, moving to a bytecode interpreter, and eventually tiered compilation. These are more work than doing just the above caching optimization, but provide a much higher return on investment. Trading some codegen quality for compiler speed is significantly easier than getting good/fast code generation to begin with, and is fairly well understood.