From 43baf95974145ea3afc149b1772aeff71257d15d Mon Sep 17 00:00:00 2001 From: Nelson Osacky Date: Wed, 10 Jun 2026 09:08:57 +0200 Subject: [PATCH 1/5] ref(core): Use static compare and drop redundant null checks Replace boxed Long.valueOf(...).compareTo(...) with Long.compare(...), which avoids the unnecessary boxing. Also remove the redundant != null checks that precede an instanceof, since instanceof already returns false for null. Co-Authored-By: Claude Opus 4.8 --- sentry/src/main/java/io/sentry/ScopesStorageFactory.java | 2 +- sentry/src/main/java/io/sentry/SentryDate.java | 2 +- sentry/src/main/java/io/sentry/SentryNanotimeDate.java | 6 +++--- sentry/src/main/java/io/sentry/SpanFactoryFactory.java | 2 +- .../internal/eventprocessor/EventProcessorAndOrder.java | 2 +- sentry/src/main/java/io/sentry/protocol/Contexts.java | 2 +- sentry/src/main/java/io/sentry/util/LifecycleHelper.java | 2 +- 7 files changed, 9 insertions(+), 9 deletions(-) diff --git a/sentry/src/main/java/io/sentry/ScopesStorageFactory.java b/sentry/src/main/java/io/sentry/ScopesStorageFactory.java index 89fa6389072..37c0acf2314 100644 --- a/sentry/src/main/java/io/sentry/ScopesStorageFactory.java +++ b/sentry/src/main/java/io/sentry/ScopesStorageFactory.java @@ -29,7 +29,7 @@ public final class ScopesStorageFactory { try { final @Nullable Object otelScopesStorage = otelScopesStorageClazz.getDeclaredConstructor().newInstance(); - if (otelScopesStorage != null && otelScopesStorage instanceof IScopesStorage) { + if (otelScopesStorage instanceof IScopesStorage) { return (IScopesStorage) otelScopesStorage; } } catch (InstantiationException e) { diff --git a/sentry/src/main/java/io/sentry/SentryDate.java b/sentry/src/main/java/io/sentry/SentryDate.java index d2620ab3024..03ea596b07a 100644 --- a/sentry/src/main/java/io/sentry/SentryDate.java +++ b/sentry/src/main/java/io/sentry/SentryDate.java @@ -47,6 +47,6 @@ public final boolean isAfter(final @NotNull SentryDate otherDate) { @Override public int compareTo(@NotNull SentryDate otherDate) { - return Long.valueOf(nanoTimestamp()).compareTo(otherDate.nanoTimestamp()); + return Long.compare(nanoTimestamp(), otherDate.nanoTimestamp()); } } diff --git a/sentry/src/main/java/io/sentry/SentryNanotimeDate.java b/sentry/src/main/java/io/sentry/SentryNanotimeDate.java index 2993eeed6c6..98c46ad5325 100644 --- a/sentry/src/main/java/io/sentry/SentryNanotimeDate.java +++ b/sentry/src/main/java/io/sentry/SentryNanotimeDate.java @@ -46,7 +46,7 @@ public long nanoTimestamp() { @Override public long laterDateNanosTimestampByDiff(final @Nullable SentryDate otherDate) { - if (otherDate != null && otherDate instanceof SentryNanotimeDate) { + if (otherDate instanceof SentryNanotimeDate) { final @NotNull SentryNanotimeDate otherNanoDate = (SentryNanotimeDate) otherDate; if (compareTo(otherDate) < 0) { return nanotimeDiff(this, otherNanoDate); @@ -66,9 +66,9 @@ public int compareTo(@NotNull SentryDate otherDate) { final long thisDateMillis = date.getTime(); final long otherDateMillis = otherNanoDate.date.getTime(); if (thisDateMillis == otherDateMillis) { - return Long.valueOf(nanos).compareTo(otherNanoDate.nanos); + return Long.compare(nanos, otherNanoDate.nanos); } else { - return Long.valueOf(thisDateMillis).compareTo(otherDateMillis); + return Long.compare(thisDateMillis, otherDateMillis); } } else { return super.compareTo(otherDate); diff --git a/sentry/src/main/java/io/sentry/SpanFactoryFactory.java b/sentry/src/main/java/io/sentry/SpanFactoryFactory.java index 7dbb9f1f588..f0e3fcbb3c7 100644 --- a/sentry/src/main/java/io/sentry/SpanFactoryFactory.java +++ b/sentry/src/main/java/io/sentry/SpanFactoryFactory.java @@ -21,7 +21,7 @@ public final class SpanFactoryFactory { try { final @Nullable Object otelSpanFactory = otelSpanFactoryClazz.getDeclaredConstructor().newInstance(); - if (otelSpanFactory != null && otelSpanFactory instanceof ISpanFactory) { + if (otelSpanFactory instanceof ISpanFactory) { return (ISpanFactory) otelSpanFactory; } } catch (InstantiationException e) { diff --git a/sentry/src/main/java/io/sentry/internal/eventprocessor/EventProcessorAndOrder.java b/sentry/src/main/java/io/sentry/internal/eventprocessor/EventProcessorAndOrder.java index 1f504f25557..1ca5f70df8f 100644 --- a/sentry/src/main/java/io/sentry/internal/eventprocessor/EventProcessorAndOrder.java +++ b/sentry/src/main/java/io/sentry/internal/eventprocessor/EventProcessorAndOrder.java @@ -29,6 +29,6 @@ public EventProcessorAndOrder( @Override public int compareTo(@NotNull EventProcessorAndOrder o) { - return order.compareTo(o.order); + return Long.compare(order, o.order); } } diff --git a/sentry/src/main/java/io/sentry/protocol/Contexts.java b/sentry/src/main/java/io/sentry/protocol/Contexts.java index fd1e9b83eb6..35168e5bcc2 100644 --- a/sentry/src/main/java/io/sentry/protocol/Contexts.java +++ b/sentry/src/main/java/io/sentry/protocol/Contexts.java @@ -282,7 +282,7 @@ public void putAll(final @Nullable Contexts contexts) { @Override public boolean equals(final @Nullable Object obj) { - if (obj != null && obj instanceof Contexts) { + if (obj instanceof Contexts) { final @NotNull Contexts otherContexts = (Contexts) obj; return internalStorage.equals(otherContexts.internalStorage); } diff --git a/sentry/src/main/java/io/sentry/util/LifecycleHelper.java b/sentry/src/main/java/io/sentry/util/LifecycleHelper.java index 4a029f620cc..fc6e9e74120 100644 --- a/sentry/src/main/java/io/sentry/util/LifecycleHelper.java +++ b/sentry/src/main/java/io/sentry/util/LifecycleHelper.java @@ -7,7 +7,7 @@ public final class LifecycleHelper { public static void close(final @Nullable Object tokenObject) { - if (tokenObject != null && tokenObject instanceof ISentryLifecycleToken) { + if (tokenObject instanceof ISentryLifecycleToken) { final @NotNull ISentryLifecycleToken token = (ISentryLifecycleToken) tokenObject; token.close(); } From 044b8ec22e9dc96f11cf9988854a416f8e363f79 Mon Sep 17 00:00:00 2001 From: Nelson Osacky Date: Wed, 10 Jun 2026 09:12:47 +0200 Subject: [PATCH 2/5] ref(core): Remove unnecessary boxing (JAVA-554) Replace Integer.valueOf/Double.valueOf boxing with primitives or the appropriate parse method. String.format takes the primitives directly, the double conversions only need a cast, and the version check parses straight to a primitive double via Double.parseDouble. Co-Authored-By: Claude Opus 4.8 --- sentry/src/main/java/io/sentry/CircularFifoQueue.java | 3 +-- sentry/src/main/java/io/sentry/DateUtils.java | 4 ++-- sentry/src/main/java/io/sentry/util/Platform.java | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/sentry/src/main/java/io/sentry/CircularFifoQueue.java b/sentry/src/main/java/io/sentry/CircularFifoQueue.java index 8fa72e39d56..4c6a123d512 100644 --- a/sentry/src/main/java/io/sentry/CircularFifoQueue.java +++ b/sentry/src/main/java/io/sentry/CircularFifoQueue.java @@ -258,8 +258,7 @@ public boolean add(final @NotNull E element) { if (index < 0 || index >= sz) { throw new NoSuchElementException( String.format( - "The specified index (%1$d) is outside the available range [0, %2$d)", - Integer.valueOf(index), Integer.valueOf(sz))); + "The specified index (%1$d) is outside the available range [0, %2$d)", index, sz)); } final int idx = (start + index) % maxElements; diff --git a/sentry/src/main/java/io/sentry/DateUtils.java b/sentry/src/main/java/io/sentry/DateUtils.java index 31a8dcd76ea..f7c46844edc 100644 --- a/sentry/src/main/java/io/sentry/DateUtils.java +++ b/sentry/src/main/java/io/sentry/DateUtils.java @@ -115,7 +115,7 @@ public static double nanosToMillis(final double nanos) { * @return date rounded down to milliseconds */ public static Date nanosToDate(final long nanos) { - final Double millis = nanosToMillis(Double.valueOf(nanos)); + final Double millis = nanosToMillis((double) nanos); return getDateTime(millis.longValue()); } @@ -137,7 +137,7 @@ public static Date nanosToDate(final long nanos) { * @return seconds */ public static double nanosToSeconds(final long nanos) { - return Double.valueOf(nanos) / (1000.0 * 1000.0 * 1000.0); + return (double) nanos / (1000.0 * 1000.0 * 1000.0); } /** diff --git a/sentry/src/main/java/io/sentry/util/Platform.java b/sentry/src/main/java/io/sentry/util/Platform.java index b08b6e584fb..cc924fb2815 100644 --- a/sentry/src/main/java/io/sentry/util/Platform.java +++ b/sentry/src/main/java/io/sentry/util/Platform.java @@ -23,7 +23,7 @@ public final class Platform { try { final @Nullable String javaStringVersion = System.getProperty("java.specification.version"); if (javaStringVersion != null) { - final @NotNull double javaVersion = Double.valueOf(javaStringVersion); + final @NotNull double javaVersion = Double.parseDouble(javaStringVersion); isJavaNinePlus = javaVersion >= 9.0; } else { isJavaNinePlus = false; From 7ed7c97be218c20ef51e5378acd18319e68eed33 Mon Sep 17 00:00:00 2001 From: Nelson Osacky Date: Wed, 10 Jun 2026 09:16:04 +0200 Subject: [PATCH 3/5] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index be4a5f7628d..6a813a8e7b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Improvements - Improve SDK init performance by replacing `java.net.URI` with custom string parsing for DSN ([#5448](https://github.com/getsentry/sentry-java/pull/5448)) +- Remove unnecessary boxing to improve performance ([#5520](https://github.com/getsentry/sentry-java/pull/5520)) ### Fixes From c248e08b1014208fda3af6cf52acce7797698a15 Mon Sep 17 00:00:00 2001 From: Nelson Osacky Date: Wed, 10 Jun 2026 09:20:54 +0200 Subject: [PATCH 4/5] ref(core): Drop redundant StringBuilder in hashing helper (JAVA-554) Return the hex string directly instead of wrapping it in a StringBuilder only to immediately call toString(). Co-Authored-By: Claude Opus 4.8 --- sentry/src/main/java/io/sentry/util/StringUtils.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sentry/src/main/java/io/sentry/util/StringUtils.java b/sentry/src/main/java/io/sentry/util/StringUtils.java index 66e3a95ddb7..c35da8d5748 100644 --- a/sentry/src/main/java/io/sentry/util/StringUtils.java +++ b/sentry/src/main/java/io/sentry/util/StringUtils.java @@ -143,10 +143,9 @@ private StringUtils() {} final BigInteger no = new BigInteger(1, messageDigest); // Convert message digest into hex value - final StringBuilder stringBuilder = new StringBuilder(no.toString(16)); // return the HashText - return stringBuilder.toString(); + return no.toString(16); } // For specifying wrong message digest algorithms From 5859b9040be971086f4cb4f521e22036ba91ede0 Mon Sep 17 00:00:00 2001 From: Nelson Osacky Date: Wed, 10 Jun 2026 09:21:45 +0200 Subject: [PATCH 5/5] ref(core): Use StandardCharsets.UTF_8 and tidy comments (JAVA-554) Replace Charset.forName("UTF-8") with the StandardCharsets constant, which avoids the lookup and cannot throw a checked exception. Also collapse the leftover hashing comments into one. Co-Authored-By: Claude Opus 4.8 --- sentry/src/main/java/io/sentry/util/StringUtils.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/sentry/src/main/java/io/sentry/util/StringUtils.java b/sentry/src/main/java/io/sentry/util/StringUtils.java index c35da8d5748..02d7d4636a4 100644 --- a/sentry/src/main/java/io/sentry/util/StringUtils.java +++ b/sentry/src/main/java/io/sentry/util/StringUtils.java @@ -4,6 +4,7 @@ import io.sentry.SentryLevel; import java.math.BigInteger; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.text.CharacterIterator; @@ -18,7 +19,7 @@ @ApiStatus.Internal public final class StringUtils { - private static final Charset UTF_8 = Charset.forName("UTF-8"); + private static final Charset UTF_8 = StandardCharsets.UTF_8; public static final String PROPER_NIL_UUID = "00000000-0000-0000-0000-000000000000"; private static final String CORRUPTED_NIL_UUID = "0000-0000"; @@ -142,9 +143,7 @@ private StringUtils() {} // Convert byte array into signum representation final BigInteger no = new BigInteger(1, messageDigest); - // Convert message digest into hex value - - // return the HashText + // Convert message digest into hex value and return the HashText return no.toString(16); }