[gnome-builder/gnome-builder-43] plugins/podman: Cache contains_program_in_path() results



commit bba5e5bf8c3b984b46dabc7c01b7a05a275a2666
Author: Ondřej Míchal <harrymichal seznam cz>
Date:   Tue Sep 20 20:29:30 2022 +0200

    plugins/podman: Cache contains_program_in_path() results
    
    Without this the synchronous check causes Builder to stutter because
    'podman exec' adds almost 100ms of an overhead execution time to the
    executed command.
    
    Based on d907158e & 6513765c
    
    Fixes https://gitlab.gnome.org/GNOME/gnome-builder/-/issues/1794

 src/plugins/podman/gbp-podman-runtime.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
---
diff --git a/src/plugins/podman/gbp-podman-runtime.c b/src/plugins/podman/gbp-podman-runtime.c
index b64b63e47..240213e13 100644
--- a/src/plugins/podman/gbp-podman-runtime.c
+++ b/src/plugins/podman/gbp-podman-runtime.c
@@ -31,6 +31,9 @@
 struct _GbpPodmanRuntime
 {
   IdeRuntime  parent_instance;
+
+  GHashTable *program_paths_cache;
+
   JsonObject *object;
   gchar      *id;
   GMutex      mutex;
@@ -171,13 +174,18 @@ gbp_podman_runtime_contains_program_in_path (IdeRuntime   *runtime,
 {
   g_autoptr(IdeRunContext) run_context = NULL;
   g_autoptr(IdeSubprocess) subprocess = NULL;
+  GbpPodmanRuntime *self = (GbpPodmanRuntime *) runtime;
   gboolean ret;
+  gpointer val = NULL;
 
   IDE_ENTRY;
 
   g_assert (GBP_IS_PODMAN_RUNTIME (runtime));
   g_assert (program != NULL);
 
+  if (g_hash_table_lookup_extended (self->program_paths_cache, program, NULL, &val))
+    return GPOINTER_TO_UINT (val);
+
   run_context = ide_run_context_new ();
 
   gbp_podman_runtime_prepare_run_context (runtime, NULL, run_context);
@@ -194,6 +202,11 @@ gbp_podman_runtime_contains_program_in_path (IdeRuntime   *runtime,
 
   ret = ide_subprocess_wait_check (subprocess, cancellable, NULL);
 
+  /* Cache both positive and negative lookups */
+  g_hash_table_insert (self->program_paths_cache,
+                       (char *) g_intern_string (program),
+                       GUINT_TO_POINTER (ret));
+
   IDE_RETURN (ret);
 }
 
@@ -552,6 +565,7 @@ gbp_podman_runtime_finalize (GObject *object)
   g_clear_pointer (&self->id, g_free);
   g_mutex_clear (&self->mutex);
   g_clear_list (&self->layers, g_free);
+  g_clear_pointer (&self->program_paths_cache, g_hash_table_unref);
 
   G_OBJECT_CLASS (gbp_podman_runtime_parent_class)->finalize (object);
 }
@@ -577,6 +591,7 @@ static void
 gbp_podman_runtime_init (GbpPodmanRuntime *self)
 {
   g_mutex_init (&self->mutex);
+  self->program_paths_cache = g_hash_table_new (g_str_hash, g_str_equal);
 }
 
 GbpPodmanRuntime *


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