Skip to content

Use tournament loser tree for k-way sort-merging, increase merge speed by 50% - #4301

Merged
alamb merged 1 commit into
apache:masterfrom
richox:tournament-merging
Nov 28, 2022
Merged

Use tournament loser tree for k-way sort-merging, increase merge speed by 50%#4301
alamb merged 1 commit into
apache:masterfrom
richox:tournament-merging

Conversation

@richox

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Closes#4300.

Rationale for this change

What changes are included in this PR?

Are these changes tested?

running benchmarks with cargo bench --bench merge:

merge i64 time: [8.0959 ms 8.2001 ms 8.3260 ms]
change: [-58.629% -57.906% -57.138%] (p = 0.00 < 0.05)
Performance has improved.
Found 11 outliers among 100 measurements (11.00%)
4 (4.00%) high mild
7 (7.00%) high severe
merge f64 time: [8.2829 ms 8.3378 ms 8.4028 ms]
change: [-56.127% -55.475% -54.829%] (p = 0.00 < 0.05)
Performance has improved.
Found 10 outliers among 100 measurements (10.00%)
5 (5.00%) high mild
5 (5.00%) high severe
merge utf8 low cardinality
time: [7.5418 ms 7.6425 ms 7.7538 ms]
change: [-65.897% -65.335% -64.703%] (p = 0.00 < 0.05)
Performance has improved.
Found 13 outliers among 100 measurements (13.00%)
7 (7.00%) high mild
6 (6.00%) high severe
merge utf8 high cardinality
time: [9.9438 ms 10.094 ms 10.257 ms]
change: [-52.043% -51.155% -50.249%] (p = 0.00 < 0.05)
Performance has improved.
Found 8 outliers among 100 measurements (8.00%)
4 (4.00%) high mild
4 (4.00%) high severe
merge utf8 tuple time: [20.197 ms 20.493 ms 20.826 ms]
change: [-40.663% -39.505% -38.279%] (p = 0.00 < 0.05)
Performance has improved.
Found 10 outliers among 100 measurements (10.00%)
6 (6.00%) high mild
4 (4.00%) high severe
merge utf8 dictionary time: [5.1537 ms 5.2135 ms 5.2788 ms]
change: [-72.062% -71.616% -71.137%] (p = 0.00 < 0.05)
Performance has improved.
Found 8 outliers among 100 measurements (8.00%)
7 (7.00%) high mild
1 (1.00%) high severe
merge utf8 dictionary tuple
time: [7.9585 ms 8.0592 ms 8.1797 ms]
change: [-63.981% -63.324% -62.678%] (p = 0.00 < 0.05)
Performance has improved.
Found 11 outliers among 100 measurements (11.00%)
6 (6.00%) high mild
5 (5.00%) high severe
merge mixed utf8 dictionary tuple
time: [14.528 ms 14.669 ms 14.829 ms]
change: [-43.886% -43.201% -42.489%] (p = 0.00 < 0.05)
Performance has improved.
Found 10 outliers among 100 measurements (10.00%)
5 (5.00%) high mild
5 (5.00%) high severe
merge mixed tuple time: [17.824 ms 18.068 ms 18.333 ms]
change: [-39.957% -38.888% -37.748%] (p = 0.00 < 0.05)
Performance has improved.
Found 5 outliers among 100 measurements (5.00%)
4 (4.00%) high mild
1 (1.00%) high severe

Are there any user-facing changes?

@github-actionsgithub-actionsBot added the core Core DataFusion crate label Nov 21, 2022
@alamb

Copy link
Copy Markdown
Contributor

cc @tustvold

@tustvoldtustvold left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks really good, thank you, I left some comments on how to make the implementation a little easier to follow, but I would be happy for this to go in as is.


