[glib/wip/antoniof/fallback-timezone-cache-lookup: 2/3] 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: 2/3] gtimezone: Cache default timezone indefinitely
- Date: Thu, 1 Oct 2020 23:03:54 +0000 (UTC)
commit 5237b4984306847dff05db54b5c12c83662f2f1d
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 | 37 ++++++++++++++++++++++++++++++++-----
1 file changed, 32 insertions(+), 5 deletions(-)
---
diff --git a/glib/gtimezone.c b/glib/gtimezone.c
index 40064c9ab..035da2f45 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;
@@ -1675,12 +1677,12 @@ g_time_zone_new (const gchar *identifier)
gint rules_num;
gchar *resolved_identifier = NULL;
- G_LOCK (time_zones);
- if (time_zones == NULL)
- time_zones = g_hash_table_new (g_str_hash, g_str_equal);
-
if (identifier)
{
+ G_LOCK (time_zones);
+ if (time_zones == NULL)
+ time_zones = g_hash_table_new (g_str_hash, g_str_equal);
+
tz = g_hash_table_lookup (time_zones, identifier);
if (tz)
{
@@ -1693,11 +1695,26 @@ g_time_zone_new (const gchar *identifier)
}
else
{
+ G_LOCK (tz_default);
#ifdef G_OS_UNIX
resolved_identifier = zone_identifier_unix ();
#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 (tz_default);
+
+ g_free (resolved_identifier);
+ return tz;
+ }
+ }
}
tz = g_slice_new0 (GTimeZone);
@@ -1773,9 +1790,19 @@ g_time_zone_new (const gchar *identifier)
{
if (identifier)
g_hash_table_insert (time_zones, tz->name, tz);
+ else if (tz->name)
+ {
+ /* Caching reference */
+ g_atomic_int_inc (&tz->ref_count);
+ tz_default = tz;
+ }
}
g_atomic_int_inc (&tz->ref_count);
- G_UNLOCK (time_zones);
+
+ if (identifier)
+ G_UNLOCK (time_zones);
+ else
+ G_UNLOCK (tz_default);
return tz;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]