From 61351f779a3076bfa8b8cecdea19caa560fc95fc Mon Sep 17 00:00:00 2001 From: Georg Semmler Date: Tue, 3 May 2022 22:20:33 +0200 Subject: [PATCH] Optimise build times This commit replaces some recursive macros with simpler implementations by generating non recursive where clauses by just forwarding to the next smaller impl on the corresponding tuple. According to my measurements this reduces the build time for a `cargo check --no-default-features --features "sqlite 64-column-table"` build from ~1min 20s to ~1min 05s. --- .../insert_with_default_for_sqlite.rs | 46 ++---------- diesel/src/type_impls/tuples.rs | 71 ++++++------------- 2 files changed, 29 insertions(+), 88 deletions(-) diff --git a/diesel/src/query_builder/insert_statement/insert_with_default_for_sqlite.rs b/diesel/src/query_builder/insert_statement/insert_with_default_for_sqlite.rs index 23b9960c7bcc..c992b1bdd9fb 100644 --- a/diesel/src/query_builder/insert_statement/insert_with_default_for_sqlite.rs +++ b/diesel/src/query_builder/insert_statement/insert_with_default_for_sqlite.rs @@ -357,46 +357,14 @@ macro_rules! tuple_impls { } macro_rules! impl_contains_defaultable_value { - ( - @build - start_ts = [$($ST: ident,)*], - ts = [$T1: ident,], - bounds = [$($bounds: tt)*], - out = [$($out: tt)*], - )=> { - impl<$($ST,)*> ContainsDefaultableValue for ($($ST,)*) - where - $($ST: ContainsDefaultableValue,)* - $($bounds)* - $T1::Out: Any<$($out)*>, - { - type Out = <$T1::Out as Any<$($out)*>>::Out; - } - - }; - ( - @build - start_ts = [$($ST: ident,)*], - ts = [$T1: ident, $($T: ident,)+], - bounds = [$($bounds: tt)*], - out = [$($out: tt)*], - )=> { - impl_contains_defaultable_value! { - @build - start_ts = [$($ST,)*], - ts = [$($T,)*], - bounds = [$($bounds)* $T1::Out: Any<$($out)*>,], - out = [<$T1::Out as Any<$($out)*>>::Out], - } - }; ($T1: ident, $($T: ident,)+) => { - impl_contains_defaultable_value! { - @build - start_ts = [$T1, $($T,)*], - ts = [$($T,)*], - bounds = [], - out = [$T1::Out], - } + impl<$T1, $($T,)*> ContainsDefaultableValue for ($T1, $($T,)*) + where $T1: ContainsDefaultableValue, + ($($T,)*): ContainsDefaultableValue, + $T1::Out: Any<<($($T,)*) as ContainsDefaultableValue>::Out> + { + type Out = <$T1::Out as Any<<($($T,)*) as ContainsDefaultableValue>::Out>>::Out; + } }; ($T1: ident,) => { impl<$T1> ContainsDefaultableValue for ($T1,) diff --git a/diesel/src/type_impls/tuples.rs b/diesel/src/type_impls/tuples.rs index df6af762ed78..d35f4a597f40 100644 --- a/diesel/src/type_impls/tuples.rs +++ b/diesel/src/type_impls/tuples.rs @@ -5,8 +5,8 @@ use crate::deserialize::{ }; use crate::expression::{ is_contained_in_group_by, AppearsOnTable, AsExpression, AsExpressionList, Expression, - IsContainedInGroupBy, QueryMetadata, Selectable, SelectableExpression, TypedExpressionType, - ValidGrouping, + IsContainedInGroupBy, MixedAggregates, QueryMetadata, Selectable, SelectableExpression, + TypedExpressionType, ValidGrouping, }; use crate::insertable::{CanInsertInSingleQuery, InsertValues, Insertable, InsertableOptionHelper}; use crate::query_builder::*; @@ -114,12 +114,6 @@ macro_rules! tuple_impls { const HAS_STATIC_QUERY_ID: bool = $($T::HAS_STATIC_QUERY_ID &&)+ true; } - const _: () = { - #[derive(ValidGrouping)] - #[diesel(foreign_derive)] - struct TupleWrapper<$($T,)*>(($($T,)*)); - }; - impl_valid_grouping_for_tuple_of_columns!($($T,)*); impl<$($T,)+ Tab> UndecoratedInsertRecord for ($($T,)+) @@ -453,49 +447,22 @@ macro_rules! impl_from_sql_row { } macro_rules! impl_valid_grouping_for_tuple_of_columns { - ( - @build - start_ts = [$($ST: ident,)*], - ts = [$T1: ident,], - bounds = [$($bounds: tt)*], - is_aggregate = [$($is_aggregate: tt)*], - ) => { - impl<$($ST,)* Col> IsContainedInGroupBy for ($($ST,)*) - where Col: Column, - $($ST: IsContainedInGroupBy,)* - $($bounds)* - <$T1 as IsContainedInGroupBy>::Output: is_contained_in_group_by::IsAny<$($is_aggregate)*>, + ($T1: ident, $($T: ident,)+) => { + impl<$T1, $($T,)* GB> ValidGrouping for ($T1, $($T,)*) + where + $T1: ValidGrouping, + ($($T,)*): ValidGrouping, + $T1::IsAggregate: MixedAggregates<<($($T,)*) as ValidGrouping>::IsAggregate>, { - type Output = <<$T1 as IsContainedInGroupBy>::Output as is_contained_in_group_by::IsAny<$($is_aggregate)*>>::Output; + type IsAggregate = <$T1::IsAggregate as MixedAggregates<<($($T,)*) as ValidGrouping>::IsAggregate>>::Output; } - }; - ( - @build - start_ts = [$($ST: ident,)*], - ts = [$T1: ident, $($T: ident,)+], - bounds = [$($bounds: tt)*], - is_aggregate = [$($is_aggregate: tt)*], - ) => { - impl_valid_grouping_for_tuple_of_columns! { - @build - start_ts = [$($ST,)*], - ts = [$($T,)*], - bounds = [ - $($bounds)* - <$T1 as IsContainedInGroupBy>::Output: is_contained_in_group_by::IsAny<$($is_aggregate)*>, - ], - is_aggregate = [ - <<$T1 as IsContainedInGroupBy>::Output as is_contained_in_group_by::IsAny<$($is_aggregate)*>>::Output - ], - } - }; - ($T1: ident, $($T: ident,)+) => { - impl_valid_grouping_for_tuple_of_columns! { - @build - start_ts = [$T1, $($T,)*], - ts = [$($T,)*], - bounds = [], - is_aggregate = [<$T1 as IsContainedInGroupBy>::Output], + impl<$T1, $($T,)* Col> IsContainedInGroupBy for ($T1, $($T,)*) + where Col: Column, + ($($T,)*): IsContainedInGroupBy, + $T1: IsContainedInGroupBy, + $T1::Output: is_contained_in_group_by::IsAny<<($($T,)*) as IsContainedInGroupBy>::Output> + { + type Output = <$T1::Output as is_contained_in_group_by::IsAny<<($($T,)*) as IsContainedInGroupBy>::Output>>::Output; } }; ($T1: ident,) => { @@ -505,6 +472,12 @@ macro_rules! impl_valid_grouping_for_tuple_of_columns { { type Output = <$T1 as IsContainedInGroupBy>::Output; } + + impl<$T1, GB> ValidGrouping for ($T1,) + where $T1: ValidGrouping + { + type IsAggregate = $T1::IsAggregate; + } }; }