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