Skip to content

Commit 3594cd9

Browse files
runningcodeclaude
andauthored
ref(core): Reduce unnecessary boxing and redundant null checks (JAVA-554) (#5520)
* 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 <noreply@anthropic.com> * 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 <noreply@anthropic.com> * changelog * 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 <noreply@anthropic.com> * 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 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 0456f5c commit 3594cd9

12 files changed

Lines changed: 18 additions & 20 deletions

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Improvements
66

77
- Improve SDK init performance by replacing `java.net.URI` with custom string parsing for DSN ([#5448](https://github.com/getsentry/sentry-java/pull/5448))
8+
- Remove unnecessary boxing to improve performance ([#5520](https://github.com/getsentry/sentry-java/pull/5520))
89

910
### Fixes
1011

sentry/src/main/java/io/sentry/CircularFifoQueue.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,7 @@ public boolean add(final @NotNull E element) {
258258
if (index < 0 || index >= sz) {
259259
throw new NoSuchElementException(
260260
String.format(
261-
"The specified index (%1$d) is outside the available range [0, %2$d)",
262-
Integer.valueOf(index), Integer.valueOf(sz)));
261+
"The specified index (%1$d) is outside the available range [0, %2$d)", index, sz));
263262
}
264263

265264
final int idx = (start + index) % maxElements;

sentry/src/main/java/io/sentry/DateUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public static double nanosToMillis(final double nanos) {
115115
* @return date rounded down to milliseconds
116116
*/
117117
public static Date nanosToDate(final long nanos) {
118-
final Double millis = nanosToMillis(Double.valueOf(nanos));
118+
final Double millis = nanosToMillis((double) nanos);
119119
return getDateTime(millis.longValue());
120120
}
121121

@@ -137,7 +137,7 @@ public static Date nanosToDate(final long nanos) {
137137
* @return seconds
138138
*/
139139
public static double nanosToSeconds(final long nanos) {
140-
return Double.valueOf(nanos) / (1000.0 * 1000.0 * 1000.0);
140+
return (double) nanos / (1000.0 * 1000.0 * 1000.0);
141141
}
142142

143143
/**

sentry/src/main/java/io/sentry/ScopesStorageFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public final class ScopesStorageFactory {
2929
try {
3030
final @Nullable Object otelScopesStorage =
3131
otelScopesStorageClazz.getDeclaredConstructor().newInstance();
32-
if (otelScopesStorage != null && otelScopesStorage instanceof IScopesStorage) {
32+
if (otelScopesStorage instanceof IScopesStorage) {
3333
return (IScopesStorage) otelScopesStorage;
3434
}
3535
} catch (InstantiationException e) {

sentry/src/main/java/io/sentry/SentryDate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ public final boolean isAfter(final @NotNull SentryDate otherDate) {
4747

4848
@Override
4949
public int compareTo(@NotNull SentryDate otherDate) {
50-
return Long.valueOf(nanoTimestamp()).compareTo(otherDate.nanoTimestamp());
50+
return Long.compare(nanoTimestamp(), otherDate.nanoTimestamp());
5151
}
5252
}

sentry/src/main/java/io/sentry/SentryNanotimeDate.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public long nanoTimestamp() {
4646

4747
@Override
4848
public long laterDateNanosTimestampByDiff(final @Nullable SentryDate otherDate) {
49-
if (otherDate != null && otherDate instanceof SentryNanotimeDate) {
49+
if (otherDate instanceof SentryNanotimeDate) {
5050
final @NotNull SentryNanotimeDate otherNanoDate = (SentryNanotimeDate) otherDate;
5151
if (compareTo(otherDate) < 0) {
5252
return nanotimeDiff(this, otherNanoDate);
@@ -66,9 +66,9 @@ public int compareTo(@NotNull SentryDate otherDate) {
6666
final long thisDateMillis = date.getTime();
6767
final long otherDateMillis = otherNanoDate.date.getTime();
6868
if (thisDateMillis == otherDateMillis) {
69-
return Long.valueOf(nanos).compareTo(otherNanoDate.nanos);
69+
return Long.compare(nanos, otherNanoDate.nanos);
7070
} else {
71-
return Long.valueOf(thisDateMillis).compareTo(otherDateMillis);
71+
return Long.compare(thisDateMillis, otherDateMillis);
7272
}
7373
} else {
7474
return super.compareTo(otherDate);

sentry/src/main/java/io/sentry/SpanFactoryFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public final class SpanFactoryFactory {
2121
try {
2222
final @Nullable Object otelSpanFactory =
2323
otelSpanFactoryClazz.getDeclaredConstructor().newInstance();
24-
if (otelSpanFactory != null && otelSpanFactory instanceof ISpanFactory) {
24+
if (otelSpanFactory instanceof ISpanFactory) {
2525
return (ISpanFactory) otelSpanFactory;
2626
}
2727
} catch (InstantiationException e) {

sentry/src/main/java/io/sentry/internal/eventprocessor/EventProcessorAndOrder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ public EventProcessorAndOrder(
2929

3030
@Override
3131
public int compareTo(@NotNull EventProcessorAndOrder o) {
32-
return order.compareTo(o.order);
32+
return Long.compare(order, o.order);
3333
}
3434
}

sentry/src/main/java/io/sentry/protocol/Contexts.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ public void putAll(final @Nullable Contexts contexts) {
282282

283283
@Override
284284
public boolean equals(final @Nullable Object obj) {
285-
if (obj != null && obj instanceof Contexts) {
285+
if (obj instanceof Contexts) {
286286
final @NotNull Contexts otherContexts = (Contexts) obj;
287287
return internalStorage.equals(otherContexts.internalStorage);
288288
}

sentry/src/main/java/io/sentry/util/LifecycleHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
public final class LifecycleHelper {
88

99
public static void close(final @Nullable Object tokenObject) {
10-
if (tokenObject != null && tokenObject instanceof ISentryLifecycleToken) {
10+
if (tokenObject instanceof ISentryLifecycleToken) {
1111
final @NotNull ISentryLifecycleToken token = (ISentryLifecycleToken) tokenObject;
1212
token.close();
1313
}

0 commit comments

Comments
 (0)