]> mj.ucw.cz Git - libucw.git/blob - lib/sorter/s-internal.h
Introduced ARRAY_LONG_HASH and unrolled radix-sorting primitives.
[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 #include "lib/stkstring.h"
11
12 typedef struct {
13   P(key) *key;
14   // FIXME: Add the hash here to save cache misses
15 } P(internal_item_t);
16
17 #define ASORT_PREFIX(x) SORT_PREFIX(array_##x)
18 #define ASORT_KEY_TYPE P(internal_item_t)
19 #define ASORT_LT(x,y) (P(compare)((x).key, (y).key) < 0)
20 #define ASORT_PAGE_ALIGNED
21 #ifdef SORT_INTERNAL_RADIX
22 #  define ASORT_HASH(x) P(hash)((x).key)
23 #    ifdef SORT_LONG_HASH
24 #      define ASORT_LONG_HASH
25 #    endif
26 #endif
27 #include "lib/sorter/array.h"
28
29 /*
30  *  The big_buf has the following layout:
31  *
32  *      +-------------------------------------------------------------------------------+
33  *      | array of internal_item's                                                      |
34  *      +-------------------------------------------------------------------------------+
35  *      | padding to make the following part page-aligned                               |
36  *      +--------------------------------+----------------------------------------------+
37  *      | shadow copy of item array      | array of pointers to data for write_merged() |
38  *      | used if radix-sorting          +----------------------------------------------+
39  *      |                                | workspace for write_merged()                 |
40  *      +--------------------------------+----------------------------------------------+
41  *      |              +---------+                                                      |
42  *      |              | key     |                                                      |
43  *      |              +---------+                                                      |
44  *      | sequence of  | padding |                                                      |
45  *      | items        +---------+                                                      |
46  *      |              | data    |                                                      |
47  *      |              +---------+                                                      |
48  *      |              | padding |                                                      |
49  *      |              +---------+                                                      |
50  *      +-------------------------------------------------------------------------------+
51  *
52  *  (the data which are in different columns are never accessed simultaneously,
53  *   so we use a single buffer for both)
54  */
55
56 static inline void *P(internal_get_data)(P(key) *key)
57 {
58   uns ksize = SORT_KEY_SIZE(*key);
59 #ifdef SORT_UNIFY
60   ksize = ALIGN_TO(ksize, CPU_STRUCT_ALIGN);
61 #endif
62   return (byte *) key + ksize;
63 }
64
65 static inline size_t P(internal_workspace)(P(key) *key UNUSED)
66 {
67   size_t ws = 0;
68 #ifdef SORT_UNIFY
69   ws += sizeof(void *);
70 #endif
71 #ifdef SORT_UNIFY_WORKSPACE
72   ws += SORT_UNIFY_WORKSPACE(*key);
73 #endif
74 #ifdef SORT_INTERNAL_RADIX
75   ws = MAX(ws, sizeof(P(internal_item_t)));
76 #endif
77   return ws;
78 }
79
80 static int P(internal)(struct sort_context *ctx, struct sort_bucket *bin, struct sort_bucket *bout, struct sort_bucket *bout_only)
81 {
82   sorter_alloc_buf(ctx);
83   struct fastbuf *in = sbuck_read(bin);
84
85   P(key) key, *keybuf = ctx->key_buf;
86   if (!keybuf)
87     keybuf = ctx->key_buf = sorter_alloc(ctx, sizeof(key));
88   if (ctx->more_keys)
89     {
90       key = *keybuf;
91       ctx->more_keys = 0;
92     }
93   else if (!P(read_key)(in, &key))
94     return 0;
95
96   size_t bufsize = ctx->big_buf_size;
97 #ifdef SORT_VAR_DATA
98   if (sizeof(key) + 2*CPU_PAGE_SIZE + SORT_DATA_SIZE(key) + P(internal_workspace)(&key) > bufsize)
99     {
100       SORT_XTRACE(3, "s-internal: Generating a giant run");
101       struct fastbuf *out = sbuck_write(bout);
102       P(copy_data)(&key, in, out);
103       bout->runs++;
104       return 1;                         // We don't know, but 1 is always safe
105     }
106 #endif
107
108   SORT_XTRACE(4, "s-internal: Reading");
109   P(internal_item_t) *item_array = ctx->big_buf, *item = item_array, *last_item;
110   byte *end = (byte *) ctx->big_buf + bufsize;
111   size_t remains = bufsize - CPU_PAGE_SIZE;
112   do
113     {
114       uns ksize = SORT_KEY_SIZE(key);
115 #ifdef SORT_UNIFY
116       uns ksize_aligned = ALIGN_TO(ksize, CPU_STRUCT_ALIGN);
117 #else
118       uns ksize_aligned = ksize;
119 #endif
120       uns dsize = SORT_DATA_SIZE(key);
121       uns recsize = ALIGN_TO(ksize_aligned + dsize, CPU_STRUCT_ALIGN);
122       size_t totalsize = recsize + sizeof(P(internal_item_t) *) + P(internal_workspace)(&key);
123       if (unlikely(totalsize > remains
124 #ifdef CPU_64BIT_POINTERS
125                    || item >= item_array + ~0U          // The number of items must fit in an uns
126 #endif
127          ))
128         {
129           ctx->more_keys = 1;
130           *keybuf = key;
131           break;
132         }
133       remains -= totalsize;
134       end -= recsize;
135       memcpy(end, &key, ksize);
136 #ifdef SORT_VAR_DATA
137       breadb(in, end + ksize_aligned, dsize);
138 #endif
139       item->key = (P(key)*) end;
140       item++;
141     }
142   while (P(read_key)(in, &key));
143   last_item = item;
144
145   uns count = last_item - item_array;
146   void *workspace UNUSED = ALIGN_PTR(last_item, CPU_PAGE_SIZE);
147   SORT_XTRACE(3, "s-internal: Read %u items (%s items, %s workspace, %s data)",
148         count,
149         stk_fsize((byte*)last_item - (byte*)item_array),
150         stk_fsize(end - (byte*)last_item - remains),
151         stk_fsize((byte*)ctx->big_buf + bufsize - end));
152   timestamp_t timer;
153   init_timer(&timer);
154   item_array = P(array_sort)(item_array, count,
155 #ifdef SORT_INTERNAL_RADIX
156     workspace, bin->hash_bits
157 #else
158     NULL, 0
159 #endif
160     );
161   ctx->total_int_time += get_timer(&timer);
162
163   SORT_XTRACE(4, "s-internal: Writing");
164   if (!ctx->more_keys)
165     bout = bout_only;
166   struct fastbuf *out = sbuck_write(bout);
167   bout->runs++;
168   uns merged UNUSED = 0;
169   for (item = item_array; item < last_item; item++)
170     {
171 #ifdef SORT_UNIFY
172       if (item < last_item - 1 && !P(compare)(item->key, item[1].key))
173         {
174           // Rewrite the item structures with just pointers to keys and place
175           // pointers to data in the workspace.
176           P(key) **key_array = (void *) item;
177           void **data_array = workspace;
178           key_array[0] = item[0].key;
179           data_array[0] = P(internal_get_data)(key_array[0]);
180           uns cnt;
181           for (cnt=1; item+cnt < last_item && !P(compare)(key_array[0], item[cnt].key); cnt++)
182             {
183               key_array[cnt] = item[cnt].key;
184               data_array[cnt] = P(internal_get_data)(key_array[cnt]);
185             }
186           P(write_merged)(out, key_array, data_array, cnt, data_array+cnt);
187           item += cnt - 1;
188           merged += cnt - 1;
189           continue;
190         }
191 #endif
192 #ifdef SORT_ASSERT_UNIQUE
193       ASSERT(item == last_item-1 || P(compare)(item->key, item[1].key) < 0);
194 #endif
195       P(write_key)(out, item->key);
196 #ifdef SORT_VAR_DATA
197       bwrite(out, P(internal_get_data)(item->key), SORT_DATA_SIZE(*item->key));
198 #endif
199     }
200 #ifdef SORT_UNIFY
201   SORT_XTRACE(3, "Merging reduced %u records", merged);
202 #endif
203
204   return ctx->more_keys;
205 }
206
207 static u64
208 P(internal_estimate)(struct sort_context *ctx, struct sort_bucket *b UNUSED)
209 {
210 #ifdef SORT_VAR_KEY
211   uns avg = ALIGN_TO(sizeof(P(key))/4, CPU_STRUCT_ALIGN);       // Wild guess...
212 #else
213   uns avg = ALIGN_TO(sizeof(P(key)), CPU_STRUCT_ALIGN);
214 #endif
215   // We ignore the data part of records, it probably won't make the estimate much worse
216   size_t bufsize = ctx->big_buf_size;
217 #ifdef SORT_UNIFY_WORKSPACE             // FIXME: Or if radix-sorting
218   bufsize /= 2;
219 #endif
220   return (bufsize / (avg + sizeof(P(internal_item_t))) * avg);
221 }