Skip to content

Commit 4ec0df2

Browse files
authored
Merge pull request #5589 from getsentry/perf/sdk-overhead-reduction-date-utils
perf(core): [SDK Overhead Reduction 5] Replace Calendar with Date in DateUtils
2 parents cd17726 + 0054a67 commit 4ec0df2

4 files changed

Lines changed: 7 additions & 10 deletions

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 timestamp helper overhead by replacing unnecessary `Calendar` usage in `DateUtils` with direct `Date` creation. ([#5589](https://github.com/getsentry/sentry-java/pull/5589))
78
- 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))
89

910
## 8.45.0

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ public Breadcrumb(@Nullable String message) {
555555
if (timestamp != null) {
556556
return (Date) timestamp.clone();
557557
} else if (timestampMs != null) {
558-
// we memoize it here into timestamp to avoid instantiating Calendar again and again
558+
// we memoize it here into timestamp to avoid creating a Date again and again
559559
timestamp = DateUtils.getDateTime(timestampMs);
560560
return timestamp;
561561
}

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/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)