Code Monkey home page Code Monkey logo

hotreload's Introduction

hotreload

Running on zig 0.12.0-dev.415+5af5d87ad

A project to try out hotreloading in zig. Hotreloading means updating an application that is already running. We do this by using dynamic libraries which are recompiled, and the running application reloads the library.

Current Structure

There are two files; main.zig and game.zig. Both of these files have access to the Game struct which is in the game.zig file.

To implement hot reloading, we compile an executable and a dynamic library. main.zig is used to create an executable called reload.exe and game.zig is used to create a dynamic library called hotreload.dll.

The dll only exposes the Game.update method, so the surface is very minimal.

On startup, the executable loads up the directory. It then spawns a thread that watches the zig-out/lib directory. The watcher thread sets a flag when it sees any change in the output dir, and the main loop sees that flag, and reloads the dll, and updates the pointer to the updateGame method.

To load the dll, we copy it from zig-out to another location to avoid access clashes when we want to rebuild the library.

When we want to build statically, we can build with -Dbuild_mode=static_exe, and the whole project will compile statically. The changes between the two modes are minimal. The state for storing dll data in main.zig is no longer required, and we call game.update() directly rather than through the dll.

Running the hot code reload

  1. zig build run -Dbuild_mode=dynamic_exe compiles everything and starts the application. You will see a rectangle bouncing around the screen.

  2. Make changes to the game.zig file - for example, change the color of the rectangle in Game.render, or draw more shapes.

  3. In a new terminal, run zig build -Dbuild_mode=hotreload. Once the compilation is complete, the running app will automatically load in the new library, and the app will hotreload.

  4. GOTO step 2.

  5. Profit?

Current Issues

  • Is this the correct approach? I am new to this, and I don't know if there are any obvious issues that I am unaware of in this space.

  • Is hot code swapping planned to release real soon? I think Andrew had streamed about it a while ago, but I don't know if there have been any updates on that front.

  • What is the best way to handle recompiling and reloading? The current approach involves copying the dll file out of zig-out/bin. If we use the dll directly from zig-out, then the next time we try to build the library, the application is already accessing the file, and we get an AccessDenied error.

  • Adding more fields to the Game struct seems like it is not memory safe. It has worked a few times, but I think that might just be lucky.

I would like to know if there is a way to make that more reliable. The way that I think about it is if I can alloc a large chunk of memory and then use the head of that to store the game struct, so that any additional fields will all be in memory that is safe to touch.

Additionally, I think the zig spec does not preserve the ordering of elements in the struct, so there may be changes needed there, possibly using extern struct or packed struct.

Discussion on Ziggit

hotreload's People

Contributors

samhattangady avatar

Stargazers

Marcel Ochs avatar Johannes Barthelmes avatar  avatar Andrew Chou avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.