-
-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add experimental C-based period apply
I became obsessed with improving the performance of period.apply() after seeing its abysmal performance in this StackOverflow question: https://stackoverflow.com/q/50062851/271616. The C code works for the most part, but tests are needed (obviously). The loop over the 'INDEX' (endpoints) was written very carefully to avoid unnecessary allocations. For example, the call remains the same for each period. The value bound to the crazily-named object changes each iteration, which avoids creating a new call for each period.
- Loading branch information
1 parent
2b04ada
commit 7c29359
Showing
3 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
# xts: eXtensible time-series | ||
# | ||
# Copyright (C) 2008 Jeffrey A. Ryan (FORTRAN implementation) | ||
# Copyright (C) 2018 Joshua M. Ulrich (C implementation) | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 2 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
|
||
#include<Rinternals.h> | ||
#include "xts.h" | ||
|
||
SEXP xts_period_apply(SEXP _data, SEXP _index, SEXP _function, SEXP _env) | ||
{ | ||
if (!isInteger(_index)) { | ||
error("index must be integer"); | ||
} | ||
|
||
int i; | ||
R_xlen_t n = xlength(_index); | ||
SEXP _result = PROTECT(allocVector(VECSXP, n)); | ||
SEXP _j = PROTECT(allocVector(INTSXP, ncols(_data))); | ||
SEXP _drop = PROTECT(ScalarLogical(0)); | ||
|
||
int *index = INTEGER(_index); | ||
for (i = 0; i < ncols(_data); i++) | ||
INTEGER(_j)[i] = i + 1; | ||
|
||
SEXP _idx0 = PROTECT(ScalarInteger(0)); | ||
SEXP _idx1 = PROTECT(ScalarInteger(0)); | ||
int *idx0 = INTEGER(_idx0); | ||
int *idx1 = INTEGER(_idx1); | ||
|
||
/* reprotect the subset object */ | ||
SEXP _xsubset; | ||
PROTECT_INDEX px; | ||
PROTECT_WITH_INDEX(_xsubset = R_NilValue, &px); | ||
|
||
/* subset object name */ | ||
SEXP _subsym = install("_.*crazy*._.*name*._"); | ||
defineVar(_subsym, _xsubset, _env); | ||
|
||
/* function call on subset */ | ||
SEXP _subcall = PROTECT(lang3(_function, _subsym, R_DotsSymbol)); | ||
|
||
int N = n - 1; | ||
for (i = 0; i < N; i++) { | ||
idx0[0] = index[i] + 1; | ||
idx1[0] = index[i + 1]; | ||
REPROTECT(_xsubset = extract_col(_data, _j, _drop, _idx0, _idx1), px); | ||
defineVar(_subsym, _xsubset, _env); | ||
SET_VECTOR_ELT(_result, i, eval(_subcall, _env)); | ||
} | ||
|
||
UNPROTECT(7); | ||
return _result; | ||
} |