Commit 9bf2de1e authored by dmitryikh's avatar dmitryikh Committed by Guolin Ke
Browse files

Small optimizations and fixes (#1607)

* fix optimization rule

* preallocate vector in RowFunctionFromCSR
parent 244db078
...@@ -220,7 +220,7 @@ private: ...@@ -220,7 +220,7 @@ private:
} }
void ClearPredictBuffer(double* pred_buf, size_t buf_size, const std::vector<std::pair<int, double>>& features) { void ClearPredictBuffer(double* pred_buf, size_t buf_size, const std::vector<std::pair<int, double>>& features) {
if (features.size() < static_cast<size_t>(buf_size / 2)) { if (features.size() > static_cast<size_t>(buf_size / 2)) {
std::memset(pred_buf, 0, sizeof(double)*(buf_size)); std::memset(pred_buf, 0, sizeof(double)*(buf_size));
} else { } else {
int loop_size = static_cast<int>(features.size()); int loop_size = static_cast<int>(features.size());
......
...@@ -1382,6 +1382,9 @@ RowFunctionFromCSR(const void* indptr, int indptr_type, const int32_t* indices, ...@@ -1382,6 +1382,9 @@ RowFunctionFromCSR(const void* indptr, int indptr_type, const int32_t* indices,
std::vector<std::pair<int, double>> ret; std::vector<std::pair<int, double>> ret;
int64_t start = ptr_indptr[idx]; int64_t start = ptr_indptr[idx];
int64_t end = ptr_indptr[idx + 1]; int64_t end = ptr_indptr[idx + 1];
if (end - start > 0) {
ret.reserve(end - start);
}
for (int64_t i = start; i < end; ++i) { for (int64_t i = start; i < end; ++i) {
ret.emplace_back(indices[i], data_ptr[i]); ret.emplace_back(indices[i], data_ptr[i]);
} }
...@@ -1393,6 +1396,9 @@ RowFunctionFromCSR(const void* indptr, int indptr_type, const int32_t* indices, ...@@ -1393,6 +1396,9 @@ RowFunctionFromCSR(const void* indptr, int indptr_type, const int32_t* indices,
std::vector<std::pair<int, double>> ret; std::vector<std::pair<int, double>> ret;
int64_t start = ptr_indptr[idx]; int64_t start = ptr_indptr[idx];
int64_t end = ptr_indptr[idx + 1]; int64_t end = ptr_indptr[idx + 1];
if (end - start > 0) {
ret.reserve(end - start);
}
for (int64_t i = start; i < end; ++i) { for (int64_t i = start; i < end; ++i) {
ret.emplace_back(indices[i], data_ptr[i]); ret.emplace_back(indices[i], data_ptr[i]);
} }
...@@ -1407,6 +1413,9 @@ RowFunctionFromCSR(const void* indptr, int indptr_type, const int32_t* indices, ...@@ -1407,6 +1413,9 @@ RowFunctionFromCSR(const void* indptr, int indptr_type, const int32_t* indices,
std::vector<std::pair<int, double>> ret; std::vector<std::pair<int, double>> ret;
int64_t start = ptr_indptr[idx]; int64_t start = ptr_indptr[idx];
int64_t end = ptr_indptr[idx + 1]; int64_t end = ptr_indptr[idx + 1];
if (end - start > 0) {
ret.reserve(end - start);
}
for (int64_t i = start; i < end; ++i) { for (int64_t i = start; i < end; ++i) {
ret.emplace_back(indices[i], data_ptr[i]); ret.emplace_back(indices[i], data_ptr[i]);
} }
...@@ -1418,6 +1427,9 @@ RowFunctionFromCSR(const void* indptr, int indptr_type, const int32_t* indices, ...@@ -1418,6 +1427,9 @@ RowFunctionFromCSR(const void* indptr, int indptr_type, const int32_t* indices,
std::vector<std::pair<int, double>> ret; std::vector<std::pair<int, double>> ret;
int64_t start = ptr_indptr[idx]; int64_t start = ptr_indptr[idx];
int64_t end = ptr_indptr[idx + 1]; int64_t end = ptr_indptr[idx + 1];
if (end - start > 0) {
ret.reserve(end - start);
}
for (int64_t i = start; i < end; ++i) { for (int64_t i = start; i < end; ++i) {
ret.emplace_back(indices[i], data_ptr[i]); ret.emplace_back(indices[i], data_ptr[i]);
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment