Skip to content

Commit 22045e5

Browse files
authored
removed redundant calls to INTEGER function (#7282)
It's especially bad in a tight loop
1 parent 266ea26 commit 22045e5

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/uniqlist.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,16 @@ SEXP uniqlengths(SEXP x, SEXP n) {
150150
// seems very similar to rbindlist.c:uniq_lengths. TODO: centralize into common function
151151
if (TYPEOF(x) != INTSXP) error(_("Input argument 'x' to 'uniqlengths' must be an integer vector"));
152152
if (TYPEOF(n) != INTSXP || length(n) != 1) error(_("Input argument 'n' to 'uniqlengths' must be an integer vector of length 1"));
153-
R_len_t len = length(x);
153+
const R_len_t len = length(x);
154154
SEXP ans = PROTECT(allocVector(INTSXP, len));
155+
156+
const int *px = INTEGER_RO(x);
157+
int *pans = INTEGER(ans);
158+
155159
for (R_len_t i=1; i<len; i++) {
156-
INTEGER(ans)[i-1] = INTEGER(x)[i] - INTEGER(x)[i-1];
160+
pans[i-1] = px[i] - px[i-1];
157161
}
158-
if (len>0) INTEGER(ans)[len-1] = INTEGER(n)[0] - INTEGER(x)[len-1] + 1;
162+
if (len>0) pans[len-1] = INTEGER(n)[0] - px[len-1] + 1;
159163
UNPROTECT(1);
160164
return(ans);
161165
}

0 commit comments

Comments
 (0)