Skip to content

Commit 307853f

Browse files
authored
Merge pull request #5600 from getsentry/perf/sdk-overhead-reduction-reflection-visiting
perf(core): [SDK Overhead Reduction 9] Lazily allocate reflection serializer state
2 parents f0276a6 + 05d60fb commit 307853f

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Internal
66

7+
- Reduce JSON serialization overhead by allocating reflection cycle-tracking state only when reflection serialization is used. ([#5600](https://github.com/getsentry/sentry-java/pull/5600))
78
- Reduce context serialization overhead by sorting key snapshots with arrays instead of temporary lists. ([#5599](https://github.com/getsentry/sentry-java/pull/5599))
89
- Reduce breadcrumb allocation overhead by creating the `Breadcrumb` data map only when data is added. ([#5598](https://github.com/getsentry/sentry-java/pull/5598))
910
- Reduce JSON serialization overhead by lowering the initial `JsonWriter` nesting stack size while preserving on-demand growth. ([#5591](https://github.com/getsentry/sentry-java/pull/5591))

sentry/src/main/java/io/sentry/JsonReflectionObjectSerializer.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
@ApiStatus.Internal
3131
public final class JsonReflectionObjectSerializer {
3232

33-
private final Set<Object> visiting = new HashSet<>();
33+
private @Nullable Set<Object> visiting;
3434
private final int maxDepth;
3535

3636
JsonReflectionObjectSerializer(int maxDepth) {
@@ -69,6 +69,7 @@ public final class JsonReflectionObjectSerializer {
6969
} else if (object.getClass().isEnum()) {
7070
return object.toString();
7171
} else {
72+
final Set<Object> visiting = getVisiting();
7273
if (visiting.contains(object)) {
7374
logger.log(SentryLevel.INFO, "Cyclic reference detected. Calling toString() on object.");
7475
return object.toString();
@@ -135,6 +136,13 @@ public final class JsonReflectionObjectSerializer {
135136

136137
// Helper
137138

139+
private @NotNull Set<Object> getVisiting() {
140+
if (visiting == null) {
141+
visiting = new HashSet<>();
142+
}
143+
return visiting;
144+
}
145+
138146
private @NotNull List<Object> list(@NotNull Object[] objectArray, @NotNull ILogger logger)
139147
throws Exception {
140148
List<Object> list = new ArrayList<>();

0 commit comments

Comments
 (0)