core/range: Compute and use timestamps for comparison
Using GDateTime utilities is costly and shows up high in profiling. That's because GDateTime has to run timezone offset checks for every comparison. We can amortize this cost by converting these datetimes to unix timestamps upfront, and using them for comparisons. A complication is date-only ranges, for which unix timestamps wouldn't work. For this, add a new kind of timestamp that only accounts for the dates. Add a new test case for date-only comparison. Part-of: <https://gitlab.gnome.org/GNOME/gnome-calendar/-/merge_requests/770>
This commit is contained in:
@@ -231,6 +231,31 @@ range_calculate_overlap (void)
|
||||
|
||||
/*********************************************************************************************************************/
|
||||
|
||||
static void
|
||||
range_calculate_overlap_date_only (void)
|
||||
{
|
||||
g_autoptr (GcalRange) range_a = NULL;
|
||||
g_autoptr (GcalRange) range_b = NULL;
|
||||
GcalRangePosition position;
|
||||
GcalRangeOverlap overlap;
|
||||
|
||||
range_a = gcal_range_new_take (g_date_time_new_utc (1921, 12, 31, 0, 0, 0),
|
||||
g_date_time_new_utc (1922, 1, 1, 0, 0, 0),
|
||||
GCAL_RANGE_DATE_ONLY);
|
||||
g_assert_nonnull (range_a);
|
||||
|
||||
range_b = gcal_range_new_take (g_date_time_new_utc (2121, 10, 31, 0, 0, 0),
|
||||
g_date_time_new_utc (2121, 11, 1, 0, 0, 0),
|
||||
GCAL_RANGE_DATE_ONLY);
|
||||
g_assert_nonnull (range_b);
|
||||
|
||||
overlap = gcal_range_calculate_overlap (range_a, range_b, &position);
|
||||
g_assert_cmpint (overlap, ==, GCAL_RANGE_NO_OVERLAP);
|
||||
g_assert_cmpint (position, ==, GCAL_RANGE_BEFORE);
|
||||
}
|
||||
|
||||
/*********************************************************************************************************************/
|
||||
|
||||
gint
|
||||
main (gint argc,
|
||||
gchar *argv[])
|
||||
@@ -242,6 +267,7 @@ main (gint argc,
|
||||
g_test_add_func ("/range/new", range_new);
|
||||
g_test_add_func ("/range/invalid-ranges", range_invalid);
|
||||
g_test_add_func ("/range/calculate-overlap", range_calculate_overlap);
|
||||
g_test_add_func ("/range/calculate-overlap-date-only", range_calculate_overlap_date_only);
|
||||
|
||||
return g_test_run ();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user