]> mj.ucw.cz Git - libucw.git/blob - lib/sorter/s-fixint.h
7aafa7de8965dce8e096b9fc8a256b21a3c121fa
[libucw.git] / lib / sorter / s-fixint.h
1 /*
2  *      UCW Library -- Universal Sorter: Fixed-Size 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 #define ASORT_PREFIX(x) SORT_PREFIX(array_##x)
13 #define ASORT_KEY_TYPE P(key)
14 #define ASORT_LT(x,y) (P(compare)(&(x), &(y)) < 0)
15 #define ASORT_PAGE_ALIGNED
16 #ifdef SORT_INTERNAL_RADIX
17 #define ASORT_HASH(x) P(hash)(&(x))
18 #endif
19 #include "lib/sorter/array.h"
20
21 /*
22  *  This is a more efficient implementation of the internal sorter,
23  *  which runs under the following assumptions:
24  *
25  *     - the keys have fixed (and small) size
26  *     - no data are present after the key
27  *     - unification does not require any workspace
28  */
29
30 static size_t P(internal_workspace)(void)
31 {
32   size_t workspace = 0;
33 #ifdef SORT_UNIFY
34   workspace = sizeof(P(key) *);
35 #endif
36 #ifdef SORT_INTERNAL_RADIX
37   workspace = MAX(workspace, sizeof(P(key)));
38 #endif
39   return workspace;
40 }
41
42 static uns P(internal_num_keys)(struct sort_context *ctx)
43 {
44   size_t bufsize = ctx->big_buf_size;
45   size_t workspace = P(internal_workspace)();
46   if (workspace)
47     bufsize -= CPU_PAGE_SIZE;
48   u64 maxkeys = bufsize / (sizeof(P(key)) + workspace);
49   return MIN(maxkeys, ~0U);                                     // The number of records must fit in uns
50 }
51
52 static int P(internal)(struct sort_context *ctx, struct sort_bucket *bin, struct sort_bucket *bout, struct sort_bucket *bout_only)
53 {
54   sorter_alloc_buf(ctx);
55   struct fastbuf *in = sbuck_read(bin);
56   P(key) *buf = ctx->big_buf;
57   uns maxkeys = P(internal_num_keys)(ctx);
58
59   SORT_XTRACE(4, "s-fixint: Reading (maxkeys=%u, hash_bits=%d)", maxkeys, bin->hash_bits);
60   uns n = 0;
61   while (n < maxkeys && P(read_key)(in, &buf[n]))
62     n++;
63   if (!n)
64     return 0;
65   void *workspace UNUSED = ALIGN_PTR(&buf[n], CPU_PAGE_SIZE);
66
67   SORT_XTRACE(3, "s-fixint: Sorting %u items (%s items, %s workspace)",
68         n,
69         stk_fsize(n * sizeof(P(key))),
70         stk_fsize(n * P(internal_workspace)()));
71   timestamp_t timer;
72   init_timer(&timer);
73   buf = P(array_sort)(buf, n,
74 #ifdef SORT_INTERNAL_RADIX
75     workspace, bin->hash_bits
76 #else
77     NULL, 0
78 #endif
79     );
80   ctx->total_int_time += get_timer(&timer);
81
82   SORT_XTRACE(4, "s-fixint: Writing");
83   if (n < maxkeys)
84     bout = bout_only;
85   struct fastbuf *out = sbuck_write(bout);
86   bout->runs++;
87   uns merged UNUSED = 0;
88   for (uns i=0; i<n; i++)
89     {
90 #ifdef SORT_UNIFY
91       if (i < n-1 && !P(compare)(&buf[i], &buf[i+1]))
92         {
93           P(key) **keys = workspace;
94           uns n = 2;
95           keys[0] = &buf[i];
96           keys[1] = &buf[i+1];
97           while (!P(compare)(&buf[i], &buf[i+n]))
98             {
99               keys[n] = &buf[i+n];
100               n++;
101             }
102           P(write_merged)(out, keys, NULL, n, NULL);
103           merged += n - 1;
104           i += n - 1;
105           continue;
106         }
107 #endif
108 #ifdef SORT_ASSERT_UNIQUE
109       ASSERT(i == n-1 || P(compare)(&buf[i], &buf[i+1]) < 0);
110 #endif
111       P(write_key)(out, &buf[i]);
112     }
113 #ifdef SORT_UNIFY
114   SORT_XTRACE(3, "Merging reduced %d records", merged);
115 #endif
116
117   return (n == maxkeys);
118 }
119
120 static u64
121 P(internal_estimate)(struct sort_context *ctx, struct sort_bucket *b UNUSED)
122 {
123   return P(internal_num_keys)(ctx) * sizeof(P(key)) - 1;        // -1 since if the buffer is full, we don't recognize EOF
124 }