From 2b933d6324e0f72d8b2e9870ed644dfba4e91751 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Tue, 23 Jun 2026 12:14:16 +0200 Subject: [PATCH 1/2] perf(core): Avoid cloning Date getters --- sentry/src/main/java/io/sentry/Breadcrumb.java | 2 +- sentry/src/main/java/io/sentry/SentryEvent.java | 2 +- sentry/src/main/java/io/sentry/Session.java | 8 ++------ sentry/src/main/java/io/sentry/protocol/App.java | 3 +-- sentry/src/main/java/io/sentry/protocol/Device.java | 3 +-- sentry/src/test/java/io/sentry/protocol/AppTest.kt | 5 +++-- sentry/src/test/java/io/sentry/protocol/DeviceTest.kt | 5 +++-- 7 files changed, 12 insertions(+), 16 deletions(-) diff --git a/sentry/src/main/java/io/sentry/Breadcrumb.java b/sentry/src/main/java/io/sentry/Breadcrumb.java index d122d1459bf..a7f4ab7b5b9 100644 --- a/sentry/src/main/java/io/sentry/Breadcrumb.java +++ b/sentry/src/main/java/io/sentry/Breadcrumb.java @@ -553,7 +553,7 @@ public Breadcrumb(@Nullable String message) { @SuppressWarnings("JavaUtilDate") public @NotNull Date getTimestamp() { if (timestamp != null) { - return (Date) timestamp.clone(); + return timestamp; } else if (timestampMs != null) { // we memoize it here into timestamp to avoid instantiating Calendar again and again timestamp = DateUtils.getDateTime(timestampMs); diff --git a/sentry/src/main/java/io/sentry/SentryEvent.java b/sentry/src/main/java/io/sentry/SentryEvent.java index 007d50681fb..8b8575fe7ec 100644 --- a/sentry/src/main/java/io/sentry/SentryEvent.java +++ b/sentry/src/main/java/io/sentry/SentryEvent.java @@ -114,7 +114,7 @@ public SentryEvent(final @NotNull Date timestamp) { @SuppressWarnings({"JdkObsolete", "JavaUtilDate"}) public Date getTimestamp() { - return (Date) timestamp.clone(); + return timestamp; } public void setTimestamp(final @NotNull Date timestamp) { diff --git a/sentry/src/main/java/io/sentry/Session.java b/sentry/src/main/java/io/sentry/Session.java index 3ce2d70e89e..2fdfffb35d9 100644 --- a/sentry/src/main/java/io/sentry/Session.java +++ b/sentry/src/main/java/io/sentry/Session.java @@ -131,10 +131,7 @@ public boolean isTerminated() { @SuppressWarnings({"JdkObsolete", "JavaUtilDate"}) public @Nullable Date getStarted() { - if (started == null) { - return null; - } - return (Date) started.clone(); + return started; } public @Nullable String getDistinctId() { @@ -193,8 +190,7 @@ public int errorCount() { @SuppressWarnings({"JdkObsolete", "JavaUtilDate"}) public @Nullable Date getTimestamp() { - final Date timestampRef = timestamp; - return timestampRef != null ? (Date) timestampRef.clone() : null; + return timestamp; } /** Ends a session and update its values */ diff --git a/sentry/src/main/java/io/sentry/protocol/App.java b/sentry/src/main/java/io/sentry/protocol/App.java index 989c3464be8..878ad0ec960 100644 --- a/sentry/src/main/java/io/sentry/protocol/App.java +++ b/sentry/src/main/java/io/sentry/protocol/App.java @@ -98,8 +98,7 @@ public void setAppIdentifier(final @Nullable String appIdentifier) { @SuppressWarnings({"JdkObsolete", "JavaUtilDate"}) public @Nullable Date getAppStartTime() { - final Date appStartTimeRef = appStartTime; - return appStartTimeRef != null ? (Date) appStartTimeRef.clone() : null; + return appStartTime; } public void setAppStartTime(final @Nullable Date appStartTime) { diff --git a/sentry/src/main/java/io/sentry/protocol/Device.java b/sentry/src/main/java/io/sentry/protocol/Device.java index e6113efbcb5..5b765640a39 100644 --- a/sentry/src/main/java/io/sentry/protocol/Device.java +++ b/sentry/src/main/java/io/sentry/protocol/Device.java @@ -366,8 +366,7 @@ public void setScreenDpi(final @Nullable Integer screenDpi) { @SuppressWarnings({"JdkObsolete", "JavaUtilDate"}) public @Nullable Date getBootTime() { - final Date bootTimeRef = bootTime; - return bootTimeRef != null ? (Date) bootTimeRef.clone() : null; + return bootTime; } public void setBootTime(final @Nullable Date bootTime) { diff --git a/sentry/src/test/java/io/sentry/protocol/AppTest.kt b/sentry/src/test/java/io/sentry/protocol/AppTest.kt index 84b4c7088e3..cc0f504b7c8 100644 --- a/sentry/src/test/java/io/sentry/protocol/AppTest.kt +++ b/sentry/src/test/java/io/sentry/protocol/AppTest.kt @@ -5,10 +5,11 @@ import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertNotNull import kotlin.test.assertNotSame +import kotlin.test.assertSame class AppTest { @Test - fun `copying app wont have the same references`() { + fun `copying app keeps date reference and copies collections`() { val app = App() app.appBuild = "app build" app.appIdentifier = "app identifier" @@ -28,7 +29,7 @@ class AppTest { assertNotNull(clone) assertNotSame(app, clone) - assertNotSame(app.appStartTime, clone.appStartTime) + assertSame(app.appStartTime, clone.appStartTime) assertNotSame(app.permissions, clone.permissions) assertNotSame(app.viewNames, clone.viewNames) diff --git a/sentry/src/test/java/io/sentry/protocol/DeviceTest.kt b/sentry/src/test/java/io/sentry/protocol/DeviceTest.kt index 121cbe6537f..a67305c37ea 100644 --- a/sentry/src/test/java/io/sentry/protocol/DeviceTest.kt +++ b/sentry/src/test/java/io/sentry/protocol/DeviceTest.kt @@ -6,11 +6,12 @@ import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertNotNull import kotlin.test.assertNotSame +import kotlin.test.assertSame class DeviceTest { @Test - fun `copying device wont have the same references`() { + fun `copying device keeps date reference and copies other mutable references`() { val device = Device() device.archs = arrayOf("archs1", "archs2") device.bootTime = Date() @@ -23,7 +24,7 @@ class DeviceTest { assertNotNull(clone) assertNotSame(device, clone) assertNotSame(device.archs, clone.archs) - assertNotSame(device.bootTime, clone.bootTime) + assertSame(device.bootTime, clone.bootTime) assertNotSame(device.timezone, clone.timezone) assertNotSame(device.unknown, clone.unknown) } From 321d84d967acfc0f0aa99e0af41ec39fa4a16763 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Wed, 24 Jun 2026 17:25:01 +0200 Subject: [PATCH 2/2] docs(core): Add Date getter changelog entries --- CHANGELOG.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d6a3b55b403..9422885bb8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,17 @@ # Changelog +## Unreleased + +### Behavioral Changes + +- `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)) + - Previously, these getters returned defensive copies for some date fields. + - This has now changed in order to reduce SDK overhead. + +### Internal + +- Reduce model access overhead by avoiding defensive `Date` copies in SDK data model getters. ([#5603](https://github.com/getsentry/sentry-java/pull/5603)) + ## 8.43.1 ### Fixes