Skip to content

Commit 7ae0823

Browse files
authored
Merge pull request #5603 from getsentry/perf/sdk-overhead-reduction-date-getters
perf(core): [SDK Overhead Reduction 12] Avoid cloning Date getters
2 parents 7399716 + 78e8755 commit 7ae0823

8 files changed

Lines changed: 19 additions & 16 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,15 @@
22

33
## Unreleased
44

5+
### Behavioral Changes
6+
7+
- `Date` objects returned by SDK data model getters are shared state and should not be mutated. ([#5603](https://github.com/getsentry/sentry-java/pull/5603))
8+
- Previously, these getters returned defensive copies for some date fields.
9+
- This has now changed in order to reduce SDK overhead.
10+
511
### Internal
612

13+
- Reduce model access overhead by avoiding defensive `Date` copies in SDK data model getters. ([#5603](https://github.com/getsentry/sentry-java/pull/5603))
714
- Reduce timestamp parsing and formatting overhead with Sentry-specific ISO-8601 handling. ([#5602](https://github.com/getsentry/sentry-java/pull/5602))
815
- Reduce JSON serialization overhead by creating the reflection serializer only when unknown-object fallback serialization is needed. ([#5601](https://github.com/getsentry/sentry-java/pull/5601))
916
- 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))

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ public Breadcrumb(@Nullable String message) {
562562
@SuppressWarnings("JavaUtilDate")
563563
public @NotNull Date getTimestamp() {
564564
if (timestamp != null) {
565-
return (Date) timestamp.clone();
565+
return timestamp;
566566
} else if (timestampMs != null) {
567567
// we memoize it here into timestamp to avoid creating a Date again and again
568568
timestamp = DateUtils.getDateTime(timestampMs);

sentry/src/main/java/io/sentry/SentryEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public SentryEvent(final @NotNull Date timestamp) {
114114

115115
@SuppressWarnings({"JdkObsolete", "JavaUtilDate"})
116116
public Date getTimestamp() {
117-
return (Date) timestamp.clone();
117+
return timestamp;
118118
}
119119

120120
public void setTimestamp(final @NotNull Date timestamp) {

sentry/src/main/java/io/sentry/Session.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,7 @@ public boolean isTerminated() {
131131

132132
@SuppressWarnings({"JdkObsolete", "JavaUtilDate"})
133133
public @Nullable Date getStarted() {
134-
if (started == null) {
135-
return null;
136-
}
137-
return (Date) started.clone();
134+
return started;
138135
}
139136

140137
public @Nullable String getDistinctId() {
@@ -193,8 +190,7 @@ public int errorCount() {
193190

194191
@SuppressWarnings({"JdkObsolete", "JavaUtilDate"})
195192
public @Nullable Date getTimestamp() {
196-
final Date timestampRef = timestamp;
197-
return timestampRef != null ? (Date) timestampRef.clone() : null;
193+
return timestamp;
198194
}
199195

200196
/** Ends a session and update its values */

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ public void setAppIdentifier(final @Nullable String appIdentifier) {
9898

9999
@SuppressWarnings({"JdkObsolete", "JavaUtilDate"})
100100
public @Nullable Date getAppStartTime() {
101-
final Date appStartTimeRef = appStartTime;
102-
return appStartTimeRef != null ? (Date) appStartTimeRef.clone() : null;
101+
return appStartTime;
103102
}
104103

105104
public void setAppStartTime(final @Nullable Date appStartTime) {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,7 @@ public void setScreenDpi(final @Nullable Integer screenDpi) {
366366

367367
@SuppressWarnings({"JdkObsolete", "JavaUtilDate"})
368368
public @Nullable Date getBootTime() {
369-
final Date bootTimeRef = bootTime;
370-
return bootTimeRef != null ? (Date) bootTimeRef.clone() : null;
369+
return bootTime;
371370
}
372371

373372
public void setBootTime(final @Nullable Date bootTime) {

sentry/src/test/java/io/sentry/protocol/AppTest.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import kotlin.test.Test
55
import kotlin.test.assertEquals
66
import kotlin.test.assertNotNull
77
import kotlin.test.assertNotSame
8+
import kotlin.test.assertSame
89

910
class AppTest {
1011
@Test
11-
fun `copying app wont have the same references`() {
12+
fun `copying app keeps date reference and copies collections`() {
1213
val app = App()
1314
app.appBuild = "app build"
1415
app.appIdentifier = "app identifier"
@@ -28,7 +29,7 @@ class AppTest {
2829

2930
assertNotNull(clone)
3031
assertNotSame(app, clone)
31-
assertNotSame(app.appStartTime, clone.appStartTime)
32+
assertSame(app.appStartTime, clone.appStartTime)
3233
assertNotSame(app.permissions, clone.permissions)
3334
assertNotSame(app.viewNames, clone.viewNames)
3435

sentry/src/test/java/io/sentry/protocol/DeviceTest.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import kotlin.test.Test
66
import kotlin.test.assertEquals
77
import kotlin.test.assertNotNull
88
import kotlin.test.assertNotSame
9+
import kotlin.test.assertSame
910

1011
class DeviceTest {
1112

1213
@Test
13-
fun `copying device wont have the same references`() {
14+
fun `copying device keeps date reference and copies other mutable references`() {
1415
val device = Device()
1516
device.archs = arrayOf("archs1", "archs2")
1617
device.bootTime = Date()
@@ -23,7 +24,7 @@ class DeviceTest {
2324
assertNotNull(clone)
2425
assertNotSame(device, clone)
2526
assertNotSame(device.archs, clone.archs)
26-
assertNotSame(device.bootTime, clone.bootTime)
27+
assertSame(device.bootTime, clone.bootTime)
2728
assertNotSame(device.timezone, clone.timezone)
2829
assertNotSame(device.unknown, clone.unknown)
2930
}

0 commit comments

Comments
 (0)