#include "lib/stkstring.h"
+#ifdef SORT_INTERNAL_RADIX
+/* Keep copies of the items' hashes to save cache misses */
+#define SORT_COPY_HASH
+#endif
+
typedef struct {
P(key) *key;
- // FIXME: Add the hash here to save cache misses
+#ifdef SORT_COPY_HASH
+ P(hash_t) hash;
+#endif
} P(internal_item_t);
#define ASORT_PREFIX(x) SORT_PREFIX(array_##x)
#define ASORT_KEY_TYPE P(internal_item_t)
-#define ASORT_ELT(i) ary[i]
-#define ASORT_LT(x,y) (P(compare)((x).key, (y).key) < 0)
-#define ASORT_EXTRA_ARGS , P(internal_item_t) *ary
-#include "lib/arraysort.h"
+#ifdef SORT_COPY_HASH
+# ifdef SORT_INT
+# define ASORT_LT(x,y) ((x).hash < (y).hash) // In this mode, the hash is the value
+# else
+# define ASORT_LT(x,y) ((x).hash < (y).hash || (x).hash == (y).hash && P(compare)((x).key, (y).key) < 0)
+# endif
+#else
+# define ASORT_LT(x,y) (P(compare)((x).key, (y).key) < 0)
+#endif
+#ifdef SORT_INTERNAL_RADIX
+# ifdef SORT_COPY_HASH
+# define ASORT_HASH(x) (x).hash
+# else
+# define ASORT_HASH(x) P(hash)((x).key)
+# endif
+# ifdef SORT_LONG_HASH
+# define ASORT_LONG_HASH
+# endif
+#endif
+#include "lib/sorter/array.h"
/*
* The big_buf has the following layout:
#ifdef SORT_UNIFY_WORKSPACE
ws += SORT_UNIFY_WORKSPACE(*key);
#endif
-#if 0 /* FIXME: Shadow copy if radix-sorting */
- ws = MAX(ws, sizeof(P(key) *));
+#ifdef SORT_INTERNAL_RADIX
+ ws = MAX(ws, sizeof(P(internal_item_t)));
#endif
return ws;
}
breadb(in, end + ksize_aligned, dsize);
#endif
item->key = (P(key)*) end;
+#ifdef SORT_COPY_HASH
+ item->hash = P(hash)(item->key);
+#endif
item++;
}
while (P(read_key)(in, &key));
stk_fsize((byte*)ctx->big_buf + bufsize - end));
timestamp_t timer;
init_timer(&timer);
- P(array_sort)(count, item_array);
+ item_array = P(array_sort)(item_array, count,
+#ifdef SORT_INTERNAL_RADIX
+ workspace, bin->hash_bits
+#else
+ NULL, 0
+#endif
+ );
ctx->total_int_time += get_timer(&timer);
SORT_XTRACE(4, "s-internal: Writing");
#endif
return (bufsize / (avg + sizeof(P(internal_item_t))) * avg);
}
+
+#undef SORT_COPY_HASH