From 9c88122074387b8e39781060596073045a888f1f Mon Sep 17 00:00:00 2001 From: Andrew Ash Date: Wed, 13 Mar 2024 03:56:56 -0700 Subject: [PATCH] Avoid computing expensive debug info for all checkState() calls (#1029) Avoid computing expensive debug info for all checkState() calls --- changelog/@unreleased/pr-1029.v2.yml | 5 +++++ .../javaformat/doc/CountWidthUntilBreakVisitor.java | 12 +++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) create mode 100644 changelog/@unreleased/pr-1029.v2.yml diff --git a/changelog/@unreleased/pr-1029.v2.yml b/changelog/@unreleased/pr-1029.v2.yml new file mode 100644 index 000000000..35e9775b9 --- /dev/null +++ b/changelog/@unreleased/pr-1029.v2.yml @@ -0,0 +1,5 @@ +type: fix +fix: + description: Avoid computing expensive debug info for all checkState() calls + links: + - https://github.com/palantir/palantir-java-format/pull/1029 diff --git a/palantir-java-format/src/main/java/com/palantir/javaformat/doc/CountWidthUntilBreakVisitor.java b/palantir-java-format/src/main/java/com/palantir/javaformat/doc/CountWidthUntilBreakVisitor.java index 9f65541a2..022aa159e 100644 --- a/palantir-java-format/src/main/java/com/palantir/javaformat/doc/CountWidthUntilBreakVisitor.java +++ b/palantir-java-format/src/main/java/com/palantir/javaformat/doc/CountWidthUntilBreakVisitor.java @@ -16,7 +16,6 @@ package com.palantir.javaformat.doc; -import com.google.common.base.Preconditions; import com.palantir.javaformat.PartialInlineability; import com.palantir.javaformat.doc.StartsWithBreakVisitor.Result; import java.util.List; @@ -69,10 +68,13 @@ public Float visitLevel(Level level) { return visit(level.getDocs().get(found.getAsInt())); } // Otherwise, assert that we encountered a break and move on. - Preconditions.checkState( - StartsWithBreakVisitor.INSTANCE.visit(level) == Result.YES, - "Didn't find expected break at the beginning of level.\n%s", - level.representation(State.startingState())); + if (StartsWithBreakVisitor.INSTANCE.visit(level) != Result.YES) { + // Avoid computing expensive level.representation() if we aren't throwing it in an exception. + throw new IllegalStateException(String.format( + "Didn't find expected break at the beginning of level.\n%s", + level.representation(State.startingState()))); + } + return 0f; }