[PATCH] WIP: sort top level messages in thread

David Bremner david at tethera.net
Sun Aug 26 18:53:53 PDT 2018


this needs a test, and  memory de-allocation of the replaced
lists. Currently it creates a fair amount of garbage.
---

Hi Gregor;

Sorry for the lack of reply, some vacation intervened. Can you test
this patch? I think it fixes the second thread also.

lib/message.cc        | 43 +++++++++++++++++++++++--------------------
 lib/notmuch-private.h |  5 +++--
 lib/thread.cc         |  9 ++-------
 3 files changed, 28 insertions(+), 29 deletions(-)

diff --git a/lib/message.cc b/lib/message.cc
index 3c547298..f329be20 100644
--- a/lib/message.cc
+++ b/lib/message.cc
@@ -639,34 +639,37 @@ _cmpmsg (const void *pa, const void *pb)
     return (int) difftime (time_a, time_b);
 }
 
-void
-_notmuch_message_sort_subtree (notmuch_message_t *root)
-{
-    size_t child_count = 0;
-    size_t child_capacity = 16;
-    notmuch_message_t **children = talloc_zero_array (root, notmuch_message_t *, child_capacity);
+notmuch_message_list_t *
+_notmuch_message_sort_subtrees (notmuch_message_list_t *list) {
+
+    size_t count = 0;
+    size_t capacity = 16;
 
-    for (notmuch_messages_t *messages = _notmuch_messages_create (root->replies);
+    if (!list)
+	return list;
+
+    notmuch_message_t **message_array = talloc_zero_array (list, notmuch_message_t *, capacity);
+
+    for (notmuch_messages_t *messages = _notmuch_messages_create (list);
 	 notmuch_messages_valid (messages);
 	 notmuch_messages_move_to_next (messages)) {
-	notmuch_message_t *child = notmuch_messages_get (messages);
-	if (child_count >= child_capacity) {
-	    child_capacity *= 2;
-	    children = talloc_realloc (root, children, notmuch_message_t *, child_capacity);
+	notmuch_message_t *root = notmuch_messages_get (messages);
+	if (count >= capacity) {
+	    capacity *= 2;
+	    message_array = talloc_realloc (root, message_array, notmuch_message_t *, capacity);
 	}
-	children[child_count++] = child;
-	_notmuch_message_sort_subtree (child);
+	message_array[count++] = root;
+	root->replies = _notmuch_message_sort_subtrees (root->replies);
     }
 
-    notmuch_message_list_t *new_replies = _notmuch_message_list_create (root);
+    notmuch_message_list_t *new_list = _notmuch_message_list_create (list);
 
-    qsort (children, child_count, sizeof (notmuch_message_t *), _cmpmsg);
-    for (size_t i=0; i<child_count; i++){
-	_notmuch_message_list_add_message (new_replies, children[i]);
+    qsort (message_array, count, sizeof (notmuch_message_t *), _cmpmsg);
+    for (size_t i=0; i<count; i++){
+	_notmuch_message_list_add_message (new_list, message_array[i]);
     }
-    talloc_free (root->replies);
-    root->replies = new_replies;
-    talloc_free (children);
+    talloc_free (message_array);
+    return new_list;
 }
 
 notmuch_messages_t *
diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h
index 67fd4990..64f4e982 100644
--- a/lib/notmuch-private.h
+++ b/lib/notmuch-private.h
@@ -558,8 +558,9 @@ size_t _notmuch_message_get_thread_depth (notmuch_message_t *message);
 void
 _notmuch_message_label_depths (notmuch_message_t *message,
 			       size_t depth);
-void
-_notmuch_message_sort_subtree (notmuch_message_t *message);
+
+notmuch_message_list_t *
+_notmuch_message_sort_subtrees (notmuch_message_list_t *list);
 
 /* sha1.c */
 
diff --git a/lib/thread.cc b/lib/thread.cc
index a5047103..e9e15a5c 100644
--- a/lib/thread.cc
+++ b/lib/thread.cc
@@ -505,13 +505,8 @@ _resolve_thread_relationships (notmuch_thread_t *thread)
 	    _notmuch_message_list_add_message (thread->toplevel_list, message);
     }
 
-    for (notmuch_messages_t *messages = _notmuch_messages_create (thread->toplevel_list);
-	 notmuch_messages_valid (messages);
-	 notmuch_messages_move_to_next (messages))
-    {
-	notmuch_message_t *message = notmuch_messages_get (messages);
-	_notmuch_message_sort_subtree (message);
-    }
+    /* XXX this creates garbage */
+    thread->toplevel_list = _notmuch_message_sort_subtrees (thread->toplevel_list);
 
     talloc_free (local);
 }
-- 
2.18.0



More information about the notmuch mailing list