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 "lib/stkstring.h"
14 // FIXME: Add the hash here to save cache misses
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"
23 * The big_buf has the following layout:
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 * +--------------------------------+----------------------------------------------+
37 * | sequence of | padding | |
38 * | items +---------+ |
43 * +-------------------------------------------------------------------------------+
45 * (the data which are in different columns are never accessed simultaneously,
46 * so we use a single buffer for both)
49 static inline void *P(internal_get_data)(P(key) *key)
51 uns ksize = SORT_KEY_SIZE(*key);
53 ksize = ALIGN_TO(ksize, CPU_STRUCT_ALIGN);
55 return (byte *) key + ksize;
58 static inline size_t P(internal_workspace)(P(key) *key UNUSED)
64 #ifdef SORT_UNIFY_WORKSPACE
65 ws += SORT_UNIFY_WORKSPACE(*key);
67 #ifdef SORT_INTERNAL_RADIX
68 ws = MAX(ws, sizeof(P(internal_item_t)));
73 static int P(internal)(struct sort_context *ctx, struct sort_bucket *bin, struct sort_bucket *bout, struct sort_bucket *bout_only)
75 sorter_alloc_buf(ctx);
76 struct fastbuf *in = sbuck_read(bin);
78 P(key) key, *keybuf = ctx->key_buf;
80 keybuf = ctx->key_buf = sorter_alloc(ctx, sizeof(key));
86 else if (!P(read_key)(in, &key))
89 size_t bufsize = ctx->big_buf_size;
91 if (sizeof(key) + 2*CPU_PAGE_SIZE + SORT_DATA_SIZE(key) + P(internal_workspace)(&key) > bufsize)
93 SORT_XTRACE(3, "s-internal: Generating a giant run");
94 struct fastbuf *out = sbuck_write(bout);
95 P(copy_data)(&key, in, out);
97 return 1; // We don't know, but 1 is always safe
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;
107 uns ksize = SORT_KEY_SIZE(key);
109 uns ksize_aligned = ALIGN_TO(ksize, CPU_STRUCT_ALIGN);
111 uns ksize_aligned = ksize;
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
126 remains -= totalsize;
128 memcpy(end, &key, ksize);
130 breadb(in, end + ksize_aligned, dsize);
132 item->key = (P(key)*) end;
135 while (P(read_key)(in, &key));
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)",
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));
147 item_array = P(array_sort)(item_array, count,
148 #ifdef SORT_INTERNAL_RADIX
149 workspace, bin->hash_bits
154 ctx->total_int_time += get_timer(&timer);
156 SORT_XTRACE(4, "s-internal: Writing");
159 struct fastbuf *out = sbuck_write(bout);
161 uns merged UNUSED = 0;
162 for (item = item_array; item < last_item; item++)
165 if (item < last_item - 1 && !P(compare)(item->key, item[1].key))
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]);
174 for (cnt=1; item+cnt < last_item && !P(compare)(key_array[0], item[cnt].key); cnt++)
176 key_array[cnt] = item[cnt].key;
177 data_array[cnt] = P(internal_get_data)(key_array[cnt]);
179 P(write_merged)(out, key_array, data_array, cnt, data_array+cnt);
185 #ifdef SORT_ASSERT_UNIQUE
186 ASSERT(item == last_item-1 || P(compare)(item->key, item[1].key) < 0);
188 P(write_key)(out, item->key);
190 bwrite(out, P(internal_get_data)(item->key), SORT_DATA_SIZE(*item->key));
194 SORT_XTRACE(3, "Merging reduced %u records", merged);
197 return ctx->more_keys;
201 P(internal_estimate)(struct sort_context *ctx, struct sort_bucket *b UNUSED)
204 uns avg = ALIGN_TO(sizeof(P(key))/4, CPU_STRUCT_ALIGN); // Wild guess...
206 uns avg = ALIGN_TO(sizeof(P(key)), CPU_STRUCT_ALIGN);
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
213 return (bufsize / (avg + sizeof(P(internal_item_t))) * avg);