Skip to content

Commit 507d84e

Browse files
authored
Merge branch 'perf/sdk-overhead-reduction' into perf/sdk-overhead-reduction-contexts-key-array
2 parents 7e885d1 + b070303 commit 507d84e

8 files changed

Lines changed: 129 additions & 28 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
### Internal
66

77
- Reduce context serialization overhead by sorting key snapshots with arrays instead of temporary lists. ([#5599](https://github.com/getsentry/sentry-java/pull/5599))
8+
- Reduce breadcrumb allocation overhead by creating the `Breadcrumb` data map only when data is added. ([#5598](https://github.com/getsentry/sentry-java/pull/5598))
9+
- 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))
10+
- Reduce timestamp helper overhead by replacing unnecessary `Calendar` usage in `DateUtils` with direct `Date` creation. ([#5589](https://github.com/getsentry/sentry-java/pull/5589))
11+
- Reduce Android startup overhead by using the default timezone directly on older devices or when no timezone info is available in the locale. ([#5587](https://github.com/getsentry/sentry-java/pull/5587))
812

913
## 8.45.0
1014

sentry-android-core/src/main/java/io/sentry/android/core/DeviceInfoUtil.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,19 @@ private void setDeviceIO(
257257
@SuppressWarnings("NewApi")
258258
@NotNull
259259
private TimeZone getTimeZone() {
260-
if (buildInfoProvider.getSdkInfoVersion() >= Build.VERSION_CODES.N) {
260+
// Only use the costly Calendar API on Android 13+ (API Level 33+) when the locale contains a
261+
// Unicode timezone extension (for example "en-US-u-tz-usnyc"), because Calendar honors that
262+
// extension. For all other cases, use the process default timezone directly for performance.
263+
if (buildInfoProvider.getSdkInfoVersion() >= Build.VERSION_CODES.TIRAMISU) {
261264
LocaleList locales = context.getResources().getConfiguration().getLocales();
262265
if (!locales.isEmpty()) {
263266
Locale locale = locales.get(0);
264-
return Calendar.getInstance(locale).getTimeZone();
267+
if (locale.getUnicodeLocaleType("tz") != null) {
268+
return Calendar.getInstance(locale).getTimeZone();
269+
}
265270
}
266271
}
267-
return Calendar.getInstance().getTimeZone();
272+
return TimeZone.getDefault();
268273
}
269274

270275
@SuppressWarnings("JdkObsolete")

sentry-android-core/src/test/java/io/sentry/android/core/DeviceInfoUtilTest.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@ package io.sentry.android.core
22

33
import android.content.Context
44
import android.content.Intent
5+
import android.content.res.Configuration
56
import android.os.BatteryManager
7+
import android.os.Build
8+
import android.os.LocaleList
69
import androidx.test.core.app.ApplicationProvider
710
import androidx.test.ext.junit.runners.AndroidJUnit4
811
import io.sentry.android.core.internal.util.CpuInfoUtils
12+
import java.util.Locale
13+
import java.util.TimeZone
914
import kotlin.test.BeforeTest
1015
import kotlin.test.Test
1116
import kotlin.test.assertEquals
1217
import kotlin.test.assertNotNull
1318
import kotlin.test.assertNull
1419
import org.junit.runner.RunWith
20+
import org.robolectric.annotation.Config
1521

1622
@RunWith(AndroidJUnit4::class)
1723
class DeviceInfoUtilTest {
@@ -47,6 +53,32 @@ class DeviceInfoUtilTest {
4753
assertNotNull(deviceInfo.memorySize)
4854
}
4955

56+
@Test
57+
fun `sets default timezone`() {
58+
val deviceInfoUtil = DeviceInfoUtil.getInstance(context, SentryAndroidOptions())
59+
val deviceInfo = deviceInfoUtil.collectDeviceInformation(false, false)
60+
61+
assertEquals(TimeZone.getDefault(), deviceInfo.timezone)
62+
}
63+
64+
@Test
65+
@Config(sdk = [Build.VERSION_CODES.TIRAMISU])
66+
fun `preserves timezone from locale unicode extension`() {
67+
val defaultTimeZone = TimeZone.getDefault()
68+
try {
69+
TimeZone.setDefault(TimeZone.getTimeZone("UTC"))
70+
val configuration = Configuration(context.resources.configuration)
71+
configuration.setLocales(LocaleList(Locale.forLanguageTag("en-US-u-tz-usnyc")))
72+
val localizedContext = context.createConfigurationContext(configuration)
73+
val deviceInfoUtil = DeviceInfoUtil(localizedContext, SentryAndroidOptions())
74+
val deviceInfo = deviceInfoUtil.collectDeviceInformation(false, false)
75+
76+
assertEquals("America/New_York", deviceInfo.timezone?.id)
77+
} finally {
78+
TimeZone.setDefault(defaultTimeZone)
79+
}
80+
}
81+
5082
@Test
5183
fun `does include cpu data`() {
5284
CpuInfoUtils.getInstance().setCpuMaxFrequencies(listOf(1024))

sentry/src/main/java/io/sentry/Breadcrumb.java

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ public final class Breadcrumb implements JsonUnknown, JsonSerializable, Comparab
3434
/** The type of breadcrumb. */
3535
private @Nullable String type;
3636

37+
private static final @NotNull Map<String, @NotNull Object> EMPTY_DATA = Collections.emptyMap();
38+
3739
/** Data associated with this breadcrumb. */
38-
private @NotNull Map<String, @NotNull Object> data = new ConcurrentHashMap<>();
40+
private volatile @NotNull Map<String, @NotNull Object> data = EMPTY_DATA;
3941

4042
/** Dotted strings that indicate what the crumb is or where it comes from. */
4143
private @Nullable String category;
@@ -78,9 +80,11 @@ public Breadcrumb(final long timestamp) {
7880
this.type = breadcrumb.type;
7981
this.category = breadcrumb.category;
8082
this.origin = breadcrumb.origin;
81-
final Map<String, Object> dataClone = CollectionUtils.newConcurrentHashMap(breadcrumb.data);
82-
if (dataClone != null) {
83-
this.data = dataClone;
83+
if (!breadcrumb.data.isEmpty()) {
84+
final Map<String, Object> dataClone = CollectionUtils.newConcurrentHashMap(breadcrumb.data);
85+
if (dataClone != null) {
86+
this.data = dataClone;
87+
}
8488
}
8589
this.unknown = CollectionUtils.newConcurrentHashMap(breadcrumb.unknown);
8690
this.level = breadcrumb.level;
@@ -100,7 +104,7 @@ public static Breadcrumb fromMap(
100104
@NotNull Date timestamp = DateUtils.getCurrentDateTime();
101105
String message = null;
102106
String type = null;
103-
@NotNull Map<String, Object> data = new ConcurrentHashMap<>();
107+
Map<String, Object> data = null;
104108
String category = null;
105109
String origin = null;
106110
SentryLevel level = null;
@@ -129,6 +133,9 @@ public static Breadcrumb fromMap(
129133
if (untypedData != null) {
130134
for (Map.Entry<Object, Object> dataEntry : untypedData.entrySet()) {
131135
if (dataEntry.getKey() instanceof String && dataEntry.getValue() != null) {
136+
if (data == null) {
137+
data = new ConcurrentHashMap<>();
138+
}
132139
data.put((String) dataEntry.getKey(), dataEntry.getValue());
133140
} else {
134141
options
@@ -166,7 +173,9 @@ public static Breadcrumb fromMap(
166173
final Breadcrumb breadcrumb = new Breadcrumb(timestamp);
167174
breadcrumb.message = message;
168175
breadcrumb.type = type;
169-
breadcrumb.data = data;
176+
if (data != null) {
177+
breadcrumb.data = data;
178+
}
170179
breadcrumb.category = category;
171180
breadcrumb.origin = origin;
172181
breadcrumb.level = level;
@@ -494,7 +503,7 @@ public static Breadcrumb fromMap(
494503
breadcrumb.setData("view.tag", viewTag);
495504
}
496505
for (final Map.Entry<String, Object> entry : additionalData.entrySet()) {
497-
breadcrumb.getData().put(entry.getKey(), entry.getValue());
506+
breadcrumb.setData(entry.getKey(), entry.getValue());
498507
}
499508
breadcrumb.setLevel(SentryLevel.INFO);
500509
return breadcrumb;
@@ -555,7 +564,7 @@ public Breadcrumb(@Nullable String message) {
555564
if (timestamp != null) {
556565
return (Date) timestamp.clone();
557566
} else if (timestampMs != null) {
558-
// we memoize it here into timestamp to avoid instantiating Calendar again and again
567+
// we memoize it here into timestamp to avoid creating a Date again and again
559568
timestamp = DateUtils.getDateTime(timestampMs);
560569
return timestamp;
561570
}
@@ -598,6 +607,20 @@ public void setType(@Nullable String type) {
598607
this.type = type;
599608
}
600609

610+
private @NotNull Map<String, @NotNull Object> getOrCreateData() {
611+
Map<String, @NotNull Object> currentData = data;
612+
if (currentData == EMPTY_DATA) {
613+
synchronized (this) {
614+
currentData = data;
615+
if (currentData == EMPTY_DATA) {
616+
currentData = new ConcurrentHashMap<>();
617+
data = currentData;
618+
}
619+
}
620+
}
621+
return currentData;
622+
}
623+
601624
/**
602625
* Returns the data map
603626
*
@@ -606,7 +629,7 @@ public void setType(@Nullable String type) {
606629
@ApiStatus.Internal
607630
@NotNull
608631
public Map<String, Object> getData() {
609-
return data;
632+
return getOrCreateData();
610633
}
611634

612635
/**
@@ -636,7 +659,7 @@ public void setData(@Nullable String key, @Nullable Object value) {
636659
if (value == null) {
637660
removeData(key);
638661
} else {
639-
data.put(key, value);
662+
getOrCreateData().put(key, value);
640663
}
641664
}
642665

@@ -649,7 +672,10 @@ public void removeData(@Nullable String key) {
649672
if (key == null) {
650673
return;
651674
}
652-
data.remove(key);
675+
final Map<String, @NotNull Object> currentData = data;
676+
if (currentData != EMPTY_DATA) {
677+
currentData.remove(key);
678+
}
653679
}
654680

655681
/**
@@ -859,7 +885,7 @@ public static final class Deserializer implements JsonDeserializer<Breadcrumb> {
859885
@NotNull Date timestamp = DateUtils.getCurrentDateTime();
860886
String message = null;
861887
String type = null;
862-
@NotNull Map<String, Object> data = new ConcurrentHashMap<>();
888+
Map<String, Object> data = null;
863889
String category = null;
864890
String origin = null;
865891
SentryLevel level = null;
@@ -884,7 +910,7 @@ public static final class Deserializer implements JsonDeserializer<Breadcrumb> {
884910
Map<String, Object> deserializedData =
885911
CollectionUtils.newConcurrentHashMap(
886912
(Map<String, Object>) reader.nextObjectOrNull());
887-
if (deserializedData != null) {
913+
if (deserializedData != null && !deserializedData.isEmpty()) {
888914
data = deserializedData;
889915
}
890916
break;
@@ -913,7 +939,9 @@ public static final class Deserializer implements JsonDeserializer<Breadcrumb> {
913939
Breadcrumb breadcrumb = new Breadcrumb(timestamp);
914940
breadcrumb.message = message;
915941
breadcrumb.type = type;
916-
breadcrumb.data = data;
942+
if (data != null) {
943+
breadcrumb.data = data;
944+
}
917945
breadcrumb.category = category;
918946
breadcrumb.origin = origin;
919947
breadcrumb.level = level;

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

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

3-
import static io.sentry.vendor.gson.internal.bind.util.ISO8601Utils.TIMEZONE_UTC;
4-
53
import io.sentry.vendor.gson.internal.bind.util.ISO8601Utils;
64
import java.math.BigDecimal;
75
import java.math.RoundingMode;
86
import java.text.ParseException;
97
import java.text.ParsePosition;
10-
import java.util.Calendar;
118
import java.util.Date;
129
import org.jetbrains.annotations.ApiStatus;
1310
import org.jetbrains.annotations.NotNull;
@@ -24,10 +21,9 @@ private DateUtils() {}
2421
*
2522
* @return the UTC Date
2623
*/
27-
@SuppressWarnings("JdkObsolete")
24+
@SuppressWarnings("JavaUtilDate")
2825
public static @NotNull Date getCurrentDateTime() {
29-
final Calendar calendar = Calendar.getInstance(TIMEZONE_UTC);
30-
return calendar.getTime();
26+
return new Date();
3127
}
3228

3329
/**
@@ -78,10 +74,9 @@ private DateUtils() {}
7874
* @param millis the UTC millis from the epoch
7975
* @return the UTC Date
8076
*/
77+
@SuppressWarnings("JavaUtilDate")
8178
public static @NotNull Date getDateTime(final long millis) {
82-
final Calendar calendar = Calendar.getInstance(TIMEZONE_UTC);
83-
calendar.setTimeInMillis(millis);
84-
return calendar.getTime();
79+
return new Date(millis);
8580
}
8681

8782
/**

sentry/src/main/java/io/sentry/vendor/gson/stream/JsonWriter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
// Source: https://github.com/google/gson
1818
// Tag: gson-parent-2.8.7
1919
// Commit Hash: 4520489c29e770c64b11ca35e0a0fdf17a1874ab
20-
// Changes: @ApiStatus.Internal, SuppressWarnings
20+
// Changes: @ApiStatus.Internal, SuppressWarnings, reduced stack size
2121

2222
package io.sentry.vendor.gson.stream;
2323

@@ -175,7 +175,7 @@ public class JsonWriter implements Closeable, Flushable {
175175
/** The output data, containing at most one top-level array or object. */
176176
private final Writer out;
177177

178-
private int[] stack = new int[32];
178+
private int[] stack = new int[8];
179179
private int stackSize = 0;
180180
{
181181
push(EMPTY_DOCUMENT);

sentry/src/test/java/io/sentry/BreadcrumbTest.kt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package io.sentry
22

33
import java.util.Date
4+
import java.util.concurrent.CountDownLatch
5+
import java.util.concurrent.Executors
6+
import java.util.concurrent.TimeUnit
47
import kotlin.test.Test
58
import kotlin.test.assertEquals
69
import kotlin.test.assertFalse
@@ -329,6 +332,39 @@ class BreadcrumbTest {
329332
breadcrumb.removeData(null)
330333
}
331334

335+
@Test
336+
fun `getData returns mutable map for new breadcrumb`() {
337+
val breadcrumb = Breadcrumb()
338+
339+
breadcrumb.data["k"] = "v"
340+
341+
assertEquals("v", breadcrumb.getData("k"))
342+
}
343+
344+
@Test
345+
fun `concurrent first writes keep all data entries`() {
346+
val breadcrumb = Breadcrumb()
347+
val count = 32
348+
val executor = Executors.newFixedThreadPool(count)
349+
val start = CountDownLatch(1)
350+
val futures =
351+
(0 until count).map { index ->
352+
executor.submit {
353+
start.await()
354+
breadcrumb.setData("key-$index", index)
355+
}
356+
}
357+
358+
start.countDown()
359+
futures.forEach { it.get(5, TimeUnit.SECONDS) }
360+
executor.shutdown()
361+
362+
assertEquals(count, breadcrumb.data.size)
363+
for (index in 0 until count) {
364+
assertEquals(index, breadcrumb.data["key-$index"])
365+
}
366+
}
367+
332368
class TestKey(val id: Long) {
333369
override fun toString(): String = id.toString()
334370
}

sentry/src/test/java/io/sentry/DateUtilsTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ class DateUtilsTest {
8686
val utcActual = convertDate(actual)
8787
val timestamp = utcActual.format(isoFormat)
8888

89+
assertEquals(millis, actual.time)
8990
assertEquals("2020-06-07T12:38:12.631Z", timestamp)
9091
}
9192

0 commit comments

Comments
 (0)