From c2e469dac3ed9002c7968c704700fd0d3c423208 Mon Sep 17 00:00:00 2001 From: Peter Palaga Date: Tue, 17 Nov 2020 23:39:37 +0100 Subject: [PATCH] Avoid environment lookups and value conversions on hot paths --- .../org/mvndaemon/mvnd/client/DaemonClientConnection.java | 3 ++- daemon/src/main/java/org/mvndaemon/mvnd/daemon/Server.java | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/client/src/main/java/org/mvndaemon/mvnd/client/DaemonClientConnection.java b/client/src/main/java/org/mvndaemon/mvnd/client/DaemonClientConnection.java index 30ceb9609..f9c89b3a4 100644 --- a/client/src/main/java/org/mvndaemon/mvnd/client/DaemonClientConnection.java +++ b/client/src/main/java/org/mvndaemon/mvnd/client/DaemonClientConnection.java @@ -53,6 +53,7 @@ public class DaemonClientConnection implements Closeable { private final Thread receiver; private final AtomicBoolean running = new AtomicBoolean(true); private final AtomicReference exception = new AtomicReference<>(); + private final long maxKeepAliveMs; private final DaemonParameters parameters; public DaemonClientConnection(DaemonConnection connection, DaemonInfo daemon, @@ -64,6 +65,7 @@ public DaemonClientConnection(DaemonConnection connection, DaemonInfo daemon, this.receiver = new Thread(this::doReceive); this.receiver.start(); this.parameters = parameters; + this.maxKeepAliveMs = parameters.keepAlive().toMillis() * parameters.maxLostKeepAlive(); } public void dispatch(Message message) throws DaemonException.ConnectException { @@ -94,7 +96,6 @@ public void dispatch(Message message) throws DaemonException.ConnectException { } public List receive() throws ConnectException, StaleAddressException { - long maxKeepAliveMs = parameters.keepAlive().toMillis() * parameters.maxLostKeepAlive(); while (true) { try { final Message m = queue.poll(maxKeepAliveMs, TimeUnit.MILLISECONDS); diff --git a/daemon/src/main/java/org/mvndaemon/mvnd/daemon/Server.java b/daemon/src/main/java/org/mvndaemon/mvnd/daemon/Server.java index 10172b003..776e8198f 100644 --- a/daemon/src/main/java/org/mvndaemon/mvnd/daemon/Server.java +++ b/daemon/src/main/java/org/mvndaemon/mvnd/daemon/Server.java @@ -89,6 +89,7 @@ public class Server implements AutoCloseable, Runnable { private final Lock stateLock = new ReentrantLock(); private final Condition condition = stateLock.newCondition(); private final DaemonMemoryStatus memoryStatus; + private final long keepAliveMs; public Server() throws IOException { // When spawning a new process, the child process is create within @@ -107,6 +108,8 @@ public Server() throws IOException { } this.uid = Environment.MVND_UID.asString(); this.noDaemon = Environment.MVND_NO_DAEMON.asBoolean(); + this.keepAliveMs = Environment.MVND_KEEP_ALIVE.asDuration().toMillis(); + try { cli = new DaemonMavenCli(); registry = new DaemonRegistry(Environment.MVND_REGISTRY.asPath()); @@ -424,7 +427,6 @@ private void cancelNow() { private void handle(DaemonConnection connection, BuildRequest buildRequest) { updateState(Busy); try { - Duration keepAlive = Environment.MVND_KEEP_ALIVE.asDuration(); LOGGER.info("Executing request"); @@ -440,7 +442,7 @@ private void handle(DaemonConnection connection, BuildRequest buildRequest) { while (true) { Message m; if (flushed) { - m = sendQueue.poll(keepAlive.toMillis(), TimeUnit.MILLISECONDS); + m = sendQueue.poll(keepAliveMs, TimeUnit.MILLISECONDS); if (m == null) { m = Message.KEEP_ALIVE_SINGLETON; }