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
3 changes: 3 additions & 0 deletions BACKLOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,9 @@ Notes; finished-work narrative does not.
Baseline cache misses and standalone CLI manifest reads now parse from buffered UTF-8. On the 101 MB
Castle Fight replay baseline, the allocating thread used 324 MB per warmed read, down from 931 MB
across three reads per build on Windows. The decoded manifests were equal in the same JVM.
Preload delivery now encodes chunks directly to the temporary file before the atomic rename.
A recorded 856 KB Castle Fight payload stayed byte-identical and allocated 2 MB per write
instead of 10.6–11 MB in the in-memory encoding and write path on Windows.

- **Audit the remaining Lua emission for waste.** The Lua backend began as "make Lua mode usable", and
recent fixes (`git log --grep "Lua"`) keep finding helper calls, allocations and dead bindings that
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public static void main(String[] args) throws IOException {
LuaHotReload.Manifest current = readManifest(Path.of(args[1]));
try {
LuaHotReload.Payload payload = LuaHotReload.payload(baseline, current, Set.of(), 1);
Files.writeString(Path.of(args[2]), preloadFile(payload.source()), StandardCharsets.UTF_8);
writePreloadFile(Path.of(args[2]), payload.source());
System.out.println("payload " + payload.version() + ": " + String.join(", ", payload.functions()));
} catch (LuaHotReload.Rejected e) {
System.err.println(e.getMessage());
Expand Down Expand Up @@ -506,10 +506,19 @@ private static Path deliver(Path customMapData, LuaHotReload.Payload payload, St
Path file = customMapData.resolve(LuaHotReload.preloadFile(payload.version()).replace('\\', '/'));
String source = receiptToken.isEmpty() ? payload.source()
: "-- " + LuaHotReload.RECEIPT_MARKER + receiptToken + "\n" + payload.source();
writeAtomically(file, preloadFile(source));
Files.createDirectories(file.getParent());
Path partial = file.resolveSibling(file.getFileName() + ".tmp");
writePreloadFile(partial, source);
Files.move(partial, file, java.nio.file.StandardCopyOption.ATOMIC_MOVE, java.nio.file.StandardCopyOption.REPLACE_EXISTING);
return file;
}

private static void writePreloadFile(Path file, String payload) throws IOException {
try (var writer = Files.newBufferedWriter(file, StandardCharsets.UTF_8)) {
appendPreloadFile(writer, payload);
}
}

/** Writes a complete temporary file and renames it, so no reader and no interrupted write leaves a partial file. */
private static void writeAtomically(Path file, String text) throws IOException {
Files.createDirectories(file.getParent());
Expand Down Expand Up @@ -623,6 +632,15 @@ private static void deleteFolder(Path folder) throws IOException {
*/
public static String preloadFile(String payload) {
StringBuilder sb = new StringBuilder();
try {
appendPreloadFile(sb, payload);
} catch (IOException e) {
throw new AssertionError(e);
}
return sb.toString();
}

private static void appendPreloadFile(Appendable sb, String payload) throws IOException {
sb.append("function PreloadFiles takes nothing returns nothing\n");
sb.append("//!beginusercode\n");
// The marker tells the map's poll that the tooltip holds a payload rather than the ability's own text.
Expand All @@ -634,30 +652,29 @@ public static String preloadFile(String payload) {
end--;
}
sb.append("p[#p + 1] = \"");
appendEscaped(sb, payload.substring(i, end));
appendEscaped(sb, payload, i, end);
sb.append("\"\n");
i = end;
}
sb.append("BlzSetAbilityTooltip(FourCC(\"").append(LuaHotReload.CHANNEL_ABILITY).append("\"), table.concat(p), 0)\n");
sb.append("//!endusercode\n");
sb.append("endfunction\n");
return sb.toString();
}

/**
* A Lua quoted-string body on one line. A {@code //!} is written {@code //\033}, so the payload cannot hold a
* marker which would end the Lua section early.
*/
private static void appendEscaped(StringBuilder sb, String s) {
for (int i = 0; i < s.length(); i++) {
private static void appendEscaped(Appendable sb, String s, int start, int end) throws IOException {
for (int i = start; i < end; i++) {
char c = s.charAt(i);
switch (c) {
case '\\' -> sb.append("\\\\");
case '"' -> sb.append("\\\"");
case '\n' -> sb.append("\\n");
case '\r' -> sb.append("\\r");
default -> {
if (c < 0x20 || c == 0x7f || (c == '!' && i >= 2 && s.startsWith("//", i - 2))) {
if (c < 0x20 || c == 0x7f || (c == '!' && i >= start + 2 && s.startsWith("//", i - 2))) {
sb.append(String.format(Locale.ROOT, "\\%03d", (int) c));
} else {
sb.append(c);
Expand Down
Loading