Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] optimize performance of percentile_cont function #14609

Merged
merged 1 commit into from
Dec 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 18 additions & 12 deletions be/src/exprs/agg/percentile_cont.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "column/vectorized_fwd.h"
#include "exprs/agg/aggregate.h"
#include "gutil/casts.h"
#include "util/orlp/pdqsort.h"

namespace starrocks::vectorized {

Expand Down Expand Up @@ -56,16 +57,21 @@ class PercentileContAggregateFunction final

const Slice slice = column->get(row_num).get_slice();
double rate = *reinterpret_cast<double*>(slice.data);
size_t items_size = *reinterpret_cast<size_t*>(slice.data + sizeof(double));
size_t second_size = *reinterpret_cast<size_t*>(slice.data + sizeof(double));
auto data_ptr = slice.data + sizeof(double) + sizeof(size_t);

std::vector<InputCppType> res;
std::vector<InputCppType>& vec = this->data(state).items;
res.resize(vec.size() + items_size);
auto second_start = reinterpret_cast<InputCppType*>(data_ptr);
auto second_end = reinterpret_cast<InputCppType*>(data_ptr + second_size * sizeof(InputCppType));

// TODO(murphy) reduce the copy overhead of merge algorithm
auto& output = this->data(state).items;
size_t first_size = output.size();
output.resize(first_size + second_size);
auto first_end = output.begin() + first_size;
std::copy(second_start, second_end, first_end);
// TODO: optimize it with SIMD bitonic merge
std::inplace_merge(output.begin(), first_end, output.end());

std::merge(vec.begin(), vec.end(), reinterpret_cast<InputCppType*>(data_ptr),
reinterpret_cast<InputCppType*>(data_ptr + items_size * sizeof(InputCppType)), res.begin());
this->data(state).items = std::move(res);
this->data(state).rate = rate;
}

Expand All @@ -83,17 +89,17 @@ class PercentileContAggregateFunction final
memcpy(bytes.data() + old_size + sizeof(double), &items_size, sizeof(size_t));
memcpy(bytes.data() + old_size + sizeof(double) + sizeof(size_t), this->data(state).items.data(),
items_size * sizeof(InputCppType));
std::sort(reinterpret_cast<InputCppType*>(bytes.data() + old_size + sizeof(double) + sizeof(size_t)),
reinterpret_cast<InputCppType*>(bytes.data() + old_size + sizeof(double) + sizeof(size_t) +
items_size * sizeof(InputCppType)));
pdqsort(false, reinterpret_cast<InputCppType*>(bytes.data() + old_size + sizeof(double) + sizeof(size_t)),
reinterpret_cast<InputCppType*>(bytes.data() + old_size + sizeof(double) + sizeof(size_t) +
items_size * sizeof(InputCppType)));

column->get_offset().emplace_back(new_size);
}

void finalize_to_column(FunctionContext* ctx, ConstAggDataPtr __restrict state, Column* to) const override {
using CppType = RunTimeCppType<PT>;
std::vector<CppType> new_vector = this->data(state).items;
std::sort(new_vector.begin(), new_vector.end());
pdqsort(false, new_vector.begin(), new_vector.end());
const double& rate = this->data(state).rate;

ResultColumnType* column = down_cast<ResultColumnType*>(to);
Expand Down Expand Up @@ -138,7 +144,7 @@ class PercentileContAggregateFunction final

double rate = ColumnHelper::get_const_value<TYPE_DOUBLE>(src[1]);
InputColumnType src_column = *down_cast<const InputColumnType*>(src[0].get());
std::sort(src_column.get_data().begin(), src_column.get_data().end());
pdqsort(false, src_column.get_data().begin(), src_column.get_data().end());

bytes.resize(old_size + sizeof(double) + sizeof(size_t) + chunk_size * sizeof(InputCppType));

Expand Down