[folks] Maximize use of 'unowned' keyword.



commit 7668bd500b5e6739da2b1e583df5265b0ba2b399
Author: Travis Reitter <travis reitter collabora co uk>
Date:   Tue Dec 28 09:15:23 2010 -0800

    Maximize use of 'unowned' keyword.
    
    This is used when (and only when) variables:
    1. would avoid a non-trivial copy (eg, a string or array but not an int or
    object ref-count increase)
    AND
    2. will only be assigned unowned values (excessive use of 'unowned' here will be
    caught by the compiler)
    
    The net benefit is avoiding unecessary memory allocation/freeing.
    
    Helps bgo#629083

 HACKING                                       |    9 +++++++++
 backends/key-file/kf-backend.vala             |    2 +-
 backends/key-file/kf-persona.vala             |    4 ++--
 backends/telepathy/lib/tpf-persona-store.vala |    9 +++++----
 backends/telepathy/lib/tpf-persona.vala       |    4 ++--
 folks/backend-store.vala                      |    5 +++--
 folks/individual-aggregator.vala              |    8 ++++----
 folks/individual.vala                         |    8 ++++----
 8 files changed, 30 insertions(+), 19 deletions(-)
---
diff --git a/HACKING b/HACKING
index b373c84..be18fdc 100644
--- a/HACKING
+++ b/HACKING
@@ -88,3 +88,12 @@ Vala-specific rules
 
    Rarely, the use of 'var' can obscure the effective type of the variable. In
    this case, it's acceptable to provide an explicit type.
