2 * UCW Library -- Universal Sorter: Internal Sorting Module
4 * (c) 2007 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
10 #include "ucw/stkstring.h"
12 #ifdef SORT_INTERNAL_RADIX
13 /* Keep copies of the items' hashes to save cache misses */
14 #define SORT_COPY_HASH
24 #define ASORT_PREFIX(x) SORT_PREFIX(array_##x)
25 #define ASORT_KEY_TYPE P(internal_item_t)
28 # define ASORT_LT(x,y) ((x).hash < (y).hash) // In this mode, the hash is the value
30 # define ASORT_LT(x,y) ((x).hash < (y).hash || (x).hash == (y).hash && P(compare)((x).key, (y).key) < 0)
33 # define ASORT_LT(x,y) (P(compare)((x).key, (y).key) < 0)
35 #ifdef SORT_INTERNAL_RADIX
36 # ifdef SORT_COPY_HASH
37 # define ASORT_HASH(x) (x).hash
39 # define ASORT_HASH(x) P(hash)((x).key)
41 # ifdef SORT_LONG_HASH
42 # define ASORT_LONG_HASH
45 #include "ucw/sorter/array.h"
48 * The big_buf has the following layout:
50 * +-------------------------------------------------------------------------------+
51 * | array of internal_item's |
52 * +-------------------------------------------------------------------------------+
53 * | padding to make the following part page-aligned |
54 * +--------------------------------+----------------------------------------------+
55 * | shadow copy of item array | array of pointers to data for write_merged() |
56 * | used if radix-sorting +----------------------------------------------+
57 * | | workspace for write_merged() |
58 * +--------------------------------+----------------------------------------------+
62 * | sequence of | padding | |
63 * | items +---------+ |
68 * +-------------------------------------------------------------------------------+
70 * (the data which are in different columns are never accessed simultaneously,
71 * so we use a single buffer for both)
74 static inline void *P(internal_get_data)(P(key) *key)
76 uns ksize = SORT_KEY_SIZE(*key);
78 ksize = ALIGN_TO(ksize, CPU_STRUCT_ALIGN);
80 return (byte *) key + ksize;
83 static inline size_t P(internal_workspace)(P(key) *key UNUSED)
89 #ifdef SORT_UNIFY_WORKSPACE
90 ws += SORT_UNIFY_WORKSPACE(*key);
92 #ifdef SORT_INTERNAL_RADIX
93 ws = MAX(ws, sizeof(P(internal_item_t)));
98 static int P(internal)(struct sort_context *ctx, struct sort_bucket *bin, struct sort_bucket *bout, struct sort_bucket *bout_only)
100 sorter_alloc_buf(ctx);
101 struct fastbuf *in = sbuck_read(bin);
103 P(key) key, *keybuf = ctx->key_buf;
105 keybuf = ctx->key_buf = sorter_alloc(ctx, sizeof(key));
111 else if (!P(read_key)(in, &key))
114 size_t bufsize = ctx->big_buf_size;
116 if (sizeof(key) + 2*CPU_PAGE_SIZE + SORT_DATA_SIZE(key) + P(internal_workspace)(&key) > bufsize)
118 SORT_XTRACE(4, "s-internal: Generating a giant run");
119 struct fastbuf *out = sbuck_write(bout);
120 P(copy_data)(&key, in, out);
122 return 1; // We don't know, but 1 is always safe
126 SORT_XTRACE(5, "s-internal: Reading");
127 P(internal_item_t) *item_array = ctx->big_buf, *item = item_array, *last_item;
128 byte *end = (byte *) ctx->big_buf + bufsize;
129 size_t remains = bufsize - CPU_PAGE_SIZE;
132 uns ksize = SORT_KEY_SIZE(key);
134 uns ksize_aligned = ALIGN_TO(ksize, CPU_STRUCT_ALIGN);
136 uns ksize_aligned = ksize;
138 uns dsize = SORT_DATA_SIZE(key);
139 uns recsize = ALIGN_TO(ksize_aligned + dsize, CPU_STRUCT_ALIGN);
140 size_t totalsize = recsize + sizeof(P(internal_item_t)) + P(internal_workspace)(&key);
141 if (unlikely(totalsize > remains
142 #ifdef CPU_64BIT_POINTERS
143 || item >= item_array + ~0U // The number of items must fit in an uns
151 remains -= totalsize;
153 memcpy(end, &key, ksize);
155 breadb(in, end + ksize_aligned, dsize);
157 item->key = (P(key)*) end;
158 #ifdef SORT_COPY_HASH
159 item->hash = P(hash)(item->key);
163 while (P(read_key)(in, &key));
166 uns count = last_item - item_array;
167 void *workspace UNUSED = ALIGN_PTR(last_item, CPU_PAGE_SIZE);
168 SORT_XTRACE(4, "s-internal: Read %u items (%s items, %s workspace, %s data)",
170 stk_fsize((byte*)last_item - (byte*)item_array),
171 stk_fsize(end - (byte*)last_item - remains),
172 stk_fsize((byte*)ctx->big_buf + bufsize - end));
175 item_array = P(array_sort)(item_array, count
176 #ifdef SORT_INTERNAL_RADIX
177 , workspace, bin->hash_bits
180 if ((void *)item_array != ctx->big_buf)
181 workspace = ctx->big_buf;
182 last_item = item_array + count;
183 ctx->total_int_time += get_timer(&timer);
185 SORT_XTRACE(5, "s-internal: Writing");
188 struct fastbuf *out = sbuck_write(bout);
190 uns merged UNUSED = 0;
191 for (item = item_array; item < last_item; item++)
194 if (item < last_item - 1 && !P(compare)(item->key, item[1].key))
196 // Rewrite the item structures with just pointers to keys and place
197 // pointers to data in the workspace.
198 P(key) **key_array = (void *) item;
199 void **data_array = workspace;
200 key_array[0] = item[0].key;
201 data_array[0] = P(internal_get_data)(key_array[0]);
203 for (cnt=1; item+cnt < last_item && !P(compare)(key_array[0], item[cnt].key); cnt++)
205 key_array[cnt] = item[cnt].key;
206 data_array[cnt] = P(internal_get_data)(key_array[cnt]);
208 P(write_merged)(out, key_array, data_array, cnt, data_array+cnt);
214 #ifdef SORT_ASSERT_UNIQUE
215 ASSERT(item == last_item-1 || P(compare)(item->key, item[1].key) < 0);
217 P(write_key)(out, item->key);
219 bwrite(out, P(internal_get_data)(item->key), SORT_DATA_SIZE(*item->key));
223 SORT_XTRACE(4, "Merging reduced %u records", merged);
226 return ctx->more_keys;
230 P(internal_estimate)(struct sort_context *ctx, struct sort_bucket *b UNUSED)
232 // Most of this is just wild guesses
234 uns avg = ALIGN_TO(sizeof(P(key))/4, CPU_STRUCT_ALIGN);
236 uns avg = ALIGN_TO(sizeof(P(key)), CPU_STRUCT_ALIGN);
240 ws += sizeof(void *);
242 #ifdef SORT_UNIFY_WORKSPACE
245 #ifdef SORT_INTERNAL_RADIX
246 ws = MAX(ws, sizeof(P(internal_item_t)));
248 // We ignore the data part of records, it probably won't make the estimate much worse
249 return (ctx->big_buf_size / (avg + ws + sizeof(P(internal_item_t))) * avg);
252 #undef SORT_COPY_HASH