if self.in_progress.len() == self.batch_size {
return Poll::Ready(Some(self.build_record_batch()));
let mut cmp_node = (num_streams + winner) / 2;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
letmut cmp_node = (num_streams + winner) / 2;
// Replace overall winner by walking tree of losers
letmut cmp_node = (num_streams + winner) / 2;

/// the loser nodes
loser_tree: Vec<usize>,

/// Identify whether the loser tree is adjusted

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Identify whether the loser tree is adjusted
/// Identify whether the most recently yielded overall winner has been replaced
/// within the loser tree, a value of `false` indicates that they overall winner
/// has been yielded but the loser tree has not been updated

Or something to make it clearer what adjusted actually means.

FWIW a boolean of should_replace_winner or something might be clearer

self.current()
.cmp(&other.current())
.then_with(|| self.stream_idx.cmp(&other.stream_idx))
match (self.is_finished(), other.is_finished()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
match(self.is_finished(), other.is_finished()){
// Order finished cursors last
match(self.is_finished(), other.is_finished()){


// Init all cursors and the loser tree in the first poll
if self.loser_tree.is_empty() {
// Ensure all non-exhausted streams have a cursor from which

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be easier to follow if this method were split into a method called init_loser_tree with a doc comment explaining what it does

self.cursor_finished[stream_idx] = true;
// Adjust the loser tree if necessary
if !self.loser_tree_adjusted {
let mut winner = self.loser_tree[0];

@tustvoldtustvoldNov 21, 2022

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be easier to follow if this was moved into a method called replace_loser_tree_winner, perhaps with a link to this GIF - https://en.wikipedia.org/wiki/K-way_merge_algorithm#/media/File:Loser_tree_replacement_selection.gif

}
}
}
let min_cursor_idx = self.loser_tree[0];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be made easier to follow if it were written along the lines of

let min_cursor = self.cursors[min_cursor_idx];
if min_cursor.is_finished() {
// All streams are exhausted
return Poll::Ready((!self.in_progress.is_empty()).then(|| self.build_record_batch()))
}
self.loser_tree_adjusted = false;
self.in_progress.push(...)
if self.in_progress.len() == self.batch_size {
return Poll::Ready(Some(self.build_record_batch()));
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't make this work in #4407

Comment on lines +590 to +595
if challenger_win {
self.loser_tree[cmp_node] = winner;
winner = challenger;
} else {
self.loser_tree[cmp_node] = challenger;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if challenger_win {
self.loser_tree[cmp_node] = winner;
winner = challenger;
}else{
self.loser_tree[cmp_node] = challenger;
}
if challenger_win {
self.loser_tree[cmp_node] = winner;
winner = challenger;
}

@alamb

Copy link
Copy Markdown
Contributor

Thanks @richox@tustvold and @viirya -- this looks awesome. Let's wait to see if @richox would like to respond to comments.

@alamb

Copy link
Copy Markdown
Contributor

If we don't hear back from @richox by Monday I plan to merge this PR and then create a follow on PR with the proposed changes from @tustvold and @viirya

@alambalamb changed the title Use tournament loser tree for k-way sort-mergingUse tournament loser tree for k-way sort-merging, increase merge speed by 50%Nov 28, 2022
@alamb

Copy link
Copy Markdown
Contributor

I will have a follow on PR up shortly

@alamb
alamb merged commit 0d334cf into apache:masterNov 28, 2022
@alamb

Copy link
Copy Markdown
Contributor

Thanks again @richox@tustvold and @viirya

@ursabot

Copy link
Copy Markdown

Benchmark runs are scheduled for baseline = 52e198e and contender = 0d334cf. 0d334cf is a master commit associated with this PR. Results will be available as each benchmark for each run completes.
Conbench compare runs links:
[Skipped ⚠️ Benchmarking of arrow-datafusion-commits is not supported on ec2-t3-xlarge-us-east-2] ec2-t3-xlarge-us-east-2
[Skipped ⚠️ Benchmarking of arrow-datafusion-commits is not supported on test-mac-arm] test-mac-arm
[Skipped ⚠️ Benchmarking of arrow-datafusion-commits is not supported on ursa-i9-9960x] ursa-i9-9960x
[Skipped ⚠️ Benchmarking of arrow-datafusion-commits is not supported on ursa-thinkcentre-m75q] ursa-thinkcentre-m75q
Buildkite builds:
Supported benchmarks:
ec2-t3-xlarge-us-east-2: Supported benchmark langs: Python, R. Runs only benchmarks with cloud = True
test-mac-arm: Supported benchmark langs: C++, Python, R
ursa-i9-9960x: Supported benchmark langs: Python, R, JavaScript
ursa-thinkcentre-m75q: Supported benchmark langs: C++, Java

@alamb

Copy link
Copy Markdown
Contributor

Follow on PR: #4407

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

coreCore DataFusion crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Introduce tournament tree to achieve better k-way sort-merging

5 participants

@richox@alamb@ursabot@viirya@tustvold