[glib/wip/antoniof/fallback-timezone-cache-lookup: 1/2] gtimezone: Also cache fallback timezones




commit 7d1a74d8c42bd61d669ad7a4b39abbc31c3c6540
Author: António Fernandes <antoniof gnome org>
Date:   Wed Sep 23 19:03:03 2020 +0100

    gtimezone: Also cache fallback timezones
    
    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 id, instead
    of returning a reference to an existing instance for the same id.
    
    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.
    
    Now that we get the resolved id earlier, we can extend the cache to
    include the default/fallback case.
    
    Fixes: https://gitlab.gnome.org/GNOME/glib/-/issues/2204
    
    Based on idea proposed by Sebastian Keller <skeller gnome org>
    
    [1] 25d950b61f92f25cc9ab20d683aa4d6969f93098

 glib/gtimezone.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
---
diff --git a/glib/gtimezone.c b/glib/gtimezone.c
index bc6c0dc91..55c71df0f 100644
--- a/glib/gtimezone.c
+++ b/glib/gtimezone.c
@@ -1670,9 +1670,9 @@ g_time_zone_new (const gchar *identifier)
 #endif
     }
 
-  if (identifier)
+  if (resolved_identifier)
     {
-      tz = g_hash_table_lookup (time_zones, identifier);
+      tz = g_hash_table_lookup (time_zones, resolved_identifier);
       if (tz)
         {
           g_atomic_int_inc (&tz->ref_count);
@@ -1740,8 +1740,6 @@ g_time_zone_new (const gchar *identifier)
     }
 #endif
 
-  g_free (resolved_identifier);
-
   /* Always fall back to UTC. */
   if (tz->t_info == NULL)
     zone_for_constant_offset (tz, "UTC");
@@ -1751,12 +1749,14 @@ g_time_zone_new (const gchar *identifier)
 
   if (tz->t_info != NULL)
     {
-      if (identifier)
+      if (resolved_identifier)
         g_hash_table_insert (time_zones, tz->name, tz);
     }
   g_atomic_int_inc (&tz->ref_count);
   G_UNLOCK (time_zones);
 
+  g_free (resolved_identifier);
+
   return tz;
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]