Skip to content
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

feat(optimizer): improve inline now proc time #13609

Merged
merged 6 commits into from
Nov 24, 2023

Conversation

chenzl25
Copy link
Contributor

@chenzl25 chenzl25 commented Nov 23, 2023

I hereby agree to the terms of the RisingWave Labs, Inc. Contributor License Agreement.

What's changed and what's your intention?

Checklist

  • I have written necessary rustdoc comments
  • I have added necessary unit tests and integration tests
  • I have added test labels as necessary. See details.
  • I have added fuzzing tests or opened an issue to track them. (Optional, recommended for new SQL features Sqlsmith: Sql feature generation #7934).
  • My PR contains breaking changes. (If it deprecates some features, please create a tracking issue to remove them in the future).
  • All checks passed in ./risedev check (or alias, ./risedev c)
  • My PR changes performance-critical code. (Please run macro/micro-benchmarks and show the results.)
  • My PR contains critical fixes that are necessary to be merged into the latest release. (Please check out the details)

Documentation

  • My PR needs documentation updates. (Please use the Release note section below to summarize the impact on users)

Release note

If this PR includes changes that directly affect users or other significant modifications relevant to the community, kindly draft a release note to provide a concise summary of these changes. Please prioritize highlighting the impact these changes will have on users.

Base automatically changed from dylan/support_expr_visitable_for_plan_node to main November 23, 2023 03:40
Copy link

codecov bot commented Nov 23, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (38a78d8) 68.06% compared to head (195bb5c) 68.07%.
Report is 11 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #13609   +/-   ##
=======================================
  Coverage   68.06%   68.07%           
=======================================
  Files        1516     1516           
  Lines      261570   261595   +25     
=======================================
+ Hits       178049   178082   +33     
+ Misses      83521    83513    -8     
Flag Coverage Δ
rust 68.07% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

xxchan
xxchan previously approved these changes Nov 23, 2023
@@ -487,10 +491,16 @@ impl LogicalOptimizer {
}

pub fn inline_now_proc_time(plan: PlanRef, ctx: &OptimizerContextRef) -> PlanRef {
// TODO: if there's no `NOW()` or `PROCTIME()`, we don't need to acquire snapshot.
let mut v = InlineNowProcTime::new(Epoch(INVALID_EPOCH));
plan.visit_exprs_recursive(&mut v);
Copy link
Member

Choose a reason for hiding this comment

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

Maybe better to add doc comment on visit_exprs_recursive and rewrite_exprs_recursive, mentioning such usage is for performance.

@BugenZhao
Copy link
Member

Actually I'm wondering why this could improve the performance. It appears that a no-op call of rewrite_exprs_recursive will still lead to performance overhead of cloning and allocation, which is far from ideal. In this PR, we manually add a visitor dual for a rewriter to avoid calling rewrite as much as possible. However, the developers still have to make sure they're identical, just like what we talked in #13587 (comment).

I'm considering whether it's possible to reduce the overhead of rewrite...

Take this for an example, a more efficient version should be...

        for expr in self.exprs.iter_mut() {
            *expr = r.rewrite_expr(std::mem::replace(expr, ExprImpl::Dummy));
        }

where no allocation will be done if there's no actual rewriting.

@@ -55,7 +50,7 @@
"stages": {
"0": {
"root": {
"plan_node_id": 10016,
"plan_node_id": 10013,
Copy link
Contributor

Choose a reason for hiding this comment

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

It somehow becomes a indicator about how many plan node has been generated during the optimization 😆

@@ -81,3 +91,21 @@ impl ExprRewriter for InlineNowProcTime {
FunctionCall::new_unchecked(func_type, inputs, ret).into()
}
}

impl ExprVisitor for InlineNowProcTime {
Copy link
Member

Choose a reason for hiding this comment

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

I would suggest defining a new visitor type since its behavior is strictly orthogonal to the rewriter.

Copy link
Member

@BugenZhao BugenZhao left a comment

Choose a reason for hiding this comment

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

This PR LGTM to achieve goal #13459.

@xxchan xxchan dismissed their stale review November 23, 2023 07:20

I agree with Bugen, so wait for his approval instead

@st1page
Copy link
Contributor

st1page commented Nov 23, 2023

  • When expressions not actually be rewritten, rewrite_exprs should be able to directly return itself without cloning. This can be achieved with Cow.
    fn rewrite_exprs(&self, r: &mut dyn ExprRewriter) -> PlanRef {
    let mut core = self.core.clone();
    core.rewrite_exprs(r);
    Self {
    base: self.base.clone_with_new_plan_id(),
    core,
    }
    .into()
    }

I guess we can achieve it with passing the Arc

use std::rc::Rc;
pub trait ExprRewritable {
    fn rewrite_exprs(self: Rc<Self>) -> Rc<dyn ExprRewritable>;
}

struct Proj {
    a: i32
}

impl ExprRewritable for Proj {
    fn rewrite_exprs(self: Rc<Self>) -> Rc<dyn ExprRewritable> {
        if self.a == 1 {
            return Rc::new(Proj{ a: 2});
        } else {
            return self
        }
    }

}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=af96f135c7068c575bd1a0d9493b7ee3

@chenzl25
Copy link
Contributor Author

Actually I'm wondering why this could improve the performance. It appears that a no-op call of rewrite_exprs_recursive will still lead to performance overhead of cloning and allocation, which is far from ideal.

Good idea. Maybe we can refactor it in the future if necessary.

In this PR, we manually add a visitor dual for a rewriter to avoid calling rewrite as much as possible.
However, the developers still have to make sure they're identical, just like what we talked in #13587 (comment).

I think letting developers make sure they're identical in our cases seems trivial. It is not too painful to work with it at this moment.

@chenzl25 chenzl25 requested a review from BugenZhao November 23, 2023 09:05
@chenzl25 chenzl25 added this pull request to the merge queue Nov 24, 2023
Merged via the queue into main with commit 937e099 Nov 24, 2023
6 of 7 checks passed
@chenzl25 chenzl25 deleted the dylan/improve_inline_now_proc_time branch November 24, 2023 06:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants