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

Speedup compile times for joins #3288

Merged
merged 6 commits into from
Aug 25, 2022

Conversation

weiznich
Copy link
Member

Addresses #3223

weiznich and others added 2 commits August 19, 2022 17:50
`DefaultSelection` type of joins

This commit introduces infrastructure that allows to skip type checking
the `DefaultSelection` of joins. diesel-rs#3223 discovered that this is
responsible for a substantial amount of compile time for larger
joins/codebases. We do not need to check this constraint there as it is
always true because each of the tables/sides of the join already have an
valid `DefaultSelection`. Therefore the resulting `DefaultSelection`
must be valid for the join too (if there is no bug in diesel itself).
it doesn't compile as-is.
The reason for this is that with this implem,
```rust
	let q = schema::some_table_1::table
		.inner_join(schema::some_table_2::table)
		.inner_join(schema::some_table_3::table.inner_join(schema::some_table_4::table));
```
won't compile, because of this bound:
```
impl<Left, Right> QuerySource for Join<Left, Right, Inner>
where
    Left: AppendSelection<Right::DefaultSelection>,
```
and we don't have `Left = Join<LeftLeft, LeftRight, Inner>: AppendSelection<SkipSelectableExpressionWrapper<_>>`
because we don't have `SkipSelectableExpressionWrapper<_>:
TupleAppend<SkipSelectableExpressionWrapper<_>>`
@weiznich weiznich requested review from Ten0 and a team August 23, 2022 13:44
@weiznich weiznich force-pushed the speedup_compile_times_for_joins branch from feb6ea1 to d5fc92b Compare August 23, 2022 14:13
// trait implementations to the inner type
//
// See https://github.com/diesel-rs/diesel/issues/3223 for details
type DefaultSelection = self::private::SkipSelectableExpressionWrapper<Left::Output>;
Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting... How did you trick the compiler into disabling check? For me, this looks like an insoluble task

Copy link
Member Author

Choose a reason for hiding this comment

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

The relevant line is here: https://github.com/diesel-rs/diesel/pull/3288/files#diff-d0e8a449e498610baa9a5f3086200726d02bd5e1612f5f1d5587c4418baddf0eR482
This impl does not check that the trait holds true for the inner type, so it effectively skips the recursive trait bound check on large tuples, which is the underlying problem.

Copy link
Contributor

Choose a reason for hiding this comment

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

You mean, that the most important thing there that T is not bounded, so the compiler does not do expensive recursive checks as it did before?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes that's the main optimization.

Copy link
Member

@Ten0 Ten0 left a comment

Choose a reason for hiding this comment

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

Apart from these minor suggestions, LGTM for merge :)

diesel/src/query_source/joins.rs Outdated Show resolved Hide resolved
diesel/src/query_source/joins.rs Show resolved Hide resolved
diesel/src/query_source/joins.rs Show resolved Hide resolved
weiznich and others added 3 commits August 25, 2022 08:28
@weiznich weiznich force-pushed the speedup_compile_times_for_joins branch from 60594b4 to d3dc7e5 Compare August 25, 2022 07:37
@weiznich weiznich merged commit 91c16b4 into diesel-rs:master Aug 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants