[glib/wip/antoniof/fallback-timezone-cache-lookup: 5/6] gtimezone: Cache default timezone indefinitely
- From: António Fernandes <antoniof src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib/wip/antoniof/fallback-timezone-cache-lookup: 5/6] gtimezone: Cache default timezone indefinitely
- Date: Thu, 1 Oct 2020 20:46:57 +0000 (UTC)
commit af8b909df7391d209dabb3f1088eb06f340a64eb
Author: António Fernandes <antoniof gnome org>
Date: Thu Oct 1 21:11:44 2020 +0100
gtimezone: Cache default timezone indefinitely
We cache GTimeZone instances to avoid expensive construction when the
same id is requested again.
However, if the NULL id is passed to g_time_zone_new(), we always
construct a new instance for the default/fallback timezone.
With the recent introduction of some heavy calculations[1], repeated
instance construction in such cases has visible performance impact in
nautilus list view and other such GtkTreeView consumers.
To avoid this, cache reference to a constructed default timezone and
use it the next time g_time_zone_new() is called with NULL argument,
as long as the default identifier doesn't change. We already did the
same for the local timezone[2].
Fixes: https://gitlab.gnome.org/GNOME/glib/-/issues/2204
Based on idea proposed by Sebastian Keller <skeller gnome org>.
[1] 25d950b61f92f25cc9ab20d683aa4d6969f93098
[2] 551e83662de9815d161a82c760cfa77995905740
glib/gtimezone.c | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)
---
diff --git a/glib/gtimezone.c b/glib/gtimezone.c
index da9c3fe65..daf5c1027 100644
--- a/glib/gtimezone.c
+++ b/glib/gtimezone.c
@@ -195,6 +195,8 @@ struct _GTimeZone
G_LOCK_DEFINE_STATIC (time_zones);
static GHashTable/*<string?, GTimeZone>*/ *time_zones;
+G_LOCK_DEFINE_STATIC (tz_default);
+static GTimeZone *tz_default = NULL;
G_LOCK_DEFINE_STATIC (tz_local);
static gchar *tzenv_cached = NULL;
static GTimeZone *tz_local = NULL;
@@ -1676,6 +1678,7 @@ g_time_zone_new (const gchar *identifier)
gchar *resolved_identifier = NULL;
G_LOCK (time_zones);
+ G_LOCK (tz_default);
if (time_zones == NULL)
time_zones = g_hash_table_new (g_str_hash, g_str_equal);
@@ -1686,6 +1689,7 @@ g_time_zone_new (const gchar *identifier)
{
g_atomic_int_inc (&tz->ref_count);
G_UNLOCK (time_zones);
+ G_UNLOCK (tz_default);
return tz;
}
else
@@ -1698,6 +1702,19 @@ g_time_zone_new (const gchar *identifier)
#elif defined (G_OS_WIN32)
resolved_identifier = windows_default_tzname ();
#endif
+ if (tz_default)
+ /* Flush default if changed */
+ if (g_strcmp0 (tz_default->name, resolved_identifier))
+ g_clear_pointer (&tz_default, g_time_zone_unref);
+ else
+ {
+ tz = g_time_zone_ref (tz_default);
+ G_UNLOCK (time_zones);
+ G_UNLOCK (tz_default);
+
+ g_free (resolved_identifier);
+ return tz;
+ }
}
tz = g_slice_new0 (GTimeZone);
@@ -1773,9 +1790,16 @@ g_time_zone_new (const gchar *identifier)
{
if (identifier)
g_hash_table_insert (time_zones, tz->name, tz);
+ else if (tz->name)
+ {
+ /* Cache a reference */
+ g_atomic_int_inc (&tz->ref_count);
+ tz_default = tz;
+ }
}
g_atomic_int_inc (&tz->ref_count);
G_UNLOCK (time_zones);
+ G_UNLOCK (tz_default);
return tz;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]