Skip to content

hot code swapping #68

Open
Open

Description

@andrewrk
  • Have the compiler run continuously, watching the file system for source
    changes and automatically perform multithreaded compilation to build projects
    quickly.
  • Hot code swapping. When integrated with the previous feature, you could
    press "save" in your editor and see the change immediately in your running
    software.

Of course this only works for some kinds of changes - obviously if you make changes to your initialization code, you won't see a difference. But it would probably work at the function level, so you could swap out any individual function for a new one.

Activity

  1. added
    enhancementSolving this issue will likely involve adding new logic or components to the codebase.
    on Jan 14, 2016
  2. PavelVozenilek commented on Feb 9, 2016

    @PavelVozenilek

    This feature (automatic rebuild in background & hot code swapping) is available for C++ in Projucer IDE, built upon LLVM infrastructure.

    Old demo 1, old demo 2, few implementation details.

    Be aware that it took several years to implement and the implementor said that "it was the most complicated thing I ever did".

  3. added this to the 0.3.0 milestone on May 20, 2017
  4. modified the milestones: 0.3.0, 0.4.0 on Feb 28, 2018
  5. 0joshuaolson1 commented on May 25, 2018

    @0joshuaolson1

    @PavelVozenilek The Projucer link has died, sadly.

  6. ccll commented on Jun 14, 2018

    @ccll

    I always wonder how should hot-swap deal with data (data structure layout and long-lived in-memory state), it seems rather difficult compared to hot-swap stateless code (like a pure function).
    In some languages which support closure, it's even more complicated.
    Are there any articles on this subject?

  7. isaachier commented on Jun 14, 2018

    @isaachier
    Contributor

    @ccll that is the biggest issue. Doing it in C/C++ is currently possible using a dynamically loaded library. See this blog post on the topic.

  8. themagnet commented on Jun 26, 2018

    @themagnet

    here https://molecular-matters.com/products_livepp.html is a commercial product which realizes hot-reload for c/c++ projects

  9. jaccarmac commented on Jun 29, 2018

    @jaccarmac

    I wonder if looking at the way Common Lisp deals with these things would be useful at all. Sure, CL has a fatter runtime than Zig at the binary level, but:

    1. If there is a watcher like @andrewrk hints we could possibly have it keep track of the old structure of the code it is watching.
    2. CL contains a full-blown object system and is able to manage updating all live instances of a CLOS class. Updating structs would be comparatively simpler, size changes might be the hardest part to deal with?
  10. ccll commented on Jul 11, 2018

    @ccll

    @isaachier Thanks, that article is very helpful!
    I can see it passes a DLL-specific 'game_state' around, but what if the layout of 'game_state' got changed between reloads? I can imagine there will be lots of crashes/segfaults/misbehavior.

    Now i've integrated C live coding successfully with libtcc, which hot-swap pure code flawlessly.

    About the state transition, I can think of a solution which serialize the state out (to a layout-agnostic format), allocate new state, then serialize in. It should work, but involves a lot of manual implementation of serializing code and is not very generic/automatic.

  11. removed this from the 0.4.0 milestone on Sep 28, 2018
  12. 13 remaining items

  13. modified the milestones: 0.8.0, 0.9.0 on Jun 4, 2021
  14. modified the milestones: 0.9.0, 0.10.0 on Nov 21, 2021
  15. dischoen commented on Jan 24, 2022

    @dischoen

    I always wonder how should hot-swap deal with data (data structure layout and long-lived in-memory state), it seems rather difficult compared to hot-swap stateless code (like a pure function). In some languages which support closure, it's even more complicated. Are there any articles on this subject?

    You could have a look at how it is done in erlang. However, that's a different world..

    Another thing, which I haven't seen in the comments so far would be security.
    Would an attacker be able to patch production code at runtime?

  16. andrewrk commented on Jan 24, 2022

    @andrewrk
    MemberAuthor

    I did some live coding on this feature recently, and we got to a proof-of-concept status:

    It's in the hcs branch. It depends on further progress on the x86_64 backend to be generally useful. It's also for Linux only currently. Each OS will need its own hot code swapping strategy.

  17. modified the milestones: 0.10.0, 0.11.0 on Apr 17, 2022
  18. deleted a comment from chris2860 on Oct 16, 2022
  19. modified the milestones: 0.11.0, 0.12.0 on Apr 9, 2023
  20. qm3ster commented on Jul 29, 2023

    @qm3ster
  21. acodervic commented on Nov 3, 2023

    @acodervic

    I need HRC(Hot Reload Code) , If Zig has HRC It will be perfect language .

  22. jeffective commented on Aug 26, 2024

    @jeffective
    Contributor

    Use case: industrial control applications which require "online changes" (ability to change code between control cycles with effectivley zero downtime)

    Would want to trigger the hot swap manually and be able to tell if a hot swap is possible or not before attempting.

  23. andergisomon commented on May 19, 2025

    @andergisomon

    Edit: xoxo

    Just adding on to @kj4tmp mentioned, this will be a perfect use case for Zig over Rust, where the borrow checker and unsafe start to become more of a less elegant solution to guard against UB.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementSolving this issue will likely involve adding new logic or components to the codebase.frontendTokenization, parsing, AstGen, Sema, and Liveness.

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions