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