Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions BACKLOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,12 @@ Notes; finished-work narrative does not.
and reruns compile-time evaluation and all lowering. Warm body-edit compilation improved by about 11 to 12 %
in isolated Castle Fight and Zombie Defense copies (`docs/RUNMAP_PERFORMANCE.md`); this does not include payload
diff/delivery. Keeping generic demand, reachability, slot bindings and Lua emission between reloads remains open.
Hot-run staging now hashes the manifest while streaming it to a temporary file. On the 101 MB Castle Fight
baseline, warmed staging allocated 6–7 MB per run instead of 1.72 GB across three runs per build on Windows.
The staged bytes and SHA-256 filename matched the existing baseline.
The compiler-to-CLI handoff also streams `manifest.json` and atomically replaces it once complete.
On the same fixture, warmed allocation fell from 1.31 GB to 1.2–1.4 MB across three writes per build,
with identical output bytes.
Concrete design and staged acceptance gates: [`docs/INCREMENTAL_BACKEND.md`](docs/INCREMENTAL_BACKEND.md).
The first scalar-function ownership/lowering/emission slice now proves zero graph-copy nodes and one lowered/emitted
function after a supported literal edit, including with 1,000 additional live functions. It excludes compile-time
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonIOException;
import de.peeeq.wurstscript.translation.lua.translation.LuaHotReload;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.lang.ref.SoftReference;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.security.DigestOutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
Expand Down Expand Up @@ -48,8 +54,7 @@ private LuaHotReloadSession() {
private static final class State {
/**
* The file in the session's folder which holds the manifest of the started map, named by its content
* ({@link LuaHotReloadSession#baselineFile(String)}). A large map's manifest is tens of megabytes, and no reload changes it, so the state
* which each reload saves only names it.
* A large map's manifest is tens of megabytes, and no reload changes it, so the state only names it.
*/
String baselineFile;
int version;
Expand Down Expand Up @@ -153,7 +158,13 @@ public static void main(String[] args) throws IOException {
public static void writeManifest(Path buildDir, LuaHotReload.Manifest manifest) throws IOException {
Path file = buildDir.resolve(SESSION_FOLDER).resolve("manifest.json");
Files.createDirectories(file.getParent());
Files.writeString(file, GSON.toJson(manifest), StandardCharsets.UTF_8);
Path partial = Files.createTempFile(file.getParent(), "manifest-", ".tmp");
try {
writeManifestJson(manifest, Files.newOutputStream(partial));
Files.move(partial, file, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
} finally {
Files.deleteIfExists(partial);
}
}

private static LuaHotReload.Manifest readManifest(Path file) throws IOException {
Expand Down Expand Up @@ -212,14 +223,9 @@ public static void abortLaunch(Path buildDir, Path customMapData) throws IOExcep
* rename, once the map is launched; {@link #unstage} drops it.
*/
public static void stage(Path buildDir, LuaHotReload.Manifest manifest) throws IOException {
String json = GSON.toJson(manifest);
Path baseline = buildDir.resolve(SESSION_FOLDER).resolve(baselineFile(json));
if (!Files.exists(baseline)) {
writeAtomically(baseline, json);
}
State state = new State();
state.compiletimeReferenceOwners = new LinkedHashMap<>();
state.baselineFile = baseline.getFileName().toString();
state.baselineFile = writeBaseline(buildDir.resolve(SESSION_FOLDER), manifest);
writeAtomically(stagedFile(buildDir), GSON.toJson(state));
}

Expand All @@ -235,14 +241,39 @@ public static void unstage(Path buildDir) throws IOException {
deleteOtherBaselines(buildDir);
}

/** The name of the file which holds a baseline manifest of this JSON text: its SHA-256. */
private static String baselineFile(String json) {
/** Hash the UTF-8 bytes as Gson writes them, then publish the complete file under that digest. */
private static String writeBaseline(Path folder, LuaHotReload.Manifest manifest) throws IOException {
MessageDigest digest;
try {
byte[] hash = MessageDigest.getInstance("SHA-256").digest(json.getBytes(StandardCharsets.UTF_8));
return BASELINE_PREFIX + HexFormat.of().formatHex(hash) + ".json";
digest = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(e);
}
Files.createDirectories(folder);
Path partial = Files.createTempFile(folder, BASELINE_PREFIX, ".tmp");
try {
writeManifestJson(manifest, new DigestOutputStream(Files.newOutputStream(partial), digest));
String name = BASELINE_PREFIX + HexFormat.of().formatHex(digest.digest()) + ".json";
Path baseline = folder.resolve(name);
if (!Files.exists(baseline)) {
Files.move(partial, baseline, StandardCopyOption.ATOMIC_MOVE);
}
return name;
} finally {
Files.deleteIfExists(partial);
}
}

/** Serialize the manifest to UTF-8 and close the supplied stream before publication. */
private static void writeManifestJson(LuaHotReload.Manifest manifest, OutputStream output) throws IOException {
try (var writer = new BufferedWriter(new OutputStreamWriter(output, StandardCharsets.UTF_8.newEncoder()))) {
GSON.toJson(manifest, writer);
} catch (JsonIOException e) {
if (e.getCause() instanceof IOException io) {
throw io;
}
throw e;
}
}

/** Deletes the baseline files which neither the current nor a staged session names. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3966,7 +3966,16 @@ public void aReloadDoesNotRewriteTheBaseline() throws Exception {
java.nio.file.Path baselineFile = onlyBaselineFile(folder);
assertTrue(java.nio.file.Files.readString(baselineFile).contains(baseline.abi()));
byte[] written = java.nio.file.Files.readAllBytes(baselineFile);
String json = new com.google.gson.GsonBuilder().disableHtmlEscaping().create().toJson(baseline);
assertEquals(java.nio.file.Files.readString(baselineFile), json);
byte[] digest = java.security.MessageDigest.getInstance("SHA-256").digest(written);
assertEquals(baselineFile.getFileName().toString(), "baseline-" + java.util.HexFormat.of().formatHex(digest) + ".json");
java.nio.file.Files.setLastModifiedTime(baselineFile, java.nio.file.attribute.FileTime.fromMillis(1_600_000_000_000L));
java.nio.file.attribute.FileTime writtenAt = java.nio.file.Files.getLastModifiedTime(baselineFile);
LuaHotReloadSession.stage(buildDir, baseline);
LuaHotReloadSession.unstage(buildDir);
assertTrue(java.nio.file.Files.isSameFile(onlyBaselineFile(folder), baselineFile));
assertEquals(java.nio.file.Files.getLastModifiedTime(baselineFile), writtenAt);

LuaHotReload.Manifest firstEdit = manifest(program("x + 10", "side * side", INIT));
LuaHotReloadSession.Sent first = LuaHotReloadSession.send(buildDir, customMapData, firstEdit);
Expand Down
Loading