[geary/wip/734757-msgset] Further cleanup and optimizations



commit acb56ab72122150db1de445636e0bc55b320a4cf
Author: Jim Nelson <jim yorba org>
Date:   Tue Aug 19 13:29:30 2014 -0700

    Further cleanup and optimizations

 src/engine/imap/command/imap-message-set.vala |   40 ++++++++++++-------------
 1 files changed, 19 insertions(+), 21 deletions(-)
---
diff --git a/src/engine/imap/command/imap-message-set.vala b/src/engine/imap/command/imap-message-set.vala
index 41eced4..44f6014 100644
--- a/src/engine/imap/command/imap-message-set.vala
+++ b/src/engine/imap/command/imap-message-set.vala
@@ -16,8 +16,8 @@ extern void qsort(void *base, size_t num, size_t size, CompareFunc compare_func)
  */
 
 public class Geary.Imap.MessageSet : BaseObject {
-    // 2^32 in base 10 requires four bytes (characters) on the wire plus a separator between each
-    // one, means ~11 bytes per seqnum or UID ... to keep lists below server maximums, this value
+    // 2^32 in base 10 requires ten bytes (characters) on the wire plus a separator between each
+    // one, i.e. ~11 bytes per seqnum or UID ... to keep lists below server maximums, this value
     // is set to keep max. command length somewhere under 1K (including tag, command, parameters,
     // etc.)
     private const int MAX_SPARSE_VALUES_PER_SET = 50;
@@ -117,7 +117,7 @@ public class Geary.Imap.MessageSet : BaseObject {
      * command beyond the server maximum, and so they will be broken up into multiple sets.
      */
     public static Gee.List<MessageSet> sparse(Gee.Collection<SequenceNumber> seq_nums) {
-        return build_sparse(seq_array_to_int64(seq_nums), false);
+        return build_sparse_sets(seq_array_to_int64(seq_nums), false);
     }
     
     /**
@@ -127,36 +127,32 @@ public class Geary.Imap.MessageSet : BaseObject {
      * command beyond the server maximum, and so they will be broken up into multiple sets.
      */
     public static Gee.List<MessageSet> uid_sparse(Gee.Collection<UID> msg_uids) {
-        return build_sparse(uid_array_to_int64(msg_uids), true);
+        return build_sparse_sets(uid_array_to_int64(msg_uids), true);
     }
     
-    private static Gee.List<MessageSet> build_sparse(int64[] sorted, bool is_uid) {
+    // create zero or more MessageSets of no more than MAX_SPARSE_VALUES_PER_SET UIDs/sequence
+    // numbers
+    private static Gee.List<MessageSet> build_sparse_sets(int64[] sorted, bool is_uid) {
         Gee.List<MessageSet> list = new Gee.ArrayList<MessageSet>();
         
-        int start_index = 0;
+        int start = 0;
         for (;;) {
-            if (start_index >= sorted.length)
+            if (start >= sorted.length)
                 break;
             
-            int end_index = (start_index + MAX_SPARSE_VALUES_PER_SET).clamp(0, sorted.length);
-            unowned int64[] slice = sorted[start_index:end_index];
-            
-            debug("MessageSet.build_sparse: sorted=%d start:end=%d:%d slice=%d",
-                sorted.length, start_index, end_index, slice.length);
+            int end = (start + MAX_SPARSE_VALUES_PER_SET).clamp(0, sorted.length);
+            unowned int64[] slice = sorted[start:end];
             
             string sparse_range = build_sparse_range(slice);
             list.add(is_uid ? new MessageSet.uid_custom(sparse_range) : new MessageSet.custom(sparse_range));
             
-            debug("MessageSet.build_sparse: sorted=%d start:end=%d:%d slice=%d sparse_range=%s",
-                sorted.length, start_index, end_index, slice.length, sparse_range);
-            
-            start_index += MAX_SPARSE_VALUES_PER_SET;
+            start = end;
         }
         
         return list;
     }
     
-    // Builds sparse range of either UID values or message numbers.  Values should be sorted before
+    // Builds sparse range of either UID values or sequence numbers.  Values should be sorted before
     // calling to maximum finding runs.
     private static string build_sparse_range(int64[] seq_nums) {
         assert(seq_nums.length > 0);
@@ -221,9 +217,10 @@ public class Geary.Imap.MessageSet : BaseObject {
         sorted.add_all(seq_nums);
         
         // build sorted array
-        int64[] ret = new int64[0];
+        int64[] ret = new int64[sorted.size];
+        int index = 0;
         foreach (SequenceNumber seq_num in sorted)
-            ret += (int64) seq_num.value;
+            ret[index++] = (int64) seq_num.value;
         
         return ret;
     }
@@ -234,9 +231,10 @@ public class Geary.Imap.MessageSet : BaseObject {
         sorted.add_all(msg_uids);
         
         // build sorted array
-        int64[] ret = new int64[0];
+        int64[] ret = new int64[sorted.size];
+        int index = 0;
         foreach (UID uid in sorted)
-            ret += uid.value;
+            ret[index++] = uid.value;
         
         return ret;
     }


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