Skip to content

Commit

Permalink
Optimise build times
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
weiznich committed May 3, 2022
1 parent b0a2c1e commit 61351f7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,)
Expand Down
71 changes: 22 additions & 49 deletions diesel/src/type_impls/tuples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -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<Tab> for ($($T,)+)
Expand Down Expand Up @@ -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<Col> for ($($ST,)*)
where Col: Column,
$($ST: IsContainedInGroupBy<Col>,)*
$($bounds)*
<$T1 as IsContainedInGroupBy<Col>>::Output: is_contained_in_group_by::IsAny<$($is_aggregate)*>,
($T1: ident, $($T: ident,)+) => {
impl<$T1, $($T,)* GB> ValidGrouping<GB> for ($T1, $($T,)*)
where
$T1: ValidGrouping<GB>,
($($T,)*): ValidGrouping<GB>,
$T1::IsAggregate: MixedAggregates<<($($T,)*) as ValidGrouping<GB>>::IsAggregate>,
{
type Output = <<$T1 as IsContainedInGroupBy<Col>>::Output as is_contained_in_group_by::IsAny<$($is_aggregate)*>>::Output;
type IsAggregate = <$T1::IsAggregate as MixedAggregates<<($($T,)*) as ValidGrouping<GB>>::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<Col>>::Output: is_contained_in_group_by::IsAny<$($is_aggregate)*>,
],
is_aggregate = [
<<$T1 as IsContainedInGroupBy<Col>>::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<Col>>::Output],
impl<$T1, $($T,)* Col> IsContainedInGroupBy<Col> for ($T1, $($T,)*)
where Col: Column,
($($T,)*): IsContainedInGroupBy<Col>,
$T1: IsContainedInGroupBy<Col>,
$T1::Output: is_contained_in_group_by::IsAny<<($($T,)*) as IsContainedInGroupBy<Col>>::Output>
{
type Output = <$T1::Output as is_contained_in_group_by::IsAny<<($($T,)*) as IsContainedInGroupBy<Col>>::Output>>::Output;
}
};
($T1: ident,) => {
Expand All @@ -505,6 +472,12 @@ macro_rules! impl_valid_grouping_for_tuple_of_columns {
{
type Output = <$T1 as IsContainedInGroupBy<Col>>::Output;
}

impl<$T1, GB> ValidGrouping<GB> for ($T1,)
where $T1: ValidGrouping<GB>
{
type IsAggregate = $T1::IsAggregate;
}
};
}

Expand Down

0 comments on commit 61351f7

Please sign in to comment.