Skip to content

Commit b92ac52

Browse files
localize cumSum vars (#7239)
* localize reused object cumSum + general vicinity cleanup * reverting to old draft for testing * gradual reintroduction of older changes * formatting * fix formatting warning * add coverage test case * change testnum * fix testnum --------- Co-authored-by: Benjamin Schwendinger <[email protected]>
1 parent 83fbcf1 commit b92ac52

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

inst/tests/tests.Rraw

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13701,6 +13701,10 @@ test(1962.054, forder(DT, ), 3:1)
1370113701

1370213702
test(1962.055, fsort(as.double(DT$a), internal = TRUE),
1370313703
error = 'Internal code should not be being called on type double')
13704+
# coverage for fsort #7239
13705+
set.seed(1)
13706+
x = as.double(sample(0:(2^20), size = 1e6, replace = TRUE))
13707+
test(1962.0551, sort(x), fsort(x))
1370413708

1370513709
l = as.list(DT)
1370613710
test(1962.056, setorder(l, a), error = 'x must be a data.frame or data.table')

src/fsort.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "data.table.h"
22

3-
#define INSERT_THRESH 200 // TODO: expose via api and test
3+
static const int INSERT_THRESH = 200; // TODO: expose via api and test
44

55
static void dinsert(double *x, const int n) { // TODO: if and when twiddled, double => ull
66
if (n<2) return;
@@ -43,17 +43,16 @@ static void dradix_r( // single-threaded recursive worker
4343
return;
4444
}
4545

46-
uint64_t cumSum=0;
47-
for (uint64_t i=0; cumSum<n; ++i) { // cumSum<n better than i<width as may return early
48-
uint64_t tmp;
49-
if ((tmp=counts[i])) { // don't cumulate through 0s, important below to save a wasteful memset to zero
46+
for (uint64_t i = 0, cumSum = 0; cumSum < n; i++) { // cumSum<n better than i<width as may return early
47+
uint64_t tmp = counts[i];
48+
if (tmp) { // don't cumulate through 0s, important below to save a wasteful memset to zero
5049
counts[i] = cumSum;
5150
cumSum += tmp;
5251
}
5352
} // leaves cumSum==n && 0<i && i<=width
5453

5554
tmp=in;
56-
for (uint64_t i=0; i<n; ++i) { // go forwards not backwards to give cpu pipeline better chance
55+
for (uint64_t i = 0; i<n; ++i) { // go forwards not backwards to give cpu pipeline better chance
5756
int thisx = (*(uint64_t *)tmp - minULL) >> fromBit & mask;
5857
working[ counts[thisx]++ ] = *tmp;
5958
tmp++;
@@ -71,8 +70,7 @@ static void dradix_r( // single-threaded recursive worker
7170
return;
7271
}
7372

74-
cumSum=0;
75-
for (int i=0; cumSum<n; ++i) { // again, cumSum<n better than i<width as it can return early
73+
for (uint64_t i = 0, cumSum = 0; cumSum < n; i++) { // again, cumSum<n better than i<width as it can return early
7674
if (counts[i] == 0) continue;
7775
uint64_t thisN = counts[i] - cumSum; // undo cummulate; i.e. diff
7876
if (thisN <= INSERT_THRESH) {

0 commit comments

Comments
 (0)