Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Performance

- Avoid a `Thread.getName()` string allocation and scan on the scope-persistence hot path by detecting the Sentry executor thread via a marker instead ([#5691](https://github.com/getsentry/sentry-java/pull/5691))

## 8.47.0

### Behavioral Changes
Expand Down
1 change: 1 addition & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -3181,6 +3181,7 @@ public final class io/sentry/SentryExecutorService : io/sentry/ISentryExecutorSe
public fun <init> (Lio/sentry/SentryOptions;)V
public fun close (J)V
public fun isClosed ()Z
public static fun isSentryExecutorThread ()Z
public fun prewarm ()V
public fun schedule (Ljava/lang/Runnable;J)Ljava/util/concurrent/Future;
public fun submit (Ljava/lang/Runnable;)Ljava/util/concurrent/Future;
Expand Down
18 changes: 17 additions & 1 deletion sentry/src/main/java/io/sentry/SentryExecutorService.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,33 @@ public void prewarm() {
}
}

/**
* Whether the calling thread is one of the Sentry executor threads. Allocation-free replacement
* for scanning {@code Thread.currentThread().getName()}, which is on hot paths (e.g. scope
* persistence on every scope mutation).
*/
public static boolean isSentryExecutorThread() {
return Thread.currentThread() instanceof SentryExecutorServiceThread;
}

private static final class SentryExecutorServiceThreadFactory implements ThreadFactory {
private int cnt;

@Override
public @NotNull Thread newThread(final @NotNull Runnable r) {
final Thread ret = new Thread(r, "SentryExecutorServiceThreadFactory-" + cnt++);
final Thread ret =
new SentryExecutorServiceThread(r, "SentryExecutorServiceThreadFactory-" + cnt++);
ret.setDaemon(true);
return ret;
}
}

private static final class SentryExecutorServiceThread extends Thread {
SentryExecutorServiceThread(final @NotNull Runnable r, final @NotNull String name) {
super(r, name);
}
}

private static final class CancelledFuture<T> implements Future<T> {
@Override
public boolean cancel(final boolean mayInterruptIfRunning) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.sentry.Breadcrumb;
import io.sentry.IScope;
import io.sentry.ScopeObserverAdapter;
import io.sentry.SentryExecutorService;
import io.sentry.SentryLevel;
import io.sentry.SentryOptions;
import io.sentry.SpanContext;
Expand Down Expand Up @@ -229,7 +230,7 @@ private void serializeToDisk(final @NotNull Runnable task) {
if (!options.isEnableScopePersistence()) {
return;
}
if (Thread.currentThread().getName().contains("SentryExecutor")) {
if (SentryExecutorService.isSentryExecutorThread()) {
// we're already on the sentry executor thread, so we can just execute it directly
try {
task.run();
Expand Down
Loading