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