Skip to content

Commit 078ca0d

Browse files
authored
perf(modules): Pre-load modules on a background thread (#4348)
* perf(modules): Pre-load modules on a background thread * Changelog * Use a simple Thread instead of executor service
1 parent 5b790bc commit 078ca0d

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
### Improvements
1919

2020
- Make user interaction tracing faster and do fewer allocations ([#4347](https://github.com/getsentry/sentry-java/pull/4347))
21+
- Pre-load modules on a background thread upon SDK init ([#4348](https://github.com/getsentry/sentry-java/pull/4348))
2122

2223
## 8.8.0
2324

sentry-android-core/src/main/java/io/sentry/android/core/internal/modules/AssetsModulesLoader.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ public final class AssetsModulesLoader extends ModulesLoader {
2121
public AssetsModulesLoader(final @NotNull Context context, final @NotNull ILogger logger) {
2222
super(logger);
2323
this.context = ContextUtils.getApplicationContext(context);
24+
25+
// pre-load modules on a bg thread to avoid doing so on the main thread in case of a crash/error
26+
//noinspection Convert2MethodRef
27+
new Thread(() -> getOrLoadModules()).start();
2428
}
2529

2630
@Override

sentry/src/main/java/io/sentry/internal/modules/ModulesLoader.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package io.sentry.internal.modules;
22

33
import io.sentry.ILogger;
4+
import io.sentry.ISentryLifecycleToken;
45
import io.sentry.SentryLevel;
6+
import io.sentry.util.AutoClosableReentrantLock;
57
import java.io.BufferedReader;
68
import java.io.IOException;
79
import java.io.InputStream;
@@ -21,18 +23,23 @@ public abstract class ModulesLoader implements IModulesLoader {
2123

2224
public static final String EXTERNAL_MODULES_FILENAME = "sentry-external-modules.txt";
2325
protected final @NotNull ILogger logger;
24-
private @Nullable Map<String, String> cachedModules = null;
26+
27+
private final @NotNull AutoClosableReentrantLock modulesLock = new AutoClosableReentrantLock();
28+
private volatile @Nullable Map<String, String> cachedModules = null;
2529

2630
public ModulesLoader(final @NotNull ILogger logger) {
2731
this.logger = logger;
2832
}
2933

3034
@Override
3135
public @Nullable Map<String, String> getOrLoadModules() {
32-
if (cachedModules != null) {
33-
return cachedModules;
36+
if (cachedModules == null) {
37+
try (final @NotNull ISentryLifecycleToken ignored = modulesLock.acquire()) {
38+
if (cachedModules == null) {
39+
cachedModules = loadModules();
40+
}
41+
}
3442
}
35-
cachedModules = loadModules();
3643
return cachedModules;
3744
}
3845

0 commit comments

Comments
 (0)