mu: Sort containers by comparing their subtree leaders

Traverse the container tree depth first and for each container find
the node in the subtree rooted at this container which comes first in
the descending sort order. Remember it as the subtree leader. Then,
while sorting siblings, compare their subtree leaders instead of the
sibling containers themselves.

IOW, make threads containing the newest message float to the top when
sorting by date in the descending order.

There is no significant performance degradation when sorting a
mailbox with ~16k messages:

$ mu find maildir:/INBOX | wc -l
16503

Current state:

$ perf stat --event=task-clock --repeat=10 -- \
  mu find maildir:/INBOX -n 1 -t > /dev/null

 Performance counter stats for 'mu find maildir:/INBOX -n 1 -t' (10 runs):

       1231.761588      task-clock (msec)         #    0.996 CPUs utilized            ( +-  1.02% )

       1.236209133 seconds time elapsed                                          ( +-  1.08% )

With patch applied:

$ perf stat --event=task-clock --repeat=10 -- \
  mu find maildir:/INBOX -n 1 -t > /dev/null

 Performance counter stats for 'mu find maildir:/INBOX -n 1 -t' (10 runs):

       1459.883316      task-clock (msec)         #    0.998 CPUs utilized            ( +-  0.72% )

       1.462540088 seconds time elapsed                                          ( +-  0.77% )

This implements https://github.com/djcb/mu/issues/164.
This commit is contained in:
Jakub Sitnicki 2014-07-07 06:23:11 +02:00
parent 619fb86885
commit 32f5c8b1f6
2 changed files with 39 additions and 11 deletions

View File

@ -52,6 +52,7 @@ mu_container_new (MuMsg *msg, guint docid, const char *msgid)
if (msg)
c->msg = mu_msg_ref (msg);
c->leader = c;
c->docid = docid;
c->msgid = msgid;
@ -356,20 +357,41 @@ container_cmp (MuContainer *a, MuContainer *b, MuMsgFieldId mfid)
return mu_msg_cmp (a->msg, b->msg, mfid);
}
static gboolean
container_is_leaf (const MuContainer *c)
{
return c->child == NULL;
}
static MuContainer*
container_max (MuContainer *a, MuContainer *b, MuMsgFieldId mfid)
{
return container_cmp (a, b, mfid) > 0 ? a : b;
}
static MuContainer*
find_sorted_tree_leader (MuContainer *root, SortFuncData *order)
{
MuContainer *last_child;
if (container_is_leaf (root))
return root;
if (!order->descending)
last_child = root->child->last;
else /* reversed order, first is last */
last_child = root->child;
return container_max (root, last_child->leader, order->mfid);
}
static int
sort_func_wrapper (MuContainer *a, MuContainer *b, SortFuncData *data)
{
MuContainer *a1, *b1;
/* use the first non-empty 'left child' message if this one
* is */
for (a1 = a; a1->msg == NULL && a1->child != NULL; a1 = a1->child);
for (b1 = b; b1->msg == NULL && b1->child != NULL; b1 = b1->child);
if (data->descending)
return container_cmp (b1, a1, data->mfid);
return container_cmp (b->leader, a->leader, data->mfid);
else
return container_cmp (a1, b1, data->mfid);
return container_cmp (a->leader, b->leader, data->mfid);
}
static MuContainer*
@ -381,9 +403,11 @@ container_sort_real (MuContainer *c, SortFuncData *sfdata)
if (!c)
return NULL;
for (cur = c; cur; cur = cur->next)
for (cur = c; cur; cur = cur->next) {
if (cur->child)
cur->child = container_sort_real (cur->child, sfdata);
cur->leader = find_sorted_tree_leader (cur, sfdata);
}
/* sort siblings */
lst = mu_container_to_list (c);
@ -396,7 +420,6 @@ container_sort_real (MuContainer *c, SortFuncData *sfdata)
return c;
}
MuContainer*
mu_container_sort (MuContainer *c, MuMsgFieldId mfid, gboolean descending,
gpointer user_data)

View File

@ -45,6 +45,11 @@ struct _MuContainer {
* */
struct _MuContainer *last;
/* Node in the subtree rooted at this node which comes first
* in the descending sort order, e.g. the latest message if
* sorting by date. We compare the leaders when ordering
* subtrees. */
struct _MuContainer *leader;
MuMsg *msg;
const char *msgid;