Skip to content

Commit 50f4e6c

Browse files
committed
add priority queue
1 parent e4128ff commit 50f4e6c

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

src/forder.c

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,6 @@ SEXP forder(SEXP DT, SEXP by, SEXP retGrpArg, SEXP sortGroupsArg, SEXP ascArg, S
520520
if (sortType!=1 && sortType!=-1)
521521
STOP(_("Item %d of order (ascending/descending) is %d. Must be +1 or -1."), col+1, sortType);
522522
}
523-
//Rprintf(_("sortType = %d\n"), sortType);
524523
switch(TYPEOF(x)) {
525524
case INTSXP : case LGLSXP : // TODO skip LGL and assume range [0,1]
526525
range_i32(INTEGER(x), nrow, &min, &max, &na_count);
@@ -828,7 +827,6 @@ static bool sort_ugrp(uint8_t *x, const int n)
828827
void radix_r(const int from, const int to, const int radix) {
829828
TBEG();
830829
const int my_n = to-from+1;
831-
Rprintf("from:%d to:%d radix:%d\n", from, to, radix);
832830
if (my_n==1) { // minor TODO: batch up the 1's instead in caller (and that's only needed when retgrp anyway)
833831
push(&my_n, 1);
834832
TEND(5);
@@ -851,7 +849,6 @@ void radix_r(const int from, const int to, const int radix) {
851849
// to use it once. However, o's type is uint8_t so many moves within this max-256 vector should be faster than many moves in osub (4 byte or 8 byte ints) [1 byte
852850
// type is always aligned]
853851
bool skip = true;
854-
//Rprintf("sortType: %d\n", sortType);
855852
if (sortType!=0) {
856853
// always ascending as desc (sortType==-1) was dealt with in WRITE_KEY
857854
int start = 1;
@@ -937,8 +934,6 @@ void radix_r(const int from, const int to, const int radix) {
937934
else my_gs[ngrp]++;
938935
}
939936
ngrp++;
940-
for(int i=0; i<ngrp; i++) Rprintf("my_gs[%d]: %d\n", i, my_gs[i]);
941-
for(int i=0; i<my_n; i++) Rprintf("my_key[%d]: %d\n", i, my_key[i]);
942937
TEND(9)
943938
if (radix+1==nradix || ngrp==my_n) { // ngrp==my_n => unique groups all size 1 and we can stop recursing now
944939
push(my_gs, ngrp);
@@ -1267,13 +1262,18 @@ void radix_r(const int from, const int to, const int radix) {
12671262
}
12681263

12691264
void pushState(State s) {
1270-
Rprintf("from:%d to:%d radix:%d\n", s.from, s.to, s.radix);
12711265
if (rear == queuesize) {
12721266
queuesize *= 2;
12731267
queue = (State*)realloc(queue, queuesize * sizeof(State));
12741268
if (!queue) STOP(_("Unable to reallocate queue of size=%d for iterative radix sort. Please report to data.table issue tracker."), queuesize);
12751269
}
1276-
queue[rear++] = s;
1270+
int pos;
1271+
for (pos = rear-1; pos>=0; pos--) {
1272+
if (s.from < queue[pos].from) queue[pos+1] = queue[pos];
1273+
else break;
1274+
}
1275+
queue[pos+1] = s;
1276+
rear++;
12771277
}
12781278

12791279
State popState() {
@@ -1283,7 +1283,7 @@ State popState() {
12831283
void radix_i(int from, int to, int radix) {
12841284
TBEG();
12851285
front = rear = 0;
1286-
queuesize = nradix;
1286+
queuesize = nradix > nrow ? nradix : nrow;
12871287
queue = (State*)malloc(queuesize * sizeof(State));
12881288
if (!queue) STOP(_("Unable to allocate queue of size=%d for iterative radix sort. Please report to data.table issue tracker."), queuesize);
12891289

@@ -1301,7 +1301,6 @@ void radix_i(int from, int to, int radix) {
13011301
}
13021302
else if (my_n<=256) {
13031303
// if nth==1
1304-
//Rprintf(_("insert clause: radix=%d, my_n=%d, from=%d, to=%d\n"), radix, my_n, from, to);
13051304
// insert sort with some twists:
13061305
// i) detects if grouped; if sortType==0 can then skip
13071306
// ii) keeps group appearance order at byte level to minimize movement
@@ -1316,7 +1315,6 @@ void radix_i(int from, int to, int radix) {
13161315
// to use it once. However, o's type is uint8_t so many moves within this max-256 vector should be faster than many moves in osub (4 byte or 8 byte ints) [1 byte
13171316
// type is always aligned]
13181317
bool skip = true;
1319-
//Rprintf("sortType: %d\n", sortType);
13201318
if (sortType!=0) {
13211319
// always ascending as desc (sortType==-1) was dealt with in WRITE_KEY
13221320
int start = 1;
@@ -1357,7 +1355,6 @@ void radix_i(int from, int to, int radix) {
13571355
skip = false;
13581356
break;
13591357
}
1360-
//Rprintf("skip: %d\n", skip);
13611358
if (!skip) {
13621359
// and now it's worth populating o[]
13631360
for (int i=0; i<third; i++) o[i] = i;
@@ -1403,8 +1400,6 @@ void radix_i(int from, int to, int radix) {
14031400
else my_gs[ngrp]++;
14041401
}
14051402
ngrp++;
1406-
for(int i=0; i<ngrp; i++) Rprintf("my_gs[%d]: %d\n", i, my_gs[i]);
1407-
for(int i=0; i<my_n; i++) Rprintf("my_key[%d]: %d\n", i, my_key[i]);
14081403
TEND(9)
14091404
if (radix+1==nradix || ngrp==my_n) { // ngrp==my_n => unique groups all size 1 and we can stop recursing now
14101405
push(my_gs, ngrp);
@@ -1417,7 +1412,6 @@ void radix_i(int from, int to, int radix) {
14171412
continue;
14181413
}
14191414
else if (my_n<=UINT16_MAX) { // UINT16_MAX==65535 (important not 65536)
1420-
// if (nth==1) Rprintf(_("counting clause: radix=%d, my_n=%d\n"), radix, my_n);
14211415
uint16_t my_counts[256] = {0}; // Needs to be all-0 on entry. This ={0} initialization should be fast as it's on stack. Otherwise, we have to manage
14221416
// a stack of counts anyway since this is called recursively and these counts are needed to make the recursive calls.
14231417
// This thread-private stack alloc has no chance of false sharing and gives omp and compiler best chance.

0 commit comments

Comments
 (0)