Skip to content

Commit

Permalink
[Enhancement] optimize performance of percentile_cont function (#14609)
Browse files Browse the repository at this point in the history
(cherry picked from commit 4e3418c)

# Conflicts:
#	be/src/exprs/agg/percentile_cont.h
  • Loading branch information
mofeiatwork authored and mergify[bot] committed Dec 6, 2022
1 parent 29ed438 commit 43bcb32
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 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,27 @@ 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);

<<<<<<< HEAD
vector<InputCppType> res;
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());
>>>>>>> 4e3418cab ([Enhancement] optimize performance of percentile_cont function (#14609))

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 +95,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 +150,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

0 comments on commit 43bcb32

Please sign in to comment.