[R] Very small performance tweaks (#9837)

This commit is contained in:
david-cortes 2023-12-04 11:40:45 +01:00 committed by GitHub
parent 381f1d3dc9
commit 9c56916fd7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -160,13 +160,6 @@ SEXP SafeMkChar(const char *c_str, SEXP continuation_token) {
using dmlc::BeginPtr; using dmlc::BeginPtr;
xgboost::Context const *DMatrixCtx(DMatrixHandle handle) {
CHECK_HANDLE();
auto p_m = static_cast<std::shared_ptr<xgboost::DMatrix> *>(handle);
CHECK(p_m);
return p_m->get()->Ctx();
}
XGB_DLL SEXP XGCheckNullPtr_R(SEXP handle) { XGB_DLL SEXP XGCheckNullPtr_R(SEXP handle) {
return ScalarLogical(R_ExternalPtrAddr(handle) == NULL); return ScalarLogical(R_ExternalPtrAddr(handle) == NULL);
} }
@ -318,6 +311,9 @@ XGB_DLL SEXP XGDMatrixSliceDMatrix_R(SEXP handle, SEXP idxset) {
int res_code; int res_code;
{ {
std::vector<int> idxvec(len); std::vector<int> idxvec(len);
#ifndef _MSC_VER
#pragma omp simd
#endif
for (R_xlen_t i = 0; i < len; ++i) { for (R_xlen_t i = 0; i < len; ++i) {
idxvec[i] = idxset_[i] - 1; idxvec[i] = idxset_[i] - 1;
} }
@ -375,6 +371,7 @@ XGB_DLL SEXP XGDMatrixSetStrFeatureInfo_R(SEXP handle, SEXP field, SEXP array) {
int res_code; int res_code;
{ {
std::vector<std::string> str_info; std::vector<std::string> str_info;
str_info.reserve(len);
for (size_t i = 0; i < len; ++i) { for (size_t i = 0; i < len; ++i) {
str_info.emplace_back(CHAR(VECTOR_ELT(str_info_holder, i))); str_info.emplace_back(CHAR(VECTOR_ELT(str_info_holder, i)));
} }
@ -457,9 +454,9 @@ XGB_DLL SEXP XGBoosterCreate_R(SEXP dmats) {
int res_code; int res_code;
{ {
std::vector<void*> dvec; std::vector<void*> dvec(len);
for (R_xlen_t i = 0; i < len; ++i) { for (R_xlen_t i = 0; i < len; ++i) {
dvec.push_back(R_ExternalPtrAddr(VECTOR_ELT(dmats, i))); dvec[i] = R_ExternalPtrAddr(VECTOR_ELT(dmats, i));
} }
res_code = XGBoosterCreate(BeginPtr(dvec), dvec.size(), &handle); res_code = XGBoosterCreate(BeginPtr(dvec), dvec.size(), &handle);
} }
@ -478,9 +475,9 @@ XGB_DLL SEXP XGBoosterCreateInEmptyObj_R(SEXP dmats, SEXP R_handle) {
int res_code; int res_code;
{ {
std::vector<void*> dvec; std::vector<void*> dvec(len);
for (R_xlen_t i = 0; i < len; ++i) { for (R_xlen_t i = 0; i < len; ++i) {
dvec.push_back(R_ExternalPtrAddr(VECTOR_ELT(dmats, i))); dvec[i] = R_ExternalPtrAddr(VECTOR_ELT(dmats, i));
} }
res_code = XGBoosterCreate(BeginPtr(dvec), dvec.size(), &handle); res_code = XGBoosterCreate(BeginPtr(dvec), dvec.size(), &handle);
} }
@ -552,15 +549,16 @@ XGB_DLL SEXP XGBoosterEvalOneIter_R(SEXP handle, SEXP iter, SEXP dmats, SEXP evn
int res_code; int res_code;
{ {
std::vector<void*> vec_dmats; std::vector<void*> vec_dmats(len);
std::vector<std::string> vec_names; std::vector<std::string> vec_names;
std::vector<const char*> vec_sptr; vec_names.reserve(len);
std::vector<const char*> vec_sptr(len);
for (R_xlen_t i = 0; i < len; ++i) { for (R_xlen_t i = 0; i < len; ++i) {
vec_dmats.push_back(R_ExternalPtrAddr(VECTOR_ELT(dmats, i))); vec_dmats[i] = R_ExternalPtrAddr(VECTOR_ELT(dmats, i));
vec_names.emplace_back(CHAR(VECTOR_ELT(evnames_lst, i))); vec_names.emplace_back(CHAR(VECTOR_ELT(evnames_lst, i)));
} }
for (R_xlen_t i = 0; i < len; ++i) { for (R_xlen_t i = 0; i < len; ++i) {
vec_sptr.push_back(vec_names[i].c_str()); vec_sptr[i] = vec_names[i].c_str();
} }
res_code = XGBoosterEvalOneIter(R_ExternalPtrAddr(handle), res_code = XGBoosterEvalOneIter(R_ExternalPtrAddr(handle),
asInteger(iter), asInteger(iter),
@ -598,11 +596,7 @@ XGB_DLL SEXP XGBoosterPredictFromDMatrix_R(SEXP handle, SEXP dmat, SEXP json_con
len *= out_shape[i]; len *= out_shape[i];
} }
r_out_result = PROTECT(allocVector(REALSXP, len)); r_out_result = PROTECT(allocVector(REALSXP, len));
auto ctx = xgboost::detail::BoosterCtx(R_ExternalPtrAddr(handle)); std::copy(out_result, out_result + len, REAL(r_out_result));
double *r_out_result_ = REAL(r_out_result);
xgboost::common::ParallelFor(len, ctx->Threads(), [&](xgboost::omp_ulong i) {
r_out_result_[i] = out_result[i];
});
SET_VECTOR_ELT(r_out, 0, r_out_shape); SET_VECTOR_ELT(r_out, 0, r_out_shape);
SET_VECTOR_ELT(r_out, 1, r_out_result); SET_VECTOR_ELT(r_out, 1, r_out_result);
@ -831,11 +825,7 @@ XGB_DLL SEXP XGBoosterFeatureScore_R(SEXP handle, SEXP json_config) {
} }
out_scores_sexp = PROTECT(allocVector(REALSXP, len)); out_scores_sexp = PROTECT(allocVector(REALSXP, len));
auto ctx = xgboost::detail::BoosterCtx(R_ExternalPtrAddr(handle)); std::copy(out_scores, out_scores + len, REAL(out_scores_sexp));
double *out_scores_sexp_ = REAL(out_scores_sexp);
xgboost::common::ParallelFor(len, ctx->Threads(), [&](xgboost::omp_ulong i) {
out_scores_sexp_[i] = out_scores[i];
});
SET_VECTOR_ELT(r_out, 0, out_features_sexp); SET_VECTOR_ELT(r_out, 0, out_features_sexp);
SET_VECTOR_ELT(r_out, 1, out_shape_sexp); SET_VECTOR_ELT(r_out, 1, out_shape_sexp);