]> mj.ucw.cz Git - libucw.git/blob - lib/sorter/s-internal.h
Cleaned up sorter timings and added a final timing statistic.
[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 typedef struct {
11   P(key) *key;
12   // FIXME: Add the hash here to save cache misses
13 } P(internal_item_t);
14
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"
21
22 static inline void *P(internal_get_data)(P(key) *key)
23 {
24   uns ksize = SORT_KEY_SIZE(*key);
25 #ifdef SORT_UNIFY
26   ksize = ALIGN_TO(ksize, CPU_STRUCT_ALIGN);
27 #endif
28   return (byte *) key + ksize;
29 }
30
31 static size_t P(internal_buf_size)(struct sort_context *ctx)
32 {
33   size_t bufsize = ctx->big_buf_half_size;      /* FIXME: In some cases, we can use the whole buffer */
34 #ifdef CPU_64BIT_POINTERS
35   bufsize = MIN((u64)bufsize, (u64)~0U * sizeof(P(internal_item_t)));   // The number of records must fit in uns
36 #endif
37   return bufsize;
38 }
39
40 static int P(internal)(struct sort_context *ctx, struct sort_bucket *bin, struct sort_bucket *bout, struct sort_bucket *bout_only)
41 {
42   sorter_alloc_buf(ctx);
43   struct fastbuf *in = sbuck_read(bin);
44
45   P(key) key, *keybuf = ctx->key_buf;
46   if (!keybuf)
47     keybuf = ctx->key_buf = sorter_alloc(ctx, sizeof(key));
48   if (ctx->more_keys)
49     {
50       key = *keybuf;
51       ctx->more_keys = 0;
52     }
53   else if (!P(read_key)(in, &key))
54     return 0;
55
56   size_t bufsize = P(internal_buf_size)(ctx);
57 #ifdef SORT_VAR_DATA
58   if (sizeof(key) + 1024 + SORT_DATA_SIZE(key) > bufsize)
59     {
60       SORT_XTRACE(3, "s-internal: Generating a giant run");
61       struct fastbuf *out = sbuck_write(bout);
62       P(copy_data)(&key, in, out);
63       bout->runs++;
64       return 1;                         // We don't know, but 1 is always safe
65     }
66 #endif
67
68   SORT_XTRACE(3, "s-internal: Reading (bufsize=%zd)", bufsize);
69   P(internal_item_t) *item_array = ctx->big_buf, *item = item_array, *last_item;
70   byte *end = (byte *) ctx->big_buf + bufsize;
71   do
72     {
73       uns ksize = SORT_KEY_SIZE(key);
74 #ifdef SORT_UNIFY
75       uns ksize_aligned = ALIGN_TO(ksize, CPU_STRUCT_ALIGN);
76 #else
77       uns ksize_aligned = ksize;
78 #endif
79       uns dsize = SORT_DATA_SIZE(key);
80       uns recsize = ALIGN_TO(ksize_aligned + dsize, CPU_STRUCT_ALIGN);
81       if (unlikely(sizeof(P(internal_item_t)) + recsize > (size_t)(end - (byte *) item)))
82         {
83           ctx->more_keys = 1;
84           *keybuf = key;
85           break;
86         }
87       end -= recsize;
88       memcpy(end, &key, ksize);
89 #ifdef SORT_VAR_DATA
90       breadb(in, end + ksize_aligned, dsize);
91 #endif
92       item->key = (P(key)*) end;
93       item++;
94     }
95   while (P(read_key)(in, &key));
96   last_item = item;
97
98   uns count = last_item - item_array;
99   SORT_XTRACE(3, "s-internal: Sorting %u items", count);
100   timestamp_t timer;
101   init_timer(&timer);
102   P(array_sort)(count, item_array);
103   ctx->total_int_time += get_timer(&timer);
104
105   SORT_XTRACE(3, "s-internal: Writing");
106   if (!ctx->more_keys)
107     bout = bout_only;
108   struct fastbuf *out = sbuck_write(bout);
109   bout->runs++;
110   uns merged UNUSED = 0;
111   for (item = item_array; item < last_item; item++)
112     {
113 #ifdef SORT_UNIFY
114       if (item < last_item - 1 && !P(compare)(item->key, item[1].key))
115         {
116           // Rewrite the item structures with just pointers to keys and place
117           // pointers to data in the secondary array.
118           P(key) **key_array = (void *) item;
119           void **data_array = (void **) ctx->big_buf_half;
120           key_array[0] = item[0].key;
121           data_array[0] = P(internal_get_data)(key_array[0]);
122           uns cnt;
123           for (cnt=1; item+cnt < last_item && !P(compare)(key_array[0], item[cnt].key); cnt++)
124             {
125               key_array[cnt] = item[cnt].key;
126               data_array[cnt] = P(internal_get_data)(key_array[cnt]);
127             }
128           P(write_merged)(out, key_array, data_array, cnt, data_array+cnt);
129           item += cnt - 1;
130           merged += cnt - 1;
131           continue;
132         }
133 #endif
134 #ifdef SORT_ASSERT_UNIQUE
135       ASSERT(item == last_item-1 || P(compare)(item->key, item[1].key) < 0);
136 #endif
137       P(write_key)(out, item->key);
138 #ifdef SORT_VAR_DATA
139       bwrite(out, P(internal_get_data)(item->key), SORT_DATA_SIZE(*item->key));
140 #endif
141     }
142 #ifdef SORT_UNIFY
143   SORT_XTRACE(3, "Merging reduced %u records", merged);
144 #endif
145
146   return ctx->more_keys;
147 }
148
149 static u64
150 P(internal_estimate)(struct sort_context *ctx, struct sort_bucket *b UNUSED)
151 {
152 #ifdef SORT_VAR_KEY
153   uns avg = ALIGN_TO(sizeof(P(key))/4, CPU_STRUCT_ALIGN);       // Wild guess...
154 #else
155   uns avg = ALIGN_TO(sizeof(P(key)), CPU_STRUCT_ALIGN);
156 #endif
157   // We ignore the data part of records, it probably won't make the estimate much worse
158   return (P(internal_buf_size)(ctx) / (avg + sizeof(P(internal_item_t))) * avg);
159 }