Live data from Hacker News

Hot-code reloading on macOS/arm64 with Zig

jakubkonka.com

51–57 of 57 posts

Re: Hot-code reloading on macOS/arm64 with Zig

#51
post #37

Awesome! vlang has it too: https://github.com/vlang/v/blob/master/doc/docs.md#hot-code-...

Nice! V does it the same way as Nim though, right? From quickly browsing over the sources, it looks like it is based on dynamic library hot swapping https://github.com/vlang/v/tree/master/vlib/v/live

V author here. Yes, dynamic library hot swapping, but we're also planning to implement a more sophisticated way to do this.

Re: Hot-code reloading on macOS/arm64 with Zig

#52
post #37

Earlier quoted context omitted.

Nice! V does it the same way as Nim though, right? From quickly browsing over the sources, it looks like it is based on dynamic library hot swapping https://github.com/vlang/v/tree/master/vlib/v/live

V author here. Yes, dynamic library hot swapping, but we're also planning to implement a more sophisticated way to do this.

What is the more sophisticated way? Do you have an Issue / Milestone in GitHub?

Re: Hot-code reloading on macOS/arm64 with Zig

#53

Earlier quoted context omitted.

JITs don’t need to reload code, they run the same code and tier it up to an optimized implementation when it gets hot. No execution context is lost, at least semantically.

> JITs don’t need to reload code If I've inlined a method, and someone redefines that method in a language that allows that, then I'm going to need to reload that code.

I don’t know much about the internals of a JIT compiler, but wouldn’t it just (recursively) deoptimize the methods which include the inlined method and do a normal method call instead which will now go to the updated implementation?

Re: Hot-code reloading on macOS/arm64 with Zig

#54
I'm using Zig, but this all sounds like being able to hot swap in Lisp dev, especially for games. I fermenter someone using Gambit and iPhone for game dev in 2010 or so pulling this off. This seems more complicated in comparison. I'm playing with this: https://github.com/michal-z/zig-gamedev

Mach for Zig looks interesting too, but it's very new.

Re: Hot-code reloading on macOS/arm64 with Zig

#55
post #40

Earlier quoted context omitted.

I have implemented live reload (in a much simpler and naive way) for my little game engine/framework written in C a few years ago, as a "scene" is essentially a reloadable shared library there. When it worked it was a huge productivity booster and time saver indeed, but I've eventually stopped using this feature that much once I started relying more and more on passing function pointers around as callbacks - so thing…

Optional late binding - function pointers that go through a lookup step (string or slot) before finding their destination.

Yeah, that was the idea, but doing it in a way that doesn't require excessive boilerplate needs some creativity :)

Re: Hot-code reloading on macOS/arm64 with Zig

#56
post #11
post #9

Earlier quoted context omitted.

The only entitlement that is relevant here is get-task-allow. But that would allow anyone to get a control task port for your application and do with it as they may. This functionality was not designed to be used in production — except for debugging. The debugger entitlement is even more powerful, but once again since you’re modifying your own memory you don’t need it.

Wait, but what about debuggers then? Plus hot-code reloading should only ever be used for quick development cycles when prototyping your app in debug mode, so very much what a debugger is used for, right? Additionally, I actually based the implementation of this PoC on lldb's debugserver for macOS.

> Plus hot-code reloading should only ever be used for quick development cycles when prototyping your app in debug mode

What about evolutionary/genetic programming? That can definitely take advantage of this as well.

Re: Hot-code reloading on macOS/arm64 with Zig

#57
post #5

Earlier quoted context omitted.

If that matters, you probably shouldn't be using hot code reloading regardless of the language or implementation.

> If that matters, you probably shouldn't be using hot code reloading regardless of the language or implementation. Why not? Perhaps I'm missing the point, but can't this be mutex'd away? Or perhaps you're refering to the fact that the hot code reloading mechanism should provide the mutex implicitly?

Things like restoring connections can be mutex'd away in the implementation, yeah. Or whatever the actually-working version would be, maybe there needs to be a socket-owning parent process that never dies, I dunno.

But if you write

    result = 0
    while true:
      result += 2 * do_something()
and change that to this and reload:

    result = 1
    while true:
      result -= 2 * do_something()
then even "ideal, correct behavior" at a reload level can give you end result values that are not possible to get while running either version of the code end-to-end. It's not possible to "fix" that because it's doing exactly what you told it to do - either it preserves values and can produce impossible values, or it does not and it's not really hot-swapping any more. Or it does some mix of that and it has even more surprising / wrong edge cases.

There are of course uses for systems with hot-swap boundaries/transactions to prevent swapping during critical regions, or to do something like stop and replace actors rather than touching their internal state, and they exist (e.g. erlang). But that requires your code to already be adopting those semantics, and it inherently defines boundaries on what is swapped and what is not. In that case the root "inconsistent transaction state" concern is not really a hot swapping issue, it's a failure to define your boundaries and semantics correctly - a normal bug / relying on undefined behavior.

Post reply on HN