[gnome-shell/wip/chergert/3-28-perf-fixes] shell-util: add API to reduce GTimeZone usage



commit 1ab890dfc174acfe67ea7dbde137dd597ee312f0
Author: Christian Hergert <chergert redhat com>
Date:   Thu Feb 27 00:07:26 2020 -0800

    shell-util: add API to reduce GTimeZone usage
    
    Creating new GTimeZone with g_time_zone_new_local() requires opening
    /etc/localtime, mmap()'ing it, and parsing information from it. This is
    quite a bit of work to do from the compositor thread for the amount we
    do it.
    
    Instead, we can cache that value and provide a simplified form to access
    it.

 js/ui/environment.js |  2 +-
 src/shell-util.c     | 26 ++++++++++++++++++++++++++
 src/shell-util.h     |  1 +
 3 files changed, 28 insertions(+), 1 deletion(-)
---
diff --git a/js/ui/environment.js b/js/ui/environment.js
index 719680f615..a2a15a5991 100644
--- a/js/ui/environment.js
+++ b/js/ui/environment.js
@@ -118,7 +118,7 @@ function init() {
 
     // Work around https://bugzilla.mozilla.org/show_bug.cgi?id=508783
     Date.prototype.toLocaleFormat = function(format) {
-        return Shell.util_format_date(format, this.getTime());
+        return Shell.util_format_now(format);
     };
 
     let slowdownEnv = GLib.getenv('GNOME_SHELL_SLOWDOWN_FACTOR');
diff --git a/src/shell-util.c b/src/shell-util.c
index 70b8c06110..46b81da66b 100644
--- a/src/shell-util.c
+++ b/src/shell-util.c
@@ -20,6 +20,8 @@
 #include <langinfo.h>
 #endif
 
+static GTimeZone *local_tz;
+
 static void
 stop_pick (ClutterActor       *actor,
            const ClutterColor *color)
@@ -154,6 +156,24 @@ shell_util_format_date (const char *format,
   return result;
 }
 
+char *
+shell_util_format_now (const char *format)
+{
+  GDateTime *datetime;
+  char *ret;
+
+  if (local_tz == NULL)
+    local_tz = g_time_zone_new_local ();
+
+  datetime = g_date_time_new_now (local_tz);
+  if (!datetime)
+    return g_strdup ("");
+
+  ret = g_date_time_format (datetime, format);
+  g_date_time_unref (datetime);
+  return ret;
+}
+
 /**
  * shell_util_get_week_start:
  *
@@ -514,3 +534,9 @@ shell_util_composite_capture_images (ClutterCapture  *captures,
 
   return image;
 }
+
+void
+shell_util_clear_timezone_cache (void)
+{
+  g_clear_pointer (&local_tz, g_time_zone_unref);
+}
diff --git a/src/shell-util.h b/src/shell-util.h
index 2218594c19..e925e3ae44 100644
--- a/src/shell-util.h
+++ b/src/shell-util.h
@@ -22,6 +22,7 @@ int      shell_util_get_week_start             (void);
 
 char    *shell_util_format_date                (const char       *format,
                                                 gint64            time_ms);
+char    *shell_util_format_now                 (const char       *format);
 const char *shell_util_translate_time_string   (const char *str);
 
 char    *shell_util_regex_escape               (const char *str);


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