Skip to content

Commit

Permalink
Fix excessive CPU and I/O load from terminfo polling thread
Browse files Browse the repository at this point in the history
  • Loading branch information
BardurArantsson committed Dec 11, 2024
1 parent 24ba0d3 commit 11d4596
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion runner/client/src/mill/runner/client/MillProcessLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import mill.main.client.EnvVars;
import mill.main.client.ServerFiles;
import mill.main.client.Util;
Expand Down Expand Up @@ -211,6 +212,9 @@ static int getTerminalDim(String s, boolean inheritError) throws Exception {
return Integer.parseInt(new String(proc.getInputStream().readAllBytes()).trim());
}

private static AtomicReference<String> memoizedTerminalDims =
new AtomicReference();

static void writeTerminalDims(boolean tputExists, Path serverDir) throws Exception {
String str;

Expand All @@ -227,7 +231,19 @@ static void writeTerminalDims(boolean tputExists, Path serverDir) throws Excepti
} catch (Exception e) {
str = "0 0";
}
Files.write(serverDir.resolve(ServerFiles.terminfo), str.getBytes());

// We memoize previously seen values to avoid causing lots
// of upstream work if the value hasn't actually changed.
// The upstream work could cause significant load, see
//
// https://github.com/com-lihaoyi/mill/discussions/4092
//
// The cause is currently unknown, but this fixes the symptoms at least.
//
String oldValue = memoizedTerminalDims.getAndSet(str);
if ((oldValue == null) || !oldValue.equals(str)) {
Files.write(serverDir.resolve(ServerFiles.terminfo), str.getBytes());
}
}

public static boolean checkTputExists() {
Expand Down

0 comments on commit 11d4596

Please sign in to comment.