-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Cache PlanProperties, add fast-path for with_new_children
#19792
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
base: main
Are you sure you want to change the base?
Conversation
with_new_childrenwith_new_children
72ff575 to
796f731
Compare
|
Also added a typical analytical query plan re-usage benchmark. On the $ cargo bench --profile=release-nonlto --bench plan_reuse |
796f731 to
5601c4f
Compare
|
I filed a ticket to track this idea |
|
run benchmark sql_planner |
|
🤖 |
Could you move the plan_reuse benchmark into its own PR (as I think it is valuable both for this PR and others, and it makes it easier to automatically compare performance) |
|
Benchmark script failed with exit code 101. Last 10 lines of output: Click to expand |
Done in #19806 |
5601c4f to
99cf634
Compare
99cf634 to
b81dd66
Compare
alamb
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @askalt -- this is quite clever and I think it looks very promising
I also think we may be able to potentially make with_new_children even faster by checking the children as well -- and if they are the same there is no reason to recompute everything either.
However, this likely won't help your usecase as the children will likely change (their states need to be reset) 🤔
| db: CustomDataSource, | ||
| projected_schema: SchemaRef, | ||
| cache: PlanProperties, | ||
| cache: Arc<PlanProperties>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯 for this change
| mut children: Vec<Arc<dyn ExecutionPlan>>, | ||
| ) -> Result<Arc<dyn ExecutionPlan>> { | ||
| if has_same_children_properties(&self, &children)? { | ||
| // Avoid properties re-computation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we coulda avoid even more copying / cloning in the common case using Arc::make_mut https://doc.rust-lang.org/std/sync/struct.Arc.html#method.make_mut
Something like
if has_same_children_properties(&self, &children)? {
// Avoid properties re-computation.
let me = Arc::make_mut(&mut self);
me.input = children.swap_remove(0);
me.metrics = ExecutionPlanMetricsSet::new();
return Ok(self);
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that I write that, I wonder if we could use make_mut to avoid lots of cloning in general 🤔
We could check if the new children are the same as the existing children and if so return self 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems we already check it explicitly in DynTreeNode implementation:
| with_new_children_if_necessary(arc_self, new_children) |
| pub fn with_new_children_if_necessary( |
b81dd66 to
741b085
Compare
| self: Arc<Self>, | ||
| mut children: Vec<Arc<dyn ExecutionPlan>>, | ||
| ) -> Result<Arc<dyn ExecutionPlan>> { | ||
| check_if_same_properties!(self, children); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fast-path is currently implemented for most plans. It seems quite odd to me that we need to ensure it's implemented for every plan (if at least one plan doesn't implement this check and recalculates its properties, then recalculation will be performed for all parent plans). I've reduced the required code as much as possible: a plan implementer needs to call the added macro and implement the with_new_children_and_same_properties(...) method for the plan.
with_new_childrenwith_new_children
|
This PR seems to have accumulated some conflicts |
|
I plan to run the newly introduced benchmark in #19806 I suspect we'll see quite a nice improvement |
This patch aims to implement a fast-path for the ExecutionPlan::with_new_children function for some plans, moving closer to a physical plan re-use implementation and improving planning performance. If the passed children properties are the same as in self, we do not actually recompute self's properties (which could be costly if projection mapping is required). Instead, we just replace the children and re-use self's properties as-is. To be able to compare two different properties -- ExecutionPlan::properties(...) signature is modified and now returns `&Arc<PlanProperties>`. If `children` properties are the same in `with_new_children` -- we clone our properties arc and then a parent plan will consider our properties as unchanged, doing the same. Also, there are other improvements. The patch includes the following changes: - Return `&Arc<PlanProperties>` from `ExecutionPlan::properties(...)` instead of a reference. - Implement `with_new_children` fast-path if there is no children properties changes for all major plans. - Store `Arc<[usize]>` instead of vector within `FilterExec`. - Store `Arc<[usize]>` instead of vector within projection of `HashJoinExec`. - Store `Arc<[Arc<AggregateFunctionExpr>]>` instead of vec for aggr expr and filters. - Store `Arc<[ProjectionExpr]> instead of vec in `ProjectionExprs` struct. - Get `Option<&[usize]>` instead of option vec ref in `project_schema` -- it makes API more flexible. Note: currently, `reset_plan_states` does not allow to re-use plan in general: it is not supported for dynamic filters and recursive queries features, as in this case state reset should update pointers in the children plans. Closes apache#19796
741b085 to
21b8a3f
Compare
|
run benchmark reset_plan_states |
|
🤖 |
|
🤖: Benchmark completed Details
|
That is pretty amazing (3000x-15000x performance improvement 👍 ) I am also testing sql_planner time using |
with_new_childrenPlanProperties, add fast-path for with_new_children
alamb
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @askalt
I went through this carefully -- I think the basic idea is great and we should proceed.
The only thing I think we should try and do is minimize the impact of the API changes when people upgrade. I have some thoughts on that below
For next steps, I suggest break this PR down into smaller parts (it is quite challenging to review all the changes now). Some ideas on relevant chunks:
- Change ExecutionPlan::properties to return an
&Arc<Properties>and the necessary plumbing changes - Changes to FilterExec to avoid cloning project
- Changes to AggregateExec to avoid cloning the vec / exprs
- Changes to the various join operations to avoid cloning the projections
thoughts on reducing upgrade impact
Since this PR does change some core APIs, think we need to be careful.
- I think we can avoid the need to change
Schema::project, see askalt#2 for my proposal - We should figure out some way to make FilterExec::with_projection easier to use -- the new signature is pretty awkward.
Maybe we can use ProjectionExprs or something like
struct SharedProjection {
Arc<[usize]>
}
impl From<Vec<usize>> for SharedProjection {
...
}And then have FilterExec take
pub fn fn with_projection(mut self, projection impl Into<SharedProjection>)
With enough comments I think this could be clear and would also be backwards compatible
| /// This information is available via methods on [`ExecutionPlanProperties`] | ||
| /// trait, which is implemented for all `ExecutionPlan`s. | ||
| fn properties(&self) -> &PlanProperties; | ||
| fn properties(&self) -> &Arc<PlanProperties>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is technically a breaking API change, so it should be documented in the upgrading.md guide:
https://github.com/apache/datafusion/blob/main/docs/source/library-user-guide/upgrading.md
Per the policy in
https://datafusion.apache.org/contributor-guide/api-health.html
I am happy to help write such an entry
| pub fn project_schema( | ||
| schema: &SchemaRef, | ||
| projection: Option<&Vec<usize>>, | ||
| projection: Option<&[usize]>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is also technically a public API change, but I think it just relaxes the constraints (you can still pass in a &Vec so all downstream code should continue to work
| name: String, | ||
| plan: FFI_ExecutionPlan, | ||
| properties: PlanProperties, | ||
| properties: Arc<PlanProperties>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since the FFI is supposed to be a stable boundary, I think we shouldn't change this (and instead just copy the properties when needed)
So I suggest reverting this change
| self.aggr_expr.clone(), | ||
| self.filter_expr.clone(), | ||
| Arc::clone(&children[0]), | ||
| self.aggr_expr.to_vec(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we might be able to avoid these clones too by changing the signature of AggregateExec 🤔
PlanPropertiesredundently #19796This patch aims to implement a fast-path for the ExecutionPlan::with_new_children function for some plans, moving closer to a physical plan re-use implementation and improving planning performance. If the passed children properties are the same as in self, we do not actually recompute self's properties (which could be costly if projection mapping is required). Instead, we just replace the children and re-use self's properties as-is.
To be able to compare two different properties -- ExecutionPlan::properties(...) signature is modified and now returns
&Arc<PlanProperties>. Ifchildrenproperties are the same inwith_new_children-- we clone our properties arc and then a parent plan will consider our properties as unchanged, doing the same.Also, there are other improvenets, all changes:
&Arc<PlanProperties>fromExecutionPlan::properties(...)instead of a reference.with_new_childrenfast-path if there is no children properties changes for allmajor plans.
Arc<[usize]>instead of vector withinFilterExec.Arc<[usize]>instead of vector within projection ofHashJoinExec.Arc<[Arc<AggregateFunctionExpr>]>instead of vec for aggr expr and filters.Arc<[ProjectionExpr]> instead of vec inProjectionExprs` struct.Option<&[usize]>instead of option vec ref inproject_schema-- it makes APImore flexible.
Note: currently,
reset_plan_statesdoes not allow to re-use plan in general: it is notsupported for dynamic filters and recursive queries features, as in this case state reset
should update pointers in the children plans.