From b8da97f174e2f90a78c297a8922eb19f3224a92e Mon Sep 17 00:00:00 2001 From: Mina Asham Date: Mon, 10 Feb 2025 09:54:33 +0000 Subject: [PATCH] Avoid creating error message string prematurely - Creating the string message prematurely puts a lot of strain on the JVM and GC, switch to the format version of `Preconditions.checkState` which only constructs the string if we throw the exception - Issue: https://github.com/googleapis/sdk-platform-java/issues/3621 --- .../google/api/gax/rpc/StateCheckingResponseObserver.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/gax-java/gax/src/main/java/com/google/api/gax/rpc/StateCheckingResponseObserver.java b/gax-java/gax/src/main/java/com/google/api/gax/rpc/StateCheckingResponseObserver.java index d787f434dc..fab764f4f2 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/rpc/StateCheckingResponseObserver.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/rpc/StateCheckingResponseObserver.java @@ -43,7 +43,7 @@ public abstract class StateCheckingResponseObserver implements ResponseObserv * ensuring consistent state. */ public final void onStart(StreamController controller) { - Preconditions.checkState(!isStarted, getClass() + " is already started."); + Preconditions.checkState(!isStarted, "%s is already started.", getClass()); isStarted = true; onStartImpl(controller); @@ -56,7 +56,7 @@ public final void onStart(StreamController controller) { * consistent state. */ public final void onResponse(V response) { - Preconditions.checkState(!isClosed, getClass() + " received a response after being closed."); + Preconditions.checkState(!isClosed, "%s received a response after being closed.", getClass()); onResponseImpl(response); } @@ -67,7 +67,7 @@ public final void onResponse(V response) { * state. */ public final void onComplete() { - Preconditions.checkState(!isClosed, getClass() + " tried to double close."); + Preconditions.checkState(!isClosed, "%s tried to double close.", getClass()); isClosed = true; onCompleteImpl(); } @@ -79,7 +79,7 @@ public final void onComplete() { * consistent state. */ public final void onError(Throwable t) { - Preconditions.checkState(!isClosed, getClass() + " received error after being closed", t); + Preconditions.checkState(!isClosed, "%s received error after being closed", t, getClass()); isClosed = true; onErrorImpl(t); }