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

perf: avoid repeat format in calc_func_dependencies_for_project #12305

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions datafusion/expr/src/logical_plan/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2793,22 +2793,28 @@ fn calc_func_dependencies_for_project(
.filter_map(|(qualifier, f)| {
let flat_name = qualifier
.map(|t| format!("{}.{}", t, f.name()))
.unwrap_or(f.name().clone());
.unwrap_or_else(|| f.name().clone());
Copy link
Member

Choose a reason for hiding this comment

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

Arguments passed to unwrap_or are eagerly evaluated; if you are passing the result of a function call, it is recommended to use unwrap_or_else, which is lazily evaluated.

👍

input_fields.iter().position(|item| *item == flat_name)
})
.collect::<Vec<_>>(),
)
}
Expr::Alias(alias) => Ok(input_fields
.iter()
.position(|item| *item == format!("{}", alias.expr))
.map(|i| vec![i])
.unwrap_or(vec![])),
_ => Ok(input_fields
.iter()
.position(|item| *item == format!("{}", expr))
.map(|i| vec![i])
.unwrap_or(vec![])),
Expr::Alias(alias) => {
let name = format!("{}", alias.expr);
Copy link
Contributor

Choose a reason for hiding this comment

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

Surprise that rust compile doesn't optimize on this 😮

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe because the rust doesn't know the length of inputs_fields at compile time, it doesn't optimize this case.

Ok(input_fields
.iter()
.position(|item| *item == name)
.map(|i| vec![i])
.unwrap_or(vec![]))
}
_ => {
let name = format!("{}", expr);
Ok(input_fields
.iter()
.position(|item| *item == name)
.map(|i| vec![i])
.unwrap_or(vec![]))
}
})
.collect::<Result<Vec<_>>>()?
.into_iter()
Expand Down