]> mj.ucw.cz Git - libucw.git/blobdiff - lib/sorter/array.c
Implemented ThreadChunk limit.
[libucw.git] / lib / sorter / array.c
index 838ad98c61eff7c4335122e76e35d0eb661dc767..d724f84128ed5a1bd14bd32e6bc68d0e40e52054 100644 (file)
@@ -15,7 +15,6 @@
 #include <string.h>
 #include <alloca.h>
 
-#define ASORT_MIN_RADIX 5000           // FIXME: var?
 #define ASORT_MIN_SHIFT 2
 
 static void
@@ -49,7 +48,7 @@ asort_radix(struct asort_context *ctx, void *array, void *buffer, uns num_elts,
   for (uns i=0; i<buckets; i++)
     {
       uns n = cnt[i] - pos;
-      if (n < ASORT_MIN_RADIX || shift < ASORT_MIN_SHIFT)
+      if (n < sorter_radix_threshold || shift < ASORT_MIN_SHIFT)
        {
          ctx->quicksort(buffer, n);
          if (!swapped_output)
@@ -215,8 +214,9 @@ rs_finish(struct worker_thread *thr UNUSED, struct work *ww)
 {
   struct rs_work *w = (struct rs_work *) ww;
 
-  DBG("Thread %d: Finishing %d items, shift=%d", thr->id, w->num_elts, w->shift);
-  if (w->shift < ASORT_MIN_SHIFT || w->num_elts < ASORT_MIN_RADIX)
+  if (thr)
+    DBG("Thread %d: Finishing %d items, shift=%d", thr->id, w->num_elts, w->shift);
+  if (w->shift < ASORT_MIN_SHIFT || w->num_elts < sorter_radix_threshold)
     {
       w->ctx->quicksort(w->in, w->num_elts);
       if (w->swap_output)
@@ -224,7 +224,8 @@ rs_finish(struct worker_thread *thr UNUSED, struct work *ww)
     }
   else
     asort_radix(w->ctx, w->in, w->out, w->num_elts, w->shift, w->swap_output);
-  DBG("Thread %d: Finishing done", thr->id);
+  if (thr)
+    DBG("Thread %d: Finishing done", thr->id);
 }
 
 static void
@@ -299,6 +300,8 @@ rs_radix(struct asort_context *ctx, void *array, void *buffer, uns num_elts, uns
   for (uns i=0; i<buckets; i++)
     {
       uns n = cnt[i] - pos;
+      if (!n)
+       continue;
       if (n * ctx->elt_size < sorter_thread_threshold)
        {
          struct rs_work *w = ep_alloc(ctx->eltpool);
@@ -310,8 +313,17 @@ rs_radix(struct asort_context *ctx, void *array, void *buffer, uns num_elts, uns
          w->num_elts = n;
          w->shift = shift;
          w->swap_output = !swapped_output;
-         clist_add_tail(&ctx->rs_bits, &w->w.n);
-         DBG("Scheduling block %d+%d", pos, n);
+         if (n * ctx->elt_size < sorter_thread_chunk)
+           {
+             DBG("Sorting block %d+%d inline", pos, n);
+             rs_finish(NULL, &w->w);
+             ep_free(ctx->eltpool, w);
+           }
+         else
+           {
+             clist_add_tail(&ctx->rs_bits, &w->w.n);
+             DBG("Scheduling block %d+%d", pos, n);
+           }
        }
       else
        rs_radix(ctx, buffer, array, n, shift, !swapped_output);
@@ -373,7 +385,7 @@ asort_run(struct asort_context *ctx)
                              ctx->num_elts * ctx->elt_size >= sorter_thread_threshold &&
                              !(sorter_debug & SORT_DEBUG_ASORT_NO_THREADS));
 
-  if (ctx->num_elts < ASORT_MIN_RADIX ||
+  if (ctx->num_elts < sorter_radix_threshold ||
       ctx->hash_bits <= ASORT_MIN_SHIFT ||
       !ctx->radix_split ||
       (sorter_debug & SORT_DEBUG_ASORT_NO_RADIX))