You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@subversion.apache.org by st...@apache.org on 2011/09/11 03:06:03 UTC

svn commit: r1167659 - /subversion/trunk/subversion/libsvn_subr/sorts.c

Author: stsp
Date: Sun Sep 11 01:06:03 2011
New Revision: 1167659

URL: http://svn.apache.org/viewvc?rev=1167659&view=rev
Log:
Small optimisation in svn_sort__hash().

* subversion/libsvn_subr/sorts.c
  (svn_sort__hash): Don't sort the generated array if it is already sorted.

Modified:
    subversion/trunk/subversion/libsvn_subr/sorts.c

Modified: subversion/trunk/subversion/libsvn_subr/sorts.c
URL: http://svn.apache.org/viewvc/subversion/trunk/subversion/libsvn_subr/sorts.c?rev=1167659&r1=1167658&r2=1167659&view=diff
==============================================================================
--- subversion/trunk/subversion/libsvn_subr/sorts.c (original)
+++ subversion/trunk/subversion/libsvn_subr/sorts.c Sun Sep 11 01:06:03 2011
@@ -140,21 +140,38 @@ svn_sort__hash(apr_hash_t *ht,
 {
   apr_hash_index_t *hi;
   apr_array_header_t *ary;
+  svn_boolean_t sorted;
+  svn_sort__item_t *prev_item;
 
   /* allocate an array with enough elements to hold all the keys. */
   ary = apr_array_make(pool, apr_hash_count(ht), sizeof(svn_sort__item_t));
 
   /* loop over hash table and push all keys into the array */
+  sorted = TRUE;
+  prev_item = NULL;
   for (hi = apr_hash_first(pool, ht); hi; hi = apr_hash_next(hi))
     {
       svn_sort__item_t *item = apr_array_push(ary);
 
       apr_hash_this(hi, &item->key, &item->klen, &item->value);
+
+      if (prev_item == NULL)
+        {
+          prev_item = item;
+          continue;
+        }
+
+      if (sorted)
+        {
+          sorted = (comparison_func(prev_item, item) < 0);
+          prev_item = item;
+        }
     }
 
-  /* now quicksort the array.  */
-  qsort(ary->elts, ary->nelts, ary->elt_size,
-        (int (*)(const void *, const void *))comparison_func);
+  /* quicksort the array if it isn't already sorted.  */
+  if (!sorted)
+    qsort(ary->elts, ary->nelts, ary->elt_size,
+          (int (*)(const void *, const void *))comparison_func);
 
   return ary;
 }