Skip to content

Commit

Permalink
[Enchancement](sort) some optimization for full sort (#45824)
Browse files Browse the repository at this point in the history
### What problem does this PR solve?

![QQ_1734963592703](https://github.com/user-attachments/assets/d5a93341-d000-4475-8435-792b521a8d00)

### Release note

None

### Check List (For Author)

- Test <!-- At least one of them must be included. -->
    - [ ] Regression test
    - [ ] Unit Test
    - [ ] Manual test (add detailed scripts or steps below)
    - [ ] No need to test or manual test. Explain why:
- [ ] This is a refactor/code format and no logic has been changed.
        - [ ] Previous test can cover this change.
        - [ ] No code files have been changed.
        - [ ] Other reason <!-- Add your reason?  -->

- Behavior changed:
    - [ ] No.
    - [ ] Yes. <!-- Explain the behavior change -->

- Does this need documentation?
    - [ ] No.
- [ ] Yes. <!-- Add document PR link here. eg:
apache/doris-website#1214 -->

### Check List (For Reviewer who merge this PR)

- [ ] Confirm the release note
- [ ] Confirm test cases
- [ ] Confirm document
- [ ] Add branch pick label <!-- Add branch pick label that this PR
should merge into -->
  • Loading branch information
BiteTheDDDDt authored Dec 25, 2024
1 parent ac385e5 commit 041db85
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 28 deletions.
32 changes: 12 additions & 20 deletions be/src/vec/common/sort/sorter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,6 @@ Status MergeSorterState::merge_sort_read(doris::vectorized::Block* block, int ba

Status MergeSorterState::_merge_sort_read_impl(int batch_size, doris::vectorized::Block* block,
bool* eos) {
if (priority_queue_.empty()) {
*eos = true;
return Status::OK();
}
size_t num_columns = priority_queue_.top().impl->block->columns();

MutableBlock m_block = VectorizedUtils::build_mutable_mem_reuse_block(
Expand All @@ -120,13 +116,15 @@ Status MergeSorterState::_merge_sort_read_impl(int batch_size, doris::vectorized

/// Take rows from queue in right order and push to 'merged'.
size_t merged_rows = 0;
while (!priority_queue_.empty()) {
// process single element queue on merge_sort_read()
while (priority_queue_.size() > 1 && merged_rows < batch_size) {
auto current = priority_queue_.top();
priority_queue_.pop();

if (offset_ == 0) {
for (size_t i = 0; i < num_columns; ++i)
for (size_t i = 0; i < num_columns; ++i) {
merged_columns[i]->insert_from(*current->block->get_columns()[i], current->pos);
}
++merged_rows;
} else {
offset_--;
Expand All @@ -136,18 +134,9 @@ Status MergeSorterState::_merge_sort_read_impl(int batch_size, doris::vectorized
current->next();
priority_queue_.push(current);
}

if (merged_rows == batch_size) {
break;
}
}
block->set_columns(std::move(merged_columns));

if (merged_rows == 0) {
*eos = true;
return Status::OK();
}

block->set_columns(std::move(merged_columns));
return Status::OK();
}

Expand Down Expand Up @@ -217,9 +206,15 @@ FullSorter::FullSorter(VSortExecExprs& vsort_exec_exprs, int limit, int64_t offs

Status FullSorter::append_block(Block* block) {
DCHECK(block->rows() > 0);

if (_reach_limit() && block->bytes() > _state->unsorted_block_->allocated_bytes() -
_state->unsorted_block_->bytes()) {
RETURN_IF_ERROR(_do_sort());
}

{
SCOPED_TIMER(_merge_block_timer);
auto& data = _state->unsorted_block_->get_columns_with_type_and_name();
const auto& data = _state->unsorted_block_->get_columns_with_type_and_name();
const auto& arrival_data = block->get_columns_with_type_and_name();
auto sz = block->rows();
for (int i = 0; i < data.size(); ++i) {
Expand All @@ -232,9 +227,6 @@ Status FullSorter::append_block(Block* block) {
}
block->clear_column_data();
}
if (_reach_limit()) {
RETURN_IF_ERROR(_do_sort());
}
return Status::OK();
}

Expand Down
10 changes: 2 additions & 8 deletions be/src/vec/common/sort/sorter.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,21 +177,15 @@ class FullSorter final : public Sorter {

private:
bool _reach_limit() {
return _state->unsorted_block_->rows() > buffered_block_size_ ||
_state->unsorted_block_->bytes() > buffered_block_bytes_;
return _state->unsorted_block_->allocated_bytes() >= buffered_block_bytes_;
}

Status _do_sort();

std::unique_ptr<MergeSorterState> _state;

static constexpr size_t INITIAL_BUFFERED_BLOCK_SIZE = 1024 * 1024;
static constexpr size_t INITIAL_BUFFERED_BLOCK_BYTES = 64 << 20;
static constexpr size_t INITIAL_BUFFERED_BLOCK_BYTES = 64 * 1024 * 1024;

static constexpr size_t SPILL_BUFFERED_BLOCK_SIZE = 4 * 1024 * 1024;
static constexpr size_t SPILL_BUFFERED_BLOCK_BYTES = 256 << 20;

size_t buffered_block_size_ = INITIAL_BUFFERED_BLOCK_SIZE;
size_t buffered_block_bytes_ = INITIAL_BUFFERED_BLOCK_BYTES;
};

Expand Down

0 comments on commit 041db85

Please sign in to comment.