+
+8. Use the 'unowned' modifier when it would prevent a non-trivial amount of
+   memory allocation. This is most commonly true for strings, arrays, and
+   non-reference-counted variables.
+
+   Do not use 'unowned' for reference-counted variables (like objects) since it
+   reduces readability without benefit. And, as of this writing, bgo#638199
+   forces unowned variables to have an explicit type (preventing the use of
+   'var').
diff --git a/backends/key-file/kf-backend.vala b/backends/key-file/kf-backend.vala
index d5edb92..a2e720d 100644
--- a/backends/key-file/kf-backend.vala
+++ b/backends/key-file/kf-backend.vala
@@ -78,7 +78,7 @@ public class Folks.Backends.Kf.Backend : Folks.Backend
           if (!this._is_prepared)
             {
               File file;
-              var path = Environment.get_variable (
+              unowned string path = Environment.get_variable (
                   "FOLKS_BACKEND_KEY_FILE_PATH");
               if (path == null)
                 {
diff --git a/backends/key-file/kf-persona.vala b/backends/key-file/kf-persona.vala
index b69a5ab..cc6ecf6 100644
--- a/backends/key-file/kf-persona.vala
+++ b/backends/key-file/kf-persona.vala
@@ -177,7 +177,7 @@ public class Folks.Backends.Kf.Persona : Folks.Persona,
       try
         {
           var keys = this._key_file.get_keys (this.display_id);
-          foreach (var key in keys)
+          foreach (unowned string key in keys)
             {
               /* Alias */
               if (key == "__alias")
@@ -189,7 +189,7 @@ public class Folks.Backends.Kf.Persona : Folks.Persona,
                 }
 
               /* IM addresses */
-              var protocol = key;
+              unowned string protocol = key;
               var im_addresses = this._key_file.get_string_list (
                   this.display_id, protocol);
 
diff --git a/backends/telepathy/lib/tpf-persona-store.vala b/backends/telepathy/lib/tpf-persona-store.vala
index 4a28ea8..af4211b 100644
--- a/backends/telepathy/lib/tpf-persona-store.vala
+++ b/backends/telepathy/lib/tpf-persona-store.vala
@@ -632,7 +632,7 @@ public class Tpf.PersonaStore : Folks.PersonaStore
 
   private void _channel_group_changes_resolve (Channel channel)
     {
-      var group = channel.get_identifier ();
+      unowned string group = channel.get_identifier ();
 
       var change_maps = new HashMap<HashSet<Tpf.Persona>, bool> ();
       if (this._group_outgoing_adds[group] != null)
@@ -691,7 +691,7 @@ public class Tpf.PersonaStore : Folks.PersonaStore
       channel.notify["channel-ready"].connect ((s, p) =>
         {
           var c = (Channel) s;
-          var name = c.get_identifier ();
+          unowned string name = c.get_identifier ();
 
           debug ("Channel '%s' is ready.", name);
 
@@ -890,7 +890,8 @@ public class Tpf.PersonaStore : Folks.PersonaStore
             return;
         }
 
-      var message = TelepathyGLib.asv_get_string (details, "message");
+      unowned string message = TelepathyGLib.asv_get_string (details,
+          "message");
       bool valid;
       Persona? actor = null;
       var actor_handle = TelepathyGLib.asv_get_uint32 (details, "actor",
@@ -1493,7 +1494,7 @@ public class Tpf.PersonaStore : Folks.PersonaStore
         {
           /* Add or remove the persona to the list of favourites as
            * appropriate. */
-          var id = ((Tpf.Persona) persona).contact.get_identifier ();
+          unowned string id = ((Tpf.Persona) persona).contact.get_identifier ();
 
           if (is_favourite)
             yield this._logger.add_favourite_contact (id);
diff --git a/backends/telepathy/lib/tpf-persona.vala b/backends/telepathy/lib/tpf-persona.vala
index 6e530cd..9713c87 100644
--- a/backends/telepathy/lib/tpf-persona.vala
+++ b/backends/telepathy/lib/tpf-persona.vala
@@ -142,14 +142,14 @@ public class Tpf.Persona : Folks.Persona,
         {
           value.foreach ((k, v) =>
             {
-              var group = (string) k;
+              unowned string group = (string) k;
               if (this._groups.lookup (group) == false)
                 this._change_group (group, true);
             });
 
           this._groups.foreach ((k, v) =>
             {
-              var group = (string) k;
+              unowned string group = (string) k;
               if (value.lookup (group) == false)
                 this._change_group (group, true);
             });
diff --git a/folks/backend-store.vala b/folks/backend-store.vala
index 10dc0d6..c1d83f3 100644
--- a/folks/backend-store.vala
+++ b/folks/backend-store.vala
@@ -200,7 +200,7 @@ public class Folks.BackendStore : Object {
 
       var modules = new HashMap<string, File?> ();
       var path_split = path.split (":");
-      foreach (var subpath in path_split)
+      foreach (unowned string subpath in path_split)
         {
           var file = File.new_for_path (subpath);
           assert (file != null);
@@ -544,7 +544,8 @@ public class Folks.BackendStore : Object {
   private async void _load_disabled_backend_names ()
     {
       File file;
-      var path = Environment.get_variable ("FOLKS_BACKEND_STORE_KEY_FILE_PATH");
+      unowned string path = Environment.get_variable (
+          "FOLKS_BACKEND_STORE_KEY_FILE_PATH");
       if (path == null)
         {
           file = File.new_for_path (Environment.get_user_data_dir ());
diff --git a/folks/individual-aggregator.vala b/folks/individual-aggregator.vala
index aab8eec..604a739 100644
--- a/folks/individual-aggregator.vala
+++ b/folks/individual-aggregator.vala
@@ -357,7 +357,7 @@ public class Folks.IndividualAggregator : Object
 
                   persona.linkable_property_to_links (prop_name, (l) =>
                     {
-                      var prop_linking_value = (string) l;
+                      unowned string prop_linking_value = (string) l;
                       var candidate_ind =
                           this._link_map.lookup (prop_linking_value);
 
@@ -454,7 +454,7 @@ public class Folks.IndividualAggregator : Object
                       final_persona.linkable_property_to_links (prop_name,
                           (l) =>
                         {
-                          string prop_linking_value = (string) l;
+                          unowned string prop_linking_value = (string) l;
 
                           debug ("            %s", prop_linking_value);
                           this._link_map.replace (prop_linking_value,
@@ -506,7 +506,7 @@ public class Folks.IndividualAggregator : Object
           /* Remove maps from the Persona's linkable properties to
            * Individuals. Add the Individuals to a list of Individuals to be
            * removed. */
-          foreach (string prop_name in persona.linkable_properties)
+          foreach (unowned string prop_name in persona.linkable_properties)
             {
               /* FIXME: can't be var because of bgo#638208 */
               unowned ObjectClass pclass = persona.get_class ();
@@ -521,7 +521,7 @@ public class Folks.IndividualAggregator : Object
 
               persona.linkable_property_to_links (prop_name, (l) =>
                 {
-                  string prop_linking_value = (string) l;
+                  unowned string prop_linking_value = (string) l;
 
                   debug ("        %s", prop_linking_value);
                   this._link_map.remove (prop_linking_value);
diff --git a/folks/individual.vala b/folks/individual.vala
index e2542a0..b07e4b1 100644
--- a/folks/individual.vala
+++ b/folks/individual.vala
@@ -476,13 +476,13 @@ public class Folks.Individual : Object,
 
       new_groups.foreach ((k, v) =>
         {
-          var group = (string) k;
+          unowned string group = (string) k;
           if (this._groups.lookup (group) != true)
             {
               this._groups.insert (group, true);
               this._groups.foreach ((k, v) =>
                 {
-                  var g = (string) k;
+                  unowned string g = (string) k;
                   debug ("   %s", g);
                 });
 
@@ -494,14 +494,14 @@ public class Folks.Individual : Object,
       var removes = new GLib.List<string> ();
       this._groups.foreach ((k, v) =>
         {
-          var group = (string) k;
+          unowned string group = (string) k;
           if (new_groups.lookup (group) != true)
             removes.prepend (group);
         });
 
       removes.foreach ((l) =>
         {
-          var group = (string) l;
+          unowned string group = (string) l;
           this._groups.remove (group);
           this.group_changed (group, false);
         });



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