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.
12 // FIXME: Add the hash here to save cache misses
15 #define ASORT_PREFIX(x) SORT_PREFIX(array_##x)
16 #define ASORT_KEY_TYPE P(internal_item_t)
17 #define ASORT_ELT(i) ary[i]
18 #define ASORT_LT(x,y) (P(compare)((x).key, (y).key) < 0)
19 #define ASORT_EXTRA_ARGS , P(internal_item_t) *ary
20 #include "lib/arraysort.h"
22 static inline void *P(internal_get_data)(P(key) *key)
24 uns ksize = SORT_KEY_SIZE(*key);
26 ksize = ALIGN_TO(ksize, CPU_STRUCT_ALIGN);
28 return (byte *) key + ksize;
31 static int P(internal)(struct sort_context *ctx, struct sort_bucket *bin, struct sort_bucket *bout, struct sort_bucket *bout_only)
33 sorter_alloc_buf(ctx);
34 struct fastbuf *in = sbuck_read(bin);
36 P(key) key, *keybuf = ctx->key_buf;
38 keybuf = ctx->key_buf = sorter_alloc(ctx, sizeof(key));
44 else if (!P(read_key)(in, &key))
48 if (sizeof(key) + 1024 + SORT_DATA_SIZE(key) > ctx->big_buf_half_size)
50 SORT_XTRACE("s-internal: Generating a giant run");
51 struct fastbuf *out = sbuck_write(bout); /* FIXME: Using a non-direct buffer would be nice here */
52 P(copy_data)(&key, in, out);
54 return 1; // We don't know, but 1 is always safe
58 size_t bufsize = ctx->big_buf_half_size; /* FIXME: In some cases, we can use the whole buffer */
59 bufsize = MIN((u64)bufsize, (u64)~0U * sizeof(P(internal_item_t))); // The number of records must fit in uns
61 SORT_XTRACE("s-internal: Reading (bufsize=%zd)", bufsize);
62 P(internal_item_t) *item_array = ctx->big_buf, *item = item_array, *last_item;
63 byte *end = (byte *) ctx->big_buf + bufsize;
66 uns ksize = SORT_KEY_SIZE(key);
68 uns ksize_aligned = ALIGN_TO(ksize, CPU_STRUCT_ALIGN);
70 uns ksize_aligned = ksize;
72 uns dsize = SORT_DATA_SIZE(key);
73 uns recsize = ALIGN_TO(ksize_aligned + dsize, CPU_STRUCT_ALIGN);
74 if (unlikely(sizeof(P(internal_item_t)) + recsize > (size_t)(end - (byte *) item)))
81 memcpy(end, &key, ksize);
83 breadb(in, end + ksize_aligned, dsize);
85 item->key = (P(key)*) end;
88 while (P(read_key)(in, &key));
91 uns count = last_item - item_array;
92 SORT_XTRACE("s-internal: Sorting %d items", count);
93 P(array_sort)(count, item_array);
95 SORT_XTRACE("s-internal: Writing");
98 struct fastbuf *out = sbuck_write(bout);
101 for (item = item_array; item < last_item; item++)
104 if (item < last_item - 1 && !P(compare)(item->key, item[1].key))
106 // Rewrite the item structures with just pointers to keys and place
107 // pointers to data in the secondary array.
108 P(key) **key_array = (void *) item;
109 void **data_array = (void **) ctx->big_buf_half;
110 key_array[0] = item[0].key;
111 data_array[0] = P(internal_get_data)(key_array[0]);
113 for (cnt=1; item+cnt < last_item && !P(compare)(key_array[0], item[cnt].key); cnt++)
115 key_array[cnt] = item[cnt].key;
116 data_array[cnt] = P(internal_get_data)(key_array[cnt]);
118 P(write_merged)(out, key_array, data_array, cnt, data_array+cnt);
124 #ifdef SORT_ASSERT_UNIQUE
125 ASSERT(item == last_item-1 || P(compare)(item->key, item[1].key) < 0);
127 P(write_key)(out, item->key);
129 bwrite(out, P(internal_get_data)(item->key), SORT_DATA_SIZE(*item->key));
133 SORT_XTRACE("Merging reduced %d records", merged);
136 return ctx->more_keys;