]> mj.ucw.cz Git - libucw.git/blob - lib/sorter/s-internal.h
Added a couple of tests with the old sorter to have reference for speed benchmarks.
[libucw.git] / lib / sorter / s-internal.h
1 /*
2  *      UCW Library -- Universal Sorter: Internal Sorting Module
3  *
4  *      (c) 2007 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 typedef struct {
11   P(key) *key;
12   // FIXME: Add the hash here to save cache misses
13 } P(internal_item_t);
14
15 #define ASORT_PREFIX(x) SORT_PREFIX(array_##x)
16 #define ASORT_KEY_TYPE P(internal_item_t)
17 #define ASORT_ELT(i) ary[i]
18 #define ASORT_LT(x,y) (P(compare)((x).key, (y).key) < 0)
19 #define ASORT_EXTRA_ARGS , P(internal_item_t) *ary
20 #include "lib/arraysort.h"
21
22 static inline void *P(internal_get_data)(P(key) *key)
23 {
24   uns ksize = SORT_KEY_SIZE(*key);
25 #ifdef SORT_UNIFY
26   ksize = ALIGN_TO(ksize, CPU_STRUCT_ALIGN);
27 #endif
28   return (byte *) key + ksize;
29 }
30
31 static int P(internal)(struct sort_context *ctx, struct sort_bucket *bin, struct sort_bucket *bout, struct sort_bucket *bout_only)
32 {
33   sorter_alloc_buf(ctx);
34   struct fastbuf *in = sbuck_read(bin);
35
36   P(key) key, *keybuf = ctx->key_buf;
37   if (!keybuf)
38     keybuf = ctx->key_buf = sorter_alloc(ctx, sizeof(key));
39   if (ctx->more_keys)
40     {
41       key = *keybuf;
42       ctx->more_keys = 0;
43     }
44   else if (!P(read_key)(in, &key))
45     return 0;
46
47 #ifdef SORT_VAR_DATA
48   if (sizeof(key) + 1024 + SORT_DATA_SIZE(key) > ctx->big_buf_half_size)
49     {
50       SORT_XTRACE(3, "s-internal: Generating a giant run");
51       struct fastbuf *out = sbuck_write(bout); /* FIXME: Using a non-direct buffer would be nice here */
52       P(copy_data)(&key, in, out);
53       bout->runs++;
54       return 1;                         // We don't know, but 1 is always safe
55     }
56 #endif
57
58   size_t bufsize = ctx->big_buf_half_size;      /* FIXME: In some cases, we can use the whole buffer */
59 #ifdef CPU_64BIT_POINTERS
60   bufsize = MIN((u64)bufsize, (u64)~0U * sizeof(P(internal_item_t)));   // The number of records must fit in uns
61 #endif
62
63   SORT_XTRACE(3, "s-internal: Reading (bufsize=%zd)", bufsize);
64   P(internal_item_t) *item_array = ctx->big_buf, *item = item_array, *last_item;
65   byte *end = (byte *) ctx->big_buf + bufsize;
66   do
67     {
68       uns ksize = SORT_KEY_SIZE(key);
69 #ifdef SORT_UNIFY
70       uns ksize_aligned = ALIGN_TO(ksize, CPU_STRUCT_ALIGN);
71 #else
72       uns ksize_aligned = ksize;
73 #endif
74       uns dsize = SORT_DATA_SIZE(key);
75       uns recsize = ALIGN_TO(ksize_aligned + dsize, CPU_STRUCT_ALIGN);
76       if (unlikely(sizeof(P(internal_item_t)) + recsize > (size_t)(end - (byte *) item)))
77         {
78           ctx->more_keys = 1;
79           *keybuf = key;
80           break;
81         }
82       end -= recsize;
83       memcpy(end, &key, ksize);
84 #ifdef SORT_VAR_DATA
85       breadb(in, end + ksize_aligned, dsize);
86 #endif
87       item->key = (P(key)*) end;
88       item++;
89     }
90   while (P(read_key)(in, &key));
91   last_item = item;
92
93   uns count = last_item - item_array;
94   SORT_XTRACE(3, "s-internal: Sorting %u items", count);
95   P(array_sort)(count, item_array);
96
97   SORT_XTRACE(3, "s-internal: Writing");
98   if (!ctx->more_keys)
99     bout = bout_only;
100   struct fastbuf *out = sbuck_write(bout);
101   bout->runs++;
102   uns merged UNUSED = 0;
103   for (item = item_array; item < last_item; item++)
104     {
105 #ifdef SORT_UNIFY
106       if (item < last_item - 1 && !P(compare)(item->key, item[1].key))
107         {
108           // Rewrite the item structures with just pointers to keys and place
109           // pointers to data in the secondary array.
110           P(key) **key_array = (void *) item;
111           void **data_array = (void **) ctx->big_buf_half;
112           key_array[0] = item[0].key;
113           data_array[0] = P(internal_get_data)(key_array[0]);
114           uns cnt;
115           for (cnt=1; item+cnt < last_item && !P(compare)(key_array[0], item[cnt].key); cnt++)
116             {
117               key_array[cnt] = item[cnt].key;
118               data_array[cnt] = P(internal_get_data)(key_array[cnt]);
119             }
120           P(write_merged)(out, key_array, data_array, cnt, data_array+cnt);
121           item += cnt - 1;
122           merged += cnt - 1;
123           continue;
124         }
125 #endif
126 #ifdef SORT_ASSERT_UNIQUE
127       ASSERT(item == last_item-1 || P(compare)(item->key, item[1].key) < 0);
128 #endif
129       P(write_key)(out, item->key);
130 #ifdef SORT_VAR_DATA
131       bwrite(out, P(internal_get_data)(item->key), SORT_DATA_SIZE(*item->key));
132 #endif
133     }
134 #ifdef SORT_UNIFY
135   SORT_XTRACE(3, "Merging reduced %u records", merged);
136 #endif
137
138   return ctx->more_keys;
139 }