You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "simonvandel (via GitHub)" <gi...@apache.org> on 2024/01/27 14:09:27 UTC
[PR] Speedup `DFSchema::merge` using HashSet indices [arrow-datafusion]
simonvandel opened a new pull request, #9020:
URL: https://github.com/apache/arrow-datafusion/pull/9020
## Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
-->
Relates to https://github.com/apache/arrow-datafusion/issues/5637
## Rationale for this change
<!--
Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
-->
Planning time of Datafusion is slow.
## What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
-->
Profiling shows that a large part of planning is spent in `DFSchema::merge`.
Before this PR, merging was complexity `O(N * M)`, where `N` and `M` are number of fields in the two schemas.
With this PR, merging is complexity `O(N + M)` using a HashSet as an index.
Speeds up benchmarks in sql_planner.rs by 18-75%. However, physical planning benchmarks seem to regress by 1.8%. I wonder if that is an acceptable trade?
<details>
<summary>Benchmark results</summary>
```
Gnuplot not found, using plotters backend
Benchmarking logical_select_one_from_700: Warming up for 3.0000 s
Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 6.8s, enable flat sampling, or reduce sample count to 60.
logical_select_one_from_700
time: [1.3324 ms 1.3359 ms 1.3396 ms]
change: [-54.725% -54.348% -53.914%] (p = 0.00 < 0.05)
Performance has improved.
Found 14 outliers among 100 measurements (14.00%)
6 (6.00%) low mild
3 (3.00%) high mild
5 (5.00%) high severe
physical_select_one_from_700
time: [4.6236 ms 4.6485 ms 4.6795 ms]
change: [-75.159% -74.941% -74.735%] (p = 0.00 < 0.05)
Performance has improved.
Found 8 outliers among 100 measurements (8.00%)
3 (3.00%) high mild
5 (5.00%) high severe
Benchmarking logical_trivial_join_low_numbered_columns: Warming up for 3.0000 s
Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 6.8s, enable flat sampling, or reduce sample count to 60.
logical_trivial_join_low_numbered_columns
time: [1.3504 ms 1.3546 ms 1.3592 ms]
change: [-22.854% -22.042% -21.199%] (p = 0.00 < 0.05)
Performance has improved.
Found 11 outliers among 100 measurements (11.00%)
1 (1.00%) low mild
4 (4.00%) high mild
6 (6.00%) high severe
Benchmarking logical_trivial_join_high_numbered_columns: Warming up for 3.0000 s
Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 7.1s, enable flat sampling, or reduce sample count to 50.
logical_trivial_join_high_numbered_columns
time: [1.3961 ms 1.4003 ms 1.4051 ms]
change: [-21.654% -21.238% -20.848%] (p = 0.00 < 0.05)
Performance has improved.
Found 12 outliers among 100 measurements (12.00%)
2 (2.00%) low mild
8 (8.00%) high mild
2 (2.00%) high severe
Benchmarking logical_aggregate_with_join: Warming up for 3.0000 s
Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 9.3s, enable flat sampling, or reduce sample count to 50.
logical_aggregate_with_join
time: [1.8286 ms 1.8331 ms 1.8381 ms]
change: [-18.973% -18.543% -18.126%] (p = 0.00 < 0.05)
Performance has improved.
Found 8 outliers among 100 measurements (8.00%)
1 (1.00%) low mild
6 (6.00%) high mild
1 (1.00%) high severe
physical_plan_tpch time: [5.6537 ms 5.6803 ms 5.7095 ms]
change: [+1.1125% +1.7618% +2.3821%] (p = 0.00 < 0.05)
Performance has regressed.
Found 9 outliers among 100 measurements (9.00%)
6 (6.00%) high mild
3 (3.00%) high severe
```
</details>
## Are these changes tested?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
-->
Should be in existing tests.
## Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be updated before approving the PR.
-->
Faster planning.
<!--
If there are any breaking changes to public APIs, please add the `api change` label.
-->
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Speedup `DFSchema::merge` using HashSet indices [arrow-datafusion]
Posted by "simonvandel (via GitHub)" <gi...@apache.org>.
simonvandel commented on code in PR #9020:
URL: https://github.com/apache/arrow-datafusion/pull/9020#discussion_r1468801356
##########
datafusion/common/src/dfschema.rs:
##########
@@ -218,17 +218,28 @@ impl DFSchema {
if other_schema.fields.is_empty() {
return;
}
+
+ let self_fields: HashSet<&DFField> = self.fields.iter().collect();
Review Comment:
I tried this, but it didn't seem like it had any impact on performance, so I'll keep the collect, which I find nicer.
<details>
<summary>Patch</summary>
```diff
diff --git a/datafusion/common/src/dfschema.rs b/datafusion/common/src/dfschema.rs
index 2642032c9..6d9e50e09 100644
--- a/datafusion/common/src/dfschema.rs
+++ b/datafusion/common/src/dfschema.rs
@@ -219,9 +219,15 @@ impl DFSchema {
return;
}
- let self_fields: HashSet<&DFField> = self.fields.iter().collect();
- let self_unqualified_names: HashSet<&str> =
- self.fields.iter().map(|x| x.name().as_str()).collect();
+ let mut self_fields: HashSet<&DFField> =
+ HashSet::with_capacity(self.fields.len());
+ let mut self_unqualified_names: HashSet<&str> =
+ HashSet::with_capacity(self.fields.len());
+
+ for f in &self.fields {
+ self_fields.insert(f);
+ self_unqualified_names.insert(f.name().as_str());
+ }
let mut fields_to_add = vec![];
```
</details>
<details>
<summary>Single vs double iteration benchmark results</summary>
```
Benchmarking logical_select_one_from_700: Warming up for 3.0000 s
Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 6.7s, enable flat sampling, or reduce sample count to 60.
logical_select_one_from_700
time: [1.3132 ms 1.3188 ms 1.3251 ms]
change: [-0.4502% +0.0956% +0.6717%] (p = 0.73 > 0.05)
No change in performance detected.
Found 10 outliers among 100 measurements (10.00%)
1 (1.00%) low severe
2 (2.00%) low mild
5 (5.00%) high mild
2 (2.00%) high severe
physical_select_one_from_700
time: [4.5525 ms 4.5705 ms 4.5909 ms]
change: [-1.5314% -1.0539% -0.6109%] (p = 0.00 < 0.05)
Change within noise threshold.
Found 11 outliers among 100 measurements (11.00%)
3 (3.00%) high mild
8 (8.00%) high severe
Benchmarking logical_trivial_join_low_numbered_columns: Warming up for 3.0000 s
Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 6.8s, enable flat sampling, or reduce sample count to 60.
logical_trivial_join_low_numbered_columns
time: [1.3299 ms 1.3379 ms 1.3478 ms]
change: [-1.0025% +0.0082% +1.0720%] (p = 0.99 > 0.05)
No change in performance detected.
Found 8 outliers among 100 measurements (8.00%)
4 (4.00%) high mild
4 (4.00%) high severe
Benchmarking logical_trivial_join_high_numbered_columns: Warming up for 3.0000 s
Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 7.0s, enable flat sampling, or reduce sample count to 50.
logical_trivial_join_high_numbered_columns
time: [1.3746 ms 1.3806 ms 1.3873 ms]
change: [-0.8147% -0.1908% +0.5022%] (p = 0.57 > 0.05)
No change in performance detected.
Found 17 outliers among 100 measurements (17.00%)
2 (2.00%) low mild
11 (11.00%) high mild
4 (4.00%) high severe
Benchmarking logical_aggregate_with_join: Warming up for 3.0000 s
Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 9.2s, enable flat sampling, or reduce sample count to 50.
logical_aggregate_with_join
time: [1.8058 ms 1.8132 ms 1.8212 ms]
change: [-0.1207% +0.7180% +1.5705%] (p = 0.11 > 0.05)
No change in performance detected.
Found 10 outliers among 100 measurements (10.00%)
4 (4.00%) high mild
6 (6.00%) high severe
physical_plan_tpch time: [5.5699 ms 5.5904 ms 5.6136 ms]
change: [-0.5328% +0.0473% +0.6442%] (p = 0.88 > 0.05)
No change in performance detected.
Found 7 outliers among 100 measurements (7.00%)
7 (7.00%) high severe
```
</details>
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Speedup `DFSchema::merge` using HashSet indices [arrow-datafusion]
Posted by "matthewmturner (via GitHub)" <gi...@apache.org>.
matthewmturner commented on PR #9020:
URL: https://github.com/apache/arrow-datafusion/pull/9020#issuecomment-1913232910
Nice 😀
Trade off seems worth it.
I'm curious what's going on in that TPCH benchmark though as the other physical select benchmark showed solid improvement so not sure it's all physical planning.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Speedup `DFSchema::merge` using HashSet indices [arrow-datafusion]
Posted by "simonvandel (via GitHub)" <gi...@apache.org>.
simonvandel commented on code in PR #9020:
URL: https://github.com/apache/arrow-datafusion/pull/9020#discussion_r1468675317
##########
datafusion/common/src/dfschema.rs:
##########
@@ -218,17 +218,28 @@ impl DFSchema {
if other_schema.fields.is_empty() {
return;
}
+
+ let self_fields: HashSet<&DFField> = self.fields.iter().collect();
Review Comment:
Do you mean constructing the hashsets in a single iteration of fields?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Speedup `DFSchema::merge` using HashSet indices [arrow-datafusion]
Posted by "comphead (via GitHub)" <gi...@apache.org>.
comphead commented on code in PR #9020:
URL: https://github.com/apache/arrow-datafusion/pull/9020#discussion_r1468674393
##########
datafusion/common/src/dfschema.rs:
##########
@@ -218,17 +218,28 @@ impl DFSchema {
if other_schema.fields.is_empty() {
return;
}
+
+ let self_fields: HashSet<&DFField> = self.fields.iter().collect();
Review Comment:
we can possibly try to avoid double iteration?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Speedup `DFSchema::merge` using HashSet indices [arrow-datafusion]
Posted by "comphead (via GitHub)" <gi...@apache.org>.
comphead commented on code in PR #9020:
URL: https://github.com/apache/arrow-datafusion/pull/9020#discussion_r1468674345
##########
datafusion/common/src/dfschema.rs:
##########
@@ -218,17 +218,28 @@ impl DFSchema {
if other_schema.fields.is_empty() {
return;
}
+
+ let self_fields: HashSet<&DFField> = self.fields.iter().collect();
+ let self_unqualified_names: HashSet<&str> = self
+ .fields
+ .iter()
+ .map(|x| x.field.name().as_str())
Review Comment:
```suggestion
.map(|x| x.name().as_str())
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Speedup `DFSchema::merge` using HashSet indices [arrow-datafusion]
Posted by "matthewmturner (via GitHub)" <gi...@apache.org>.
matthewmturner commented on code in PR #9020:
URL: https://github.com/apache/arrow-datafusion/pull/9020#discussion_r1468513711
##########
datafusion/common/src/dfschema.rs:
##########
@@ -218,17 +218,28 @@ impl DFSchema {
if other_schema.fields.is_empty() {
return;
}
+
+ let self_fields: HashSet<&DFField> = self.fields.iter().map(|x| x).collect();
Review Comment:
I believe we can remove the map here since no transformation is done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Speedup `DFSchema::merge` using HashSet indices [arrow-datafusion]
Posted by "comphead (via GitHub)" <gi...@apache.org>.
comphead commented on code in PR #9020:
URL: https://github.com/apache/arrow-datafusion/pull/9020#discussion_r1469916490
##########
datafusion/common/src/dfschema.rs:
##########
@@ -218,17 +218,28 @@ impl DFSchema {
if other_schema.fields.is_empty() {
return;
}
+
+ let self_fields: HashSet<&DFField> = self.fields.iter().collect();
Review Comment:
if its no perf benefit then collect is more readable I agree
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Speedup `DFSchema::merge` using HashSet indices [arrow-datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #9020:
URL: https://github.com/apache/arrow-datafusion/pull/9020#issuecomment-1915632345
> Speeds up benchmarks in sql_planner.rs by 18-75%. However, physical planning benchmarks seem to regress by 1.8%. I wonder if that is an acceptable trade?
I think this is an acceptable trade
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org