Skip to content

Rust: Type inference refactor and improve join orders #20076

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

Merged
merged 3 commits into from
Jul 23, 2025

Conversation

paldepind
Copy link
Contributor

@paldepind paldepind commented Jul 17, 2025

This PR makes a few refactors to the type inference library.

I think these changes make sense on their own, but on databend they also reduce the total number of tuples (observed using "Compare Performance" in VSCode) and the following bad join reported by VScode (also observed on databend) disappears:

Bad join
Pipeline standard for TypeInference::CallExprBaseMatching::typeConstraintBaseTypeMatch/5#ebeb4216@f3cabgyd was evaluated in 530 iterations totaling 145ms (delta sizes total: 36047).
             5442   ~0%    {5} r1 = JOIN `TypeInference::CallExprBaseMatchingInput::Access.getTarget/0#dispred#29d66fd1#prev_delta` WITH TypeInference::CallExprBaseMatching::AccessConstraint::MkRelevantAccess#8a6252ad#prev ON FIRST 1 OUTPUT Rhs.2, Lhs.0, Lhs.1, Rhs.3, Rhs.4
             5442   ~0%    {5}    | JOIN WITH `TypeInference::CallExprBaseMatchingInput::accessDeclarationPositionMatch/2#6c58c89d` ON FIRST 1 OUTPUT Lhs.2, Rhs.1, Lhs.3, Lhs.1, Lhs.4
              240   ~0%    {6}    | JOIN WITH `project#TypeInference::CallExprBaseMatching::typeParameterConstraintHasTypeParameter/7#7f1e227b#2` ON FIRST 3 OUTPUT Lhs.3, Lhs.0, Lhs.4, Rhs.3, Rhs.4, Rhs.5
                           {6}    | AND NOT `_GenericParamList::GenericParamList.getTypeParam/1#dispred#01a987e9_21#join_rhs_Type::Type#75554d9d___#antijoin_rhs#1`(FIRST 6)
              184   ~0%    {6}    | SCAN OUTPUT In.2, In.3, In.0, In.1, In.4, In.5
                0   ~0%    {7}    | JOIN WITH `TypeInference::M2::SatisfiesConstraint<TypeInference::CallExprBaseMatching::AccessConstraint::RelevantAccess,TypeInference::CallExprBaseMatching::AccessConstraint::SatisfiesConstraintInput>::satisfiesConstraintType/4#d5b1679c#prev` ON FIRST 2 OUTPUT Lhs.2, Lhs.3, Lhs.5, Rhs.3, _, Lhs.4, Rhs.2
                           {5}    | REWRITE WITH Tmp.4 := "", Out.4 := InverseAppend(In.5,Tmp.4,In.6) KEEPING 5
                0   ~0%    {5}    | SCAN OUTPUT In.0, In.1, In.4, In.3, In.2
                       
          2676185   ~2%    {6} r2 = JOIN `TypeInference::M2::SatisfiesConstraint<TypeInference::CallExprBaseMatching::AccessConstraint::RelevantAccess,TypeInference::CallExprBaseMatching::AccessConstraint::SatisfiesConstraintInput>::satisfiesConstraintType/4#d5b1679c#prev_delta` WITH TypeInference::CallExprBaseMatching::AccessConstraint::MkRelevantAccess#8a6252ad#reorder_4_0_1_2_3#prev ON FIRST 1 OUTPUT Rhs.3, Lhs.1, Lhs.2, Lhs.3, Rhs.1, Rhs.4
          2669957   ~3%    {6}    | JOIN WITH `TypeInference::CallExprBaseMatchingInput::accessDeclarationPositionMatch/2#6c58c89d` ON FIRST 1 OUTPUT Lhs.4, Lhs.1, Lhs.2, Lhs.3, Lhs.5, Rhs.1
        143545032   ~8%    {7}    | JOIN WITH `TypeInference::CallExprBaseMatchingInput::Access.getTarget/0#dispred#29d66fd1#prev` ON FIRST 1 OUTPUT Rhs.1, Lhs.5, Lhs.4, Lhs.1, Lhs.2, Lhs.3, Lhs.0
            63836   ~1%    {6}    | JOIN WITH `project#TypeInference::CallExprBaseMatching::typeParameterConstraintHasTypeParameter/7#7f1e227b#2` ON FIRST 4 OUTPUT Lhs.4, Lhs.5, Lhs.6, Lhs.0, Rhs.4, Rhs.5
                           {6}    | AND NOT `_GenericParamList::GenericParamList.getTypeParam/1#dispred#01a987e9_21#join_rhs_Type::Type#75554d9d___#antijoin_rhs#2`(FIRST 6)
            63701   ~0%    {7}    | SCAN OUTPUT In.1, In.2, In.3, In.5, _, In.4, In.0
                           {5}    | REWRITE WITH Tmp.4 := "", Out.4 := InverseAppend(In.5,Tmp.4,In.6) KEEPING 5
            42102   ~1%    {5}    | SCAN OUTPUT In.1, In.2, In.4, In.0, In.3
                       
            65650   ~1%    {4} r3 = SCAN TypeInference::CallExprBaseMatching::AccessConstraint::MkRelevantAccess#8a6252ad#prev_delta OUTPUT In.0, In.2, In.3, In.4
            80414   ~1%    {5}    | JOIN WITH `TypeInference::CallExprBaseMatchingInput::Access.getTarget/0#dispred#29d66fd1#prev` ON FIRST 1 OUTPUT Lhs.1, Lhs.0, Lhs.2, Lhs.3, Rhs.1
            80414   ~0%    {5}    | JOIN WITH `TypeInference::CallExprBaseMatchingInput::accessDeclarationPositionMatch/2#6c58c89d` ON FIRST 1 OUTPUT Lhs.4, Rhs.1, Lhs.2, Lhs.1, Lhs.3
            98323   ~1%    {6}    | JOIN WITH `project#TypeInference::CallExprBaseMatching::typeParameterConstraintHasTypeParameter/7#7f1e227b#2` ON FIRST 3 OUTPUT Lhs.3, Lhs.4, Lhs.0, Rhs.3, Rhs.4, Rhs.5
                           {6}    | AND NOT `_GenericParamList::GenericParamList.getTypeParam/1#dispred#01a987e9_21#join_rhs_Type::Type#75554d9d___#antijoin_rhs#3`(FIRST 6)
            98176   ~0%    {6}    | SCAN OUTPUT In.1, In.3, In.0, In.2, In.4, In.5
                0   ~0%    {7}    | JOIN WITH `TypeInference::M2::SatisfiesConstraint<TypeInference::CallExprBaseMatching::AccessConstraint::RelevantAccess,TypeInference::CallExprBaseMatching::AccessConstraint::SatisfiesConstraintInput>::satisfiesConstraintType/4#d5b1679c#prev` ON FIRST 2 OUTPUT Lhs.2, Lhs.3, Lhs.5, Rhs.3, _, Lhs.4, Rhs.2
                           {5}    | REWRITE WITH Tmp.4 := "", Out.4 := InverseAppend(In.5,Tmp.4,In.6) KEEPING 5
                0   ~0%    {5}    | SCAN OUTPUT In.0, In.1, In.4, In.3, In.2
                       
            42102   ~1%    {5} r4 = r1 UNION r2 UNION r3
            36047   ~1%    {5}    | AND NOT `TypeInference::CallExprBaseMatching::typeConstraintBaseTypeMatch/5#ebeb4216#prev`(FIRST 5)
                           return r4
I don't have an "after" join order to show as I can't find the equivalent thing after the refactors.

DCA is uneventful and don't show any change in analysis time 😢

@paldepind paldepind changed the title Shared, Rust: Adjust type inference predicates to better match use sites Shared, Rust: Type inference refactor and improve join orders Jul 17, 2025
@paldepind paldepind added the Rust Pull requests that update Rust code label Jul 17, 2025
@paldepind paldepind force-pushed the rust/type-inference-cleanup-join branch from c00cf2f to 43b2977 Compare July 18, 2025 13:39
@github-actions github-actions bot removed the Rust Pull requests that update Rust code label Jul 18, 2025
@paldepind paldepind changed the title Shared, Rust: Type inference refactor and improve join orders Rust: Type inference refactor and improve join orders Jul 18, 2025
@paldepind paldepind added Rust Pull requests that update Rust code no-change-note-required This PR does not need a change note labels Jul 18, 2025
@paldepind paldepind marked this pull request as ready for review July 19, 2025 08:43
@Copilot Copilot AI review requested due to automatic review settings July 19, 2025 08:43
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors the Rust type inference library to improve join orders and reduce computational overhead. The changes focus on optimizing predicate evaluation and streamlining constraint satisfaction logic.

Key changes:

  • Extracted multipleConstraintImplementations predicate to improve reusability and join ordering
  • Moved hasTypeConstraint predicate definition earlier for better join optimization
  • Simplified typeParameterConstraintHasTypeParameter predicate by reducing unnecessary joins and intermediate variables

@@ -989,7 +996,7 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
path = prefix0.append(suffix)
)
or
tt.getTypeAt(TypePath::nil()) = constraint and
hasTypeConstraint(tt, constraint, constraint) and
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The crux here is hat hasTypeConstraint includes relevantConstraint. Before that this disjunct allowed constraint to be anything tt.getTypeAt(TypePath::nil()) which caused the relation to contain a lot of junk.

Copy link
Contributor

@geoffw0 geoffw0 left a comment

Choose a reason for hiding this comment

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

LGTM, one question.

@@ -1409,15 +1413,9 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
Access a, Declaration target, TypePath path, Type t, TypeParameter tp
) {
not exists(getTypeArgument(a, target, tp, _)) and
target = a.getTarget() and
Copy link
Contributor

Choose a reason for hiding this comment

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

What has happened to this constraint?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good question. It's implied by AccessConstraint::satisfiesConstraintType(a, target, ...) so including it here didn't add anything.

@paldepind paldepind merged commit 49ecc60 into github:main Jul 23, 2025
41 checks passed
@paldepind paldepind deleted the rust/type-inference-cleanup-join branch July 23, 2025 10:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-change-note-required This PR does not need a change note Rust Pull requests that update Rust code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy