[folks/perf2: 5/6] edsf: Avoid Gee.LinkedList in copy_contacts(_ids)




commit b214404fd77e474996feca69e25164ae1dc14c18
Author: Niels De Graef <nielsdegraef gmail com>
Date:   Sat Oct 17 14:43:18 2020 +0200

    edsf: Avoid Gee.LinkedList in copy_contacts(_ids)
    
    When we receive a signal that adds/removes/changes contacts, we get a
    list of the affectefd contacts (or their ID's). Since we process them at
    a separate point in time in the main loop, we have to make a copy to
    make sure that they're still around when being processed.
    
    The current way we do this, is by creating a `Gee.LinkedList`, and
    appending each entry at the end (note that appending an item is an O(1)
    operation in `Gee.LinkedList` since it keeps track of its last element).
    However, there's some problems with this approach:
    
    * Each time an element is added into the list, a little helper object (a
      `Node`) is allocated. This is yet again just another unnecessary
      allocation that we wish to avoid.
    * Iterating over any `Gee.Collection` gives you an owned reference to
      each element, meaning we do unnecessary copies or reference count
      fiddling.
    
    Most of these issues can be solved quite elegantly by using
    `GLib.GenericArray` (ie. `GPtrArray`), which will keep track of the
    owned references, but won't try to do anything fancy with it.

 backends/eds/lib/edsf-persona-store.vala | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)
---
diff --git a/backends/eds/lib/edsf-persona-store.vala b/backends/eds/lib/edsf-persona-store.vala
index 2eb99e67..4cac5620 100644
--- a/backends/eds/lib/edsf-persona-store.vala
+++ b/backends/eds/lib/edsf-persona-store.vala
@@ -2439,19 +2439,19 @@ public class Edsf.PersonaStore : Folks.PersonaStore
         }
     }
 
-  private Gee.LinkedList<E.Contact> _copy_contacts (GLib.List<E.Contact> contacts)
+  private GenericArray<E.Contact> _copy_contacts (GLib.List<E.Contact> contacts)
     {
-      var copy = new Gee.LinkedList<E.Contact> ();
-      foreach (E.Contact c in contacts)
+      var copy = new GenericArray<E.Contact> (contacts.length());
+      foreach (unowned E.Contact c in contacts)
         {
           copy.add (c);
         }
       return copy;
     }
 
-  private Gee.LinkedList<string> _copy_contacts_ids (GLib.List<string> contacts_ids)
+  private GenericArray<string> _copy_contacts_ids (GLib.List<string> contacts_ids)
     {
-      var copy = new Gee.LinkedList<string> ();
+      var copy = new GenericArray<string> (contacts_ids.length());
       foreach (unowned string s in contacts_ids)
         {
           copy.add (s);
@@ -2465,7 +2465,7 @@ public class Edsf.PersonaStore : Folks.PersonaStore
       this._idle_queue (() => { return this._contacts_added_idle (copy); });
     }
 
-  private bool _contacts_added_idle (Gee.List<E.Contact> contacts)
+  private bool _contacts_added_idle (GenericArray<E.Contact> contacts)
     {
       HashSet<Persona> added_personas, removed_personas;
 
@@ -2489,8 +2489,9 @@ public class Edsf.PersonaStore : Folks.PersonaStore
 
       removed_personas = new HashSet<Persona> ();
 
-      foreach (E.Contact c in contacts)
+      for (uint i = 0; i < contacts.length; i++)
         {
+          unowned E.Contact c = contacts[i];
           string? _iid = Edsf.Persona.build_iid_from_contact (this.id, c);
 
           if (_iid == null)
@@ -2538,10 +2539,11 @@ public class Edsf.PersonaStore : Folks.PersonaStore
       this._idle_queue (() => { return this._contacts_changed_idle (copy); });
     }
 
-  private bool _contacts_changed_idle (Gee.List<E.Contact> contacts)
+  private bool _contacts_changed_idle (GenericArray<E.Contact> contacts)
     {
-      foreach (E.Contact c in contacts)
+      for (uint i = 0; i < contacts.length; i++)
         {
+          unowned E.Contact c = contacts[i];
           string? _iid = Edsf.Persona.build_iid_from_contact (this.id, c);
 
           if (_iid == null)
@@ -2566,12 +2568,13 @@ public class Edsf.PersonaStore : Folks.PersonaStore
       this._idle_queue (() => { return this._contacts_removed_idle (copy); });
     }
 
-  private bool _contacts_removed_idle (Gee.List<string> contacts_ids)
+  private bool _contacts_removed_idle (GenericArray<string> contacts_ids)
     {
       var removed_personas = new HashSet<Persona> ();
 
-      foreach (string contact_id in contacts_ids)
+      for (uint i = 0; i < contacts_ids.length; i++)
         {
+          unowned string contact_id = contacts_ids[i];
           /* Not sure how this could happen, but better to be safe. We do not
            * allow empty UIDs. */
           if (contact_id == "")


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