Skip to content

Commit 15f32dd

Browse files
simonvandelalamb
andauthored
Reduce binary size of ScalarUDFImpl default impls (#24966)
## 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. --> Part of #24727 ## 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. Please explain the problem you are trying to solve in terms of the user-visible behavior, rather than the implementation. For example, "The code in `foo.rs` doesn't handle nulls" is a symptom of the implementation. "COUNT(DISTINCT) returns wrong results when the column contains nulls" is the user-visible problem. --> Reduce binary code size by reducing monomorphized code. ## 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. --> Extracted non-generic default method body implementations of `ScalarUDFImpl`: - `coerce_types` - `schema_name` - `return_field_from_args` - `output_ordering` Measurements: `cargo llvm-lines --release -p datafusion-expr --lib | head -3` Before (1d9a982): ``` Lines Copies Function name ----- ------ ------------- 1247760 26213 (TOTAL) ``` After (7278ffd4fe7f): ``` Lines Copies Function name ----- ------ ------------- 1247097 26207 (TOTAL) ``` `cargo build --profile release-nonlto --bin datafusion-cli && stat -f%z ../shared-cargo-target/release-nonlto/datafusion-cli` Before (1d9a982): ``` 186946400 ``` After (7278ffd4fe7f): ``` 186574320 ``` So 663 fewer LLVM lines, 372 080 fewer bytes (non-lto build of datafusion-cli) ## What is the testing strategy for this PR? Just code-shuffling, no behaviour change. <!-- 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 Briefly describe how this PR is tested, and point to the specific tests you added. For example: 'This new feature is covered by the `sqllogictest` cases added in `foo.slt`'. If this PR does not add tests, explain why. For example, if the change is already covered by existing tests, please mention it. You should also check the `codecov` bot reply on this PR to confirm the changed code is exercised. --> ## Are there any user-facing changes? Slightly binary size, and maybe faster compilation. <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. If there are any breaking changes to public APIs, please add the `api change` label. --> --------- Co-authored-by: Andrew Lamb <[email protected]>
1 parent 2f58ae9 commit 15f32dd

1 file changed

Lines changed: 39 additions & 24 deletions

File tree

‎datafusion/expr/src/udf.rs‎

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -543,11 +543,16 @@ pub trait ScalarUDFImpl: Debug + DynEq + DynHash + Send + Sync + Any {
543543
///
544544
/// See [`Expr::schema_name`] for details
545545
fn schema_name(&self, args: &[Expr]) -> Result<String> {
546-
Ok(format!(
547-
"{}({})",
548-
self.name(),
549-
schema_name_from_exprs_comma_separated_without_space(args)?
550-
))
546+
/// Use an inner function to avoid code duplication over all
547+
/// generic callsites as the body is the same.
548+
fn inner(name: &str, args: &[Expr]) -> Result<String> {
549+
Ok(format!(
550+
"{}({})",
551+
name,
552+
schema_name_from_exprs_comma_separated_without_space(args)?
553+
))
554+
}
555+
inner(self.name(), args)
551556
}
552557

553558
/// Returns a [`Signature`] describing the argument types for which this
@@ -660,12 +665,12 @@ pub trait ScalarUDFImpl: Debug + DynEq + DynHash + Send + Sync + Any {
660665
/// logical input even if the input is simplified (e.g. it must return the same
661666
/// value for `('foo' | 'bar')` as it does for ('foobar').
662667
fn return_field_from_args(&self, args: ReturnFieldArgs) -> Result<FieldRef> {
663-
let data_types = args
664-
.arg_fields
665-
.iter()
666-
.map(|f| f.data_type())
667-
.cloned()
668-
.collect::<Vec<_>>();
668+
/// Use an inner function to avoid code duplication over all
669+
/// generic callsites as the body is the same.
670+
fn arg_fields_to_data_types(arg_fields: &[FieldRef]) -> Vec<DataType> {
671+
arg_fields.iter().map(|f| f.data_type().clone()).collect()
672+
}
673+
let data_types = arg_fields_to_data_types(args.arg_fields);
669674
let return_type = self.return_type(&data_types)?;
670675
Ok(Arc::new(Field::new(self.name(), return_type, true)))
671676
}
@@ -942,19 +947,24 @@ pub trait ScalarUDFImpl: Debug + DynEq + DynHash + Send + Sync + Any {
942947
return Ok(SortProperties::Unordered);
943948
}
944949

945-
let Some(first_order) = inputs.first().map(|p| &p.sort_properties) else {
946-
return Ok(SortProperties::Singleton);
947-
};
948-
949-
if inputs
950-
.iter()
951-
.skip(1)
952-
.all(|input| &input.sort_properties == first_order)
953-
{
954-
Ok(*first_order)
955-
} else {
956-
Ok(SortProperties::Unordered)
950+
/// Use an inner function to avoid code duplication over all
951+
/// generic callsites as the body is the same.
952+
fn inner(inputs: &[ExprProperties]) -> SortProperties {
953+
let Some(first_order) = inputs.first().map(|p| &p.sort_properties) else {
954+
return SortProperties::Singleton;
955+
};
956+
957+
if inputs
958+
.iter()
959+
.skip(1)
960+
.all(|input| &input.sort_properties == first_order)
961+
{
962+
*first_order
963+
} else {
964+
SortProperties::Unordered
965+
}
957966
}
967+
Ok(inner(inputs))
958968
}
959969

960970
/// Returns true if the function preserves lexicographical ordering based on
@@ -995,7 +1005,12 @@ pub trait ScalarUDFImpl: Debug + DynEq + DynHash + Send + Sync + Any {
9951005
/// A Vec the same length as `arg_types`. DataFusion will `CAST` the function call
9961006
/// arguments to these specific types.
9971007
fn coerce_types(&self, _arg_types: &[DataType]) -> Result<Vec<DataType>> {
998-
not_impl_err!("Function {} does not implement coerce_types", self.name())
1008+
/// Use an inner function to avoid code duplication over all
1009+
/// generic callsites as the body is the same.
1010+
fn inner(name: &str) -> Result<Vec<DataType>> {
1011+
not_impl_err!("Function {name} does not implement coerce_types")
1012+
}
1013+
inner(self.name())
9991014
}
10001015

10011016
/// For struct-producing functions, return how output fields map to input

0 commit comments

Comments
 (0)