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