Hotswap code in a running program

Not so long ago I discovered a cool feature that could be useful for more advanced users (those not using the Processing IDE but something like IntelliJ Idea).

In Idea, if you Debug your program (instead of Running it), you can make changes to your code and test the changes without stopping the running program. After editing the code (no need to save), go to Run > Reload Changed Classes and after ~2 seconds (compilation) I see the effect.

The Processing IDE offers a simple version of this, the Tweak mode. That mode allows you to manipulate values on your program (sizes, colors, positions) and immediately see the effect on the running program.

With the hotswapping approach you can change not only values, but also the code itself. You can’t add new functions, but you can change existing functions: change algorithms, draw more or less things, in different order, comment things out, etc. And when you do this, your program state is maintained, so variables keep their existing values, setup() is not called again, and if you had animating objects, they don’t jump to new positions but continue smoothly where they were before the change.

I only wish it took 20 milliseconds instead of 2000 :slight_smile:

PraxisLIVE offers something similar, right @neilcsmith ?


Vaguely! :smile: I’m actually halfway through writing a blog post on PraxisLIVE’s implementation of this. PraxisCORE, PraxisLIVE’s runtime, is designed from the ground up to be a live-programming / hot-swapping framework for Java, for development and runtime purposes (eg. influenced by SmallTalk, Erlang, Extempore, etc.). It rejects Java’s built-in hotswap, as well as things like JRebel or HotswapAgent/DCEVM, as not being fit for this purpose.

So, what does that mean in practice. Well, it doesn’t use agent-based hacks and works kind of in the opposite way. We load a new class each time, and the framework handles injecting state from one iteration to the next, creating or disposing of resources as necessary. By doing things this way, we can maintain state, while still allowing for adding and removing methods and functions (I emphasise both because lambdas generally don’t work with default hotswap either, which makes this far less useful with Java 8)

The Processing nodes actually work like a sketch but draw to a PGraphics, which means we can mix multiple “sketches”. One decision in doing the wrapper though was to run setup() each code iteration and reset all styles behind the scenes - PraxisLIVE tries very hard to ensure if you remove code you need, it will show up immediately, not the next time you run things - another big failing with the other hotswap options if you change your setup()!

Oh, and while not doing hacks, we use an in-memory file system and classloader to interact with the compiler API, which means source code and bytecode never hits the disk when hotswapping - 20ms sounds about right - it certainly feels instantaneous! :smile:

Sorry, long post, but you asked … :wink:


@hamoid well, that blog post took a while to complete! Promised myself it would be up in 2018 - managed with a few hours to spare. :smile: If you’re interested in reading further …

Happy New Year! :fireworks: