Rust: Improve performance of two type inference predicates#21706
Merged
hvitved merged 2 commits intogithub:mainfrom Apr 14, 2026
Merged
Rust: Improve performance of two type inference predicates#21706hvitved merged 2 commits intogithub:mainfrom
hvitved merged 2 commits intogithub:mainfrom
Conversation
Before
```
Pipeline standard for TypeInference::M2::SatisfiesConstraintWithTypeMatching<TypeInference::FunctionCallMatching::AccessConstraint::RelevantAccess,TypeMention::TypeMention,TypeInference::FunctionCallMatching::AccessConstraint::SatisfiesTypeParameterConstraintInput>::TermIsInstantiationOfCondition::typeParametersHaveEqualInstantiationToIndex/4#dde26112@d5eb7x9q was evaluated in 471 iterations totaling 24306ms (delta sizes total: 42097188).
5676156578 ~1% {7} r1 = JOIN `TypeInference::M2::SatisfiesConstraintWithTypeMatching<TypeInference::FunctionCallMatching::AccessConstraint::RelevantAccess,TypeMention::TypeMention,TypeInference::FunctionCallMatching::AccessConstraint::SatisfiesTypeParameterConstraintInput>::TermIsInstantiationOfCondition::typeParametersHaveEqualInstantiationToIndex/4#dde26112#prev_delta` WITH `TypeInference::M2::SatisfiesConstraintWithTypeMatching<TypeInference::FunctionCallMatching::AccessConstraint::RelevantAccess,TypeMention::TypeMention,TypeInference::FunctionCallMatching::AccessConstraint::SatisfiesTypeParameterConstraintInput>::TermIsInstantiationOfCondition::typeParametersEqual/4#a276e5d4#prev` ON FIRST 3 OUTPUT Lhs.0, Lhs.1, Lhs.2, Rhs.3, _, Lhs.3, _
{5} | REWRITE WITH Tmp.4 := 1, Out.4 := (Tmp.4 + In.5), Tmp.6 := 0, TEST Out.4 != Tmp.6 KEEPING 5
5676156578 ~1% {5} | SCAN OUTPUT In.1, In.4, In.3, In.0, In.2
41691564 ~1% {4} | JOIN WITH `TypeInference::M2::SatisfiesConstraintWithTypeMatching<TypeInference::AssocFunctionResolution::ArgSatisfiesBlanketLikeConstraint::ArgumentTypeAndBlanketOffset,TypeMention::TypeMention,TypeInference::AssocFunctionResolution::ArgSatisfiesBlanketLikeConstraint::SatisfiesBlanketConstraint::Inp>::TermIsInstantiationOfCondition::getNthTypeParameter/2#40c66343` ON FIRST 3 OUTPUT Lhs.3, Lhs.0, Lhs.4, Lhs.1
42097188 ~2% {4} r2 = SCAN `TypeInference::M2::SatisfiesConstraintWithTypeMatching<TypeInference::FunctionCallMatching::AccessConstraint::RelevantAccess,TypeMention::TypeMention,TypeInference::FunctionCallMatching::AccessConstraint::SatisfiesTypeParameterConstraintInput>::TermIsInstantiationOfCondition::typeParametersEqual/4#a276e5d4#prev_delta` OUTPUT In.1, In.3, In.0, In.2
42097188 ~1% {5} r3 = JOIN r2 WITH `TypeInference::M2::SatisfiesConstraintWithTypeMatching<TypeInference::AssocFunctionResolution::ArgSatisfiesBlanketLikeConstraint::ArgumentTypeAndBlanketOffset,TypeMention::TypeMention,TypeInference::AssocFunctionResolution::ArgSatisfiesBlanketLikeConstraint::SatisfiesBlanketConstraint::Inp>::TermIsInstantiationOfCondition::getNthTypeParameter/2#40c66343_021#join_rhs` ON FIRST 2 OUTPUT Lhs.2, Lhs.0, Lhs.3, Rhs.2, _
{4} | REWRITE WITH Tmp.4 := 0, TEST InOut.3 != Tmp.4 KEEPING 4
41691564 ~1% {5} | SCAN OUTPUT In.0, In.1, In.2, _, In.3
41691564 ~1% {5} | REWRITE WITH Tmp.3 := 1, Out.3 := (InOut.4 - Tmp.3)
0 ~0% {4} | JOIN WITH `TypeInference::M2::SatisfiesConstraintWithTypeMatching<TypeInference::FunctionCallMatching::AccessConstraint::RelevantAccess,TypeMention::TypeMention,TypeInference::FunctionCallMatching::AccessConstraint::SatisfiesTypeParameterConstraintInput>::TermIsInstantiationOfCondition::typeParametersHaveEqualInstantiationToIndex/4#dde26112#prev` ON FIRST 4 OUTPUT Lhs.0, Lhs.1, Lhs.2, Lhs.4
42097188 ~0% {6} r4 = JOIN r2 WITH `TypeInference::M2::SatisfiesConstraintWithTypeMatching<TypeInference::AssocFunctionResolution::ArgSatisfiesBlanketLikeConstraint::ArgumentTypeAndBlanketOffset,TypeMention::TypeMention,TypeInference::AssocFunctionResolution::ArgSatisfiesBlanketLikeConstraint::SatisfiesBlanketConstraint::Inp>::TermIsInstantiationOfCondition::getNthTypeParameter/2#40c66343_021#join_rhs` ON FIRST 2 OUTPUT Lhs.2, Lhs.0, Lhs.3, Lhs.1, Rhs.2, _
{5} | REWRITE WITH Tmp.5 := 0, TEST InOut.4 = Tmp.5 KEEPING 5
405624 ~1% {5} | SCAN OUTPUT In.1, _, In.3, In.0, In.2
405624 ~1% {5} | REWRITE WITH Out.1 := 0
405624 ~0% {4} | JOIN WITH `TypeInference::M2::SatisfiesConstraintWithTypeMatching<TypeInference::AssocFunctionResolution::ArgSatisfiesBlanketLikeConstraint::ArgumentTypeAndBlanketOffset,TypeMention::TypeMention,TypeInference::AssocFunctionResolution::ArgSatisfiesBlanketLikeConstraint::SatisfiesBlanketConstraint::Inp>::TermIsInstantiationOfCondition::getNthTypeParameter/2#40c66343` ON FIRST 3 OUTPUT Lhs.3, Lhs.0, Lhs.4, _
405624 ~1% {4} | REWRITE WITH Out.3 := 0
42097188 ~1% {4} r5 = r1 UNION r3 UNION r4
42097188 ~1% {4} | AND NOT `TypeInference::M2::SatisfiesConstraintWithTypeMatching<TypeInference::FunctionCallMatching::AccessConstraint::RelevantAccess,TypeMention::TypeMention,TypeInference::FunctionCallMatching::AccessConstraint::SatisfiesTypeParameterConstraintInput>::TermIsInstantiationOfCondition::typeParametersHaveEqualInstantiationToIndex/4#dde26112#prev`(FIRST 4)
return r5
```
After
```
Pipeline standard for TypeInference::M2::SatisfiesConstraintWithTypeMatching<TypeInference::FunctionCallMatching::AccessConstraint::RelevantAccess,TypeMention::TypeMention,TypeInference::FunctionCallMatching::AccessConstraint::SatisfiesTypeParameterConstraintInput>::TermIsInstantiationOfCondition::typeParametersHaveEqualInstantiationToIndex/4#dde26112@96df1x2u was evaluated in 471 iterations totaling 4058ms (delta sizes total: 42097188).
42097188 ~0% {4} r1 = SCAN `TypeInference::M2::SatisfiesConstraintWithTypeMatching<TypeInference::FunctionCallMatching::AccessConstraint::RelevantAccess,TypeMention::TypeMention,TypeInference::FunctionCallMatching::AccessConstraint::SatisfiesTypeParameterConstraintInput>::TermIsInstantiationOfCondition::typeParametersEqual/5#ddfcf430#prev_delta` OUTPUT In.3, In.0, In.1, In.2
405624 ~0% {4} | JOIN WITH const_0 ON FIRST 1 OUTPUT Lhs.1, Lhs.2, Lhs.3, _
405624 ~1% {4} | REWRITE WITH Out.3 := 0
42097188 ~1% {6} r2 = SCAN `TypeInference::M2::SatisfiesConstraintWithTypeMatching<TypeInference::FunctionCallMatching::AccessConstraint::RelevantAccess,TypeMention::TypeMention,TypeInference::FunctionCallMatching::AccessConstraint::SatisfiesTypeParameterConstraintInput>::TermIsInstantiationOfCondition::typeParametersHaveEqualInstantiationToIndex/4#dde26112#prev_delta` OUTPUT In.0, In.1, In.2, _, In.3, _
42097188 ~1% {4} | REWRITE WITH Tmp.3 := 1, Out.3 := (Tmp.3 + In.4), Tmp.5 := 0, TEST Out.3 != Tmp.5 KEEPING 4
41691564 ~1% {4} | JOIN WITH `TypeInference::M2::SatisfiesConstraintWithTypeMatching<TypeInference::FunctionCallMatching::AccessConstraint::RelevantAccess,TypeMention::TypeMention,TypeInference::FunctionCallMatching::AccessConstraint::SatisfiesTypeParameterConstraintInput>::TermIsInstantiationOfCondition::typeParametersEqual/5#ddfcf430#prev` ON FIRST 4 OUTPUT Lhs.0, Lhs.1, Lhs.2, Lhs.3
42097188 ~1% {6} r3 = SCAN `TypeInference::M2::SatisfiesConstraintWithTypeMatching<TypeInference::FunctionCallMatching::AccessConstraint::RelevantAccess,TypeMention::TypeMention,TypeInference::FunctionCallMatching::AccessConstraint::SatisfiesTypeParameterConstraintInput>::TermIsInstantiationOfCondition::typeParametersEqual/5#ddfcf430#prev_delta` OUTPUT In.0, In.1, In.2, In.3, In.4, _
{5} | REWRITE WITH Tmp.5 := 0, TEST InOut.3 != Tmp.5 KEEPING 5
41691564 ~1% {5} | SCAN OUTPUT In.0, In.1, In.2, _, In.3
41691564 ~1% {5} | REWRITE WITH Tmp.3 := 1, Out.3 := (InOut.4 - Tmp.3)
0 ~0% {4} | JOIN WITH `TypeInference::M2::SatisfiesConstraintWithTypeMatching<TypeInference::FunctionCallMatching::AccessConstraint::RelevantAccess,TypeMention::TypeMention,TypeInference::FunctionCallMatching::AccessConstraint::SatisfiesTypeParameterConstraintInput>::TermIsInstantiationOfCondition::typeParametersHaveEqualInstantiationToIndex/4#dde26112#prev` ON FIRST 4 OUTPUT Lhs.0, Lhs.1, Lhs.2, Lhs.4
42097188 ~1% {4} r4 = r1 UNION r2 UNION r3
42097188 ~1% {4} | AND NOT `TypeInference::M2::SatisfiesConstraintWithTypeMatching<TypeInference::FunctionCallMatching::AccessConstraint::RelevantAccess,TypeMention::TypeMention,TypeInference::FunctionCallMatching::AccessConstraint::SatisfiesTypeParameterConstraintInput>::TermIsInstantiationOfCondition::typeParametersHaveEqualInstantiationToIndex/4#dde26112#prev`(FIRST 4)
return r4
```
Before
```
Pipeline standard for TypeInference::AssocFunctionResolution::AssocFunctionCall.hasIncompatibleTarget/5#85c07422@d5eb7r0w was evaluated in 782 iterations totaling 13208ms (delta sizes total: 20187834).
1464 ~2% {7} r1 = JOIN `TypeInference::AssocFunctionResolution::SelfArgIsInstantiationOf::argIsInstantiationOf/6#aaa87ac9#prev_delta` WITH `TypeInference::AssocFunctionResolution::OverloadedCallArgsAreInstantiationsOf::argsAreNotInstantiationsOf/2#6a6070f7#prev` ON FIRST 2 OUTPUT Lhs.5, _, Lhs.0, Lhs.1, Lhs.2, Lhs.3, Lhs.4
1464 ~0% {7} | REWRITE WITH Out.1 := ""
1464 ~0% {6} | JOIN WITH `FunctionType::AssocFunctionType.getTypeAt/1#dispred#d4d46f61` ON FIRST 2 OUTPUT Lhs.2, Lhs.3, Lhs.4, Lhs.5, Lhs.6, Rhs.2
173691 ~1% {7} r2 = JOIN `TypeInference::AssocFunctionResolution::OverloadedCallArgsAreInstantiationsOf::argsAreNotInstantiationsOf/2#6a6070f7#prev_delta` WITH `TypeInference::AssocFunctionResolution::SelfArgIsInstantiationOf::argIsInstantiationOf/6#aaa87ac9#prev` ON FIRST 2 OUTPUT Rhs.5, _, Lhs.0, Lhs.1, Rhs.2, Rhs.3, Rhs.4
173691 ~1% {7} | REWRITE WITH Out.1 := ""
173691 ~1% {6} | JOIN WITH `FunctionType::AssocFunctionType.getTypeAt/1#dispred#d4d46f61` ON FIRST 2 OUTPUT Lhs.2, Lhs.3, Lhs.4, Lhs.5, Lhs.6, Rhs.2
20022454 ~0% {7} r3 = SCAN `TypeInference::AssocFunctionResolution::SelfArgIsInstantiationOf::argIsNotInstantiationOf/6#1b8e512e#prev_delta` OUTPUT In.0, In.1, In.2, In.3, In.4, In.5, _
20022454 ~0% {7} | REWRITE WITH Out.6 := "^([0-9]+)\\..*$"
20022175 ~2% {9} | JOIN WITH PRIMITIVE regexpCapture#bbff ON Lhs.5,Lhs.6
20022175 ~2% {10} | SCAN OUTPUT In.0, In.1, In.2, In.3, In.4, In.5, In.6, In.7, In.8, _
{9} | REWRITE WITH Tmp.9 := 1, TEST InOut.7 = Tmp.9 KEEPING 9
20022175 ~1% {7} | SCAN OUTPUT In.8, In.0, In.1, In.2, In.3, In.4, In.5
20022175 ~1% {8} | JOIN WITH `UnboundList::Make<Locations::Location,TypeInference::M1::UnboundListInput>::encode/1#47b2ec3f_10#join_rhs` ON FIRST 1 OUTPUT Rhs.1, Lhs.1, Lhs.2, Lhs.3, Lhs.4, Lhs.5, Lhs.6, Lhs.0
20022175 ~0% {10} | JOIN WITH `Type::Type.getATypeParameter/0#dispred#ddf0e8ff_10#join_rhs` ON FIRST 1 OUTPUT Lhs.1, Lhs.2, Lhs.3, Lhs.4, Lhs.5, Rhs.1, _, Lhs.6, Lhs.7, _
{7} | REWRITE WITH Tmp.6 := length(In.8), Tmp.9 := 1, Tmp.6 := (Tmp.6 + Tmp.9), Out.6 := suffix(In.7,Tmp.6) KEEPING 7
20022175 ~0% {6} | SCAN OUTPUT In.0, In.1, In.2, In.3, In.4, In.5
20197330 ~0% {6} r4 = r1 UNION r2 UNION r3
20187834 ~0% {6} | AND NOT `TypeInference::AssocFunctionResolution::AssocFunctionCall.hasIncompatibleTarget/5#85c07422#prev`(FIRST 6)
return r4
```
After
```
Pipeline standard for TypeInference::AssocFunctionResolution::AssocFunctionCall.hasIncompatibleTarget/5#85c07422@a58ce91w was evaluated in 537 iterations totaling 382ms (delta sizes total: 20033950).
19862347 ~0% {7} r1 = SCAN `TypeInference::AssocFunctionResolution::SelfArgIsInstantiationOf::argIsNotInstantiationOf/6#1b8e512e#prev_delta` OUTPUT In.5, _, In.0, In.1, In.2, In.3, In.4
19862347 ~0% {7} | REWRITE WITH Out.1 := ""
174684 ~1% {7} r2 = SCAN `TypeInference::AssocFunctionResolution::AssocFunctionCall.hasIncompatibleArgsTarget/5#dispred#7d49b9f9#prev_delta` OUTPUT In.5, _, In.0, In.1, In.2, In.3, In.4
174684 ~1% {7} | REWRITE WITH Out.1 := ""
20037031 ~0% {7} r3 = r1 UNION r2
20037031 ~0% {6} | JOIN WITH `FunctionType::AssocFunctionType.getTypeAt/1#dispred#d4d46f61` ON FIRST 2 OUTPUT Lhs.2, Lhs.3, Lhs.4, Lhs.5, Lhs.6, Rhs.2
20033950 ~0% {6} | AND NOT `TypeInference::AssocFunctionResolution::AssocFunctionCall.hasIncompatibleTarget/5#85c07422#prev`(FIRST 6)
return r3
```
da98941 to
d69be77
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors two internal type-inference predicates (one in the shared type inference library and one in the Rust-specific type inference library) with the goal of improving evaluation performance.
Changes:
- Shared type inference: refactors
typeParametersEqual/typeParametersHaveEqualInstantiationToIndexto iterate by type-parameter index and addspragma[nomagic]to the refactored predicate. - Rust type inference: factors out an intermediate predicate (
hasIncompatibleArgsTarget) and restructureshasIncompatibleTargetto reduce duplicated work; updatesSelfArgIsInstantiationOf::argIsNotInstantiationOfto takeAssocFunctionType selfTypeand filter non-empty paths.
Show a summary per file
| File | Description |
|---|---|
| shared/typeinference/codeql/typeinference/internal/TypeInference.qll | Refactors type-parameter equality checking to work by index and marks the predicate nomagic to improve performance characteristics. |
| rust/ql/lib/codeql/rust/internal/typeinference/TypeInference.qll | Refactors incompatible-target filtering by extracting shared logic into a helper predicate and adjusting related predicate signatures/conditions. |
Copilot's findings
- Files reviewed: 2/2 changed files
- Comments generated: 0
aschackmull
approved these changes
Apr 14, 2026
Contributor
aschackmull
left a comment
There was a problem hiding this comment.
I'm missing a little bit of context to validate the second commit (the refactor relies on some relation between selfType and path that's not apparent in the diff), but otherwise looks good, so Looks Plausible To Me.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
See commit messages for details.