Skip to content

Michaelrfairhurst/compatible types performance fix alt #891

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

MichaelRFairhurst
Copy link
Contributor

Description

Alternate change to also test and compare results

Change request type

  • Release or process automation (GitHub workflows, internal scripts)
  • Internal documentation
  • External documentation
  • Query files (.ql, .qll, .qls or unit tests)
  • External scripts (analysis report or other code shipped as part of a release)

Rules with added or modified queries

  • No rules added
  • Queries have been added for the following rules:
    • rule number here
  • Queries have been modified for the following rules:
    • rule number here

Release change checklist

A change note (development_handbook.md#change-notes) is required for any pull request which modifies:

  • The structure or layout of the release artifacts.
  • The evaluation performance (memory, execution time) of an existing query.
  • The results of an existing query in any circumstance.

If you are only adding new rule queries, a change note is not required.

Author: Is a change note required?

  • Yes
  • No

🚨🚨🚨
Reviewer: Confirm that format of shared queries (not the .qll file, the
.ql file that imports it) is valid by running them within VS Code.

  • Confirmed

Reviewer: Confirm that either a change note is not required or the change note is required and has been added.

  • Confirmed

Query development review checklist

For PRs that add new queries or modify existing queries, the following checklist should be completed by both the author and reviewer:

Author

  • Have all the relevant rule package description files been checked in?
  • Have you verified that the metadata properties of each new query is set appropriately?
  • Do all the unit tests contain both "COMPLIANT" and "NON_COMPLIANT" cases?
  • Are the alert messages properly formatted and consistent with the style guide?
  • Have you run the queries on OpenPilot and verified that the performance and results are acceptable?
    As a rule of thumb, predicates specific to the query should take no more than 1 minute, and for simple queries be under 10 seconds. If this is not the case, this should be highlighted and agreed in the code review process.
  • Does the query have an appropriate level of in-query comments/documentation?
  • Have you considered/identified possible edge cases?
  • Does the query not reinvent features in the standard library?
  • Can the query be simplified further (not golfed!)

Reviewer

  • Have all the relevant rule package description files been checked in?
  • Have you verified that the metadata properties of each new query is set appropriately?
  • Do all the unit tests contain both "COMPLIANT" and "NON_COMPLIANT" cases?
  • Are the alert messages properly formatted and consistent with the style guide?
  • Have you run the queries on OpenPilot and verified that the performance and results are acceptable?
    As a rule of thumb, predicates specific to the query should take no more than 1 minute, and for simple queries be under 10 seconds. If this is not the case, this should be highlighted and agreed in the code review process.
  • Does the query have an appropriate level of in-query comments/documentation?
  • Have you considered/identified possible edge cases?
  • Does the query not reinvent features in the standard library?
  • Can the query be simplified further (not golfed!)

…oin orders.

More pragmas added to encourage the join ordering pipeline to make function
comparisons more efficient.

New approach in type equivalence assumes that all types are trivially equivalent
to themselves. Therefore, only type comparisons between non-identical types need
to be considered as interesting roots. The types that are reachable in the type
graph from these roots are the ones considered by the recursive type equivalence
predicate.
@Copilot Copilot AI review requested due to automatic review settings April 27, 2025 08:51
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 introduces change notes documenting performance improvements for type comparisons in several CodeQL queries.

  • Added pragmas to control join order on function parameter equivalence.
  • Refactored expressions to avoid optimizer confusion that produced Cartesian products.
  • Adjusted modules (Compatible.qll and SimpleAssignment.qll) to limit expensive equality checks on types.
Files not reviewed (8)
  • c/cert/src/rules/DCL40-C/IncompatibleFunctionDeclarations.ql: Language not supported
  • c/misra/src/rules/RULE-23-5/DangerousDefaultSelectionForPointerInGeneric.ql: Language not supported
  • c/misra/src/rules/RULE-8-3/DeclarationsOfAFunctionSameNameAndType.ql: Language not supported
  • c/misra/src/rules/RULE-8-3/DeclarationsOfAnObjectSameNameAndType.ql: Language not supported
  • c/misra/src/rules/RULE-8-4/CompatibleDeclarationFunctionDefined.ql: Language not supported
  • c/misra/src/rules/RULE-8-4/CompatibleDeclarationObjectDefined.ql: Language not supported
  • cpp/common/src/codingstandards/cpp/types/Compatible.qll: Language not supported
  • cpp/common/src/codingstandards/cpp/types/SimpleAssignment.qll: Language not supported

@@ -0,0 +1,6 @@
- `RULE-8-3`, `RULE-8-4`, `DCL40-C`, `RULE-23-5`: `DeclarationsOfAFunctionSameNameAndType.ql`, `DeclarationsOfAnObjectSameNameAndType.ql`, `CompatibleDeclarationOfFunctionDefined.ql`, `CompatibleDeclarationObjectDefined.ql`, `IncompatibleFunctionDeclarations.ql`, `DangerousDefaultSelectionForPointerInGeneric.ql`:
- Added pragmas to alter join order on function parameter equivalence (names and types).
- Refactored expression which the optimizer was confused by, and compiled into a cartesian product.
Copy link
Preview

Copilot AI Apr 27, 2025

Choose a reason for hiding this comment

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

Consider rewording this line for clarity. For instance, specify that the expression was refactored to prevent the optimizer from generating an unintended cartesian product.

Suggested change
- Refactored expression which the optimizer was confused by, and compiled into a cartesian product.
- Refactored an expression to prevent the optimizer from generating an unintended cartesian product, which could lead to performance issues. The refactoring ensures that the join order is explicitly defined, avoiding ambiguity in the optimizer's behavior.

Copilot uses AI. Check for mistakes.

- Added pragmas to alter join order on function parameter equivalence (names and types).
- Refactored expression which the optimizer was confused by, and compiled into a cartesian product.
- Altered the module `Compatible.qll` to only perform expensive equality checks on types that are compared to a non identical other type, and those reachable from those types in the type graph. Types that are identical will trivially be considered equivalent.
- `RULE-23-5`: `DangerousDefaultSelectionForPointerInGeneric.ql`:
Copy link
Preview

Copilot AI Apr 27, 2025

Choose a reason for hiding this comment

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

[nitpick] The RULE-23-5 changes are mentioned both in the consolidated bullet and separately. It would improve clarity to consolidate or better distinguish the separate concerns if intentional.

Suggested change
- `RULE-23-5`: `DangerousDefaultSelectionForPointerInGeneric.ql`:

Copilot uses AI. Check for mistakes.

@MichaelRFairhurst
Copy link
Contributor Author

/test-performance

Copy link

🏁 Beep Boop! Performance testing for this PR has been initiated. Please check back later for results. Note that the query package generation step must complete before testing will start so it might be a minute.

💡 If you do not hear back from me please check my status! I will report even if I fail!

🏁 Beep Boop! One or things failed during performance testing. Please check the release engineering repo for details.

@MichaelRFairhurst
Copy link
Contributor Author

/test-performance

Copy link

🏁 Beep Boop! Performance testing for this PR has been initiated. Please check back later for results. Note that the query package generation step must complete before testing will start so it might be a minute.

💡 If you do not hear back from me please check my status! I will report even if I fail!

🏁 Beep Boop! Performance testing complete! See below for performance of the last 3 runs vs your PR. Times are based on predicate performance. You can find full graphs and stats in the PR that was created for this test in the release engineering repo.


Release                            : v2.42.0
Platform                           : x86-windows
Language                           : cpp
Total_Serialized_Execution_Time_Ms : 2687722
Mean_Predicate_Execution_Time_Ms   : 54.08761973758352
Median_Predicate_Execution_Time_Ms : 1.0
Standard_Deviation_Ms              : 374.33936932152056
Total_Serialized_Execution_Time_s  : 2687.722
Mean_Query_Execution_Time_s        : 0.0540876197375835
Median_Predicate_Execution_Time_s  : 0.001
Percentile95_Ms                    : 177.0
Number_of_Predicates               : 49692

Release                            : v2.42.0
Platform                           : x86-windows
Language                           : c
Total_Serialized_Execution_Time_Ms : 3190077
Mean_Predicate_Execution_Time_Ms   : 62.68819761043861
Median_Predicate_Execution_Time_Ms : 0.0
Standard_Deviation_Ms              : 755.9204691024984
Total_Serialized_Execution_Time_s  : 3190.077
Mean_Query_Execution_Time_s        : 0.0626881976104386
Median_Predicate_Execution_Time_s  : 0.0
Percentile95_Ms                    : 168.0
Number_of_Predicates               : 50888

Release                            : v2.42.0
Platform                           : x86-linux
Language                           : cpp
Total_Serialized_Execution_Time_Ms : 3079798
Mean_Predicate_Execution_Time_Ms   : 61.84084976506968
Median_Predicate_Execution_Time_Ms : 0.0
Standard_Deviation_Ms              : 644.1368683954564
Total_Serialized_Execution_Time_s  : 3079.798
Mean_Query_Execution_Time_s        : 0.0618408497650696
Median_Predicate_Execution_Time_s  : 0.0
Percentile95_Ms                    : 141.0
Number_of_Predicates               : 49802

Release                            : v2.42.0
Platform                           : x86-linux
Language                           : c
Total_Serialized_Execution_Time_Ms : 2508565
Mean_Predicate_Execution_Time_Ms   : 49.44446634473243
Median_Predicate_Execution_Time_Ms : 0.0
Standard_Deviation_Ms              : 677.505130040798
Total_Serialized_Execution_Time_s  : 2508.565
Mean_Query_Execution_Time_s        : 0.0494444663447324
Median_Predicate_Execution_Time_s  : 0.0
Percentile95_Ms                    : 112.0
Number_of_Predicates               : 50735

Release                            : v2.43.0
Platform                           : x86-linux
Language                           : c
Total_Serialized_Execution_Time_Ms : 2531408
Mean_Predicate_Execution_Time_Ms   : 49.26068342803767
Median_Predicate_Execution_Time_Ms : 0.0
Standard_Deviation_Ms              : 639.8847265762785
Total_Serialized_Execution_Time_s  : 2531.408
Mean_Query_Execution_Time_s        : 0.0492606834280376
Median_Predicate_Execution_Time_s  : 0.0
Percentile95_Ms                    : 113.0
Number_of_Predicates               : 51388

Release                            : v2.43.0
Platform                           : x86-linux
Language                           : cpp
Total_Serialized_Execution_Time_Ms : 3131459
Mean_Predicate_Execution_Time_Ms   : 62.2729785626218
Median_Predicate_Execution_Time_Ms : 0.0
Standard_Deviation_Ms              : 663.6500547757107
Total_Serialized_Execution_Time_s  : 3131.459
Mean_Query_Execution_Time_s        : 0.0622729785626218
Median_Predicate_Execution_Time_s  : 0.0
Percentile95_Ms                    : 146.0
Number_of_Predicates               : 50286

Release                            : v2.43.0
Platform                           : x86-windows
Language                           : c
Total_Serialized_Execution_Time_Ms : 2675068
Mean_Predicate_Execution_Time_Ms   : 51.75013541747272
Median_Predicate_Execution_Time_Ms : 0.0
Standard_Deviation_Ms              : 547.9728701007625
Total_Serialized_Execution_Time_s  : 2675.068
Mean_Query_Execution_Time_s        : 0.0517501354174727
Median_Predicate_Execution_Time_s  : 0.0
Percentile95_Ms                    : 147.0
Number_of_Predicates               : 51692

Release                            : v2.43.0
Platform                           : x86-windows
Language                           : cpp
Total_Serialized_Execution_Time_Ms : 2820280
Mean_Predicate_Execution_Time_Ms   : 55.86040247187451
Median_Predicate_Execution_Time_Ms : 0.0
Standard_Deviation_Ms              : 472.6115929503292
Total_Serialized_Execution_Time_s  : 2820.28
Mean_Query_Execution_Time_s        : 0.0558604024718745
Median_Predicate_Execution_Time_s  : 0.0
Percentile95_Ms                    : 172.0
Number_of_Predicates               : 50488

Release                            : 891
Platform                           : x86-linux
Language                           : cpp
Total_Serialized_Execution_Time_Ms : 2798218
Mean_Predicate_Execution_Time_Ms   : 55.84596655091207
Median_Predicate_Execution_Time_Ms : 0.0
Standard_Deviation_Ms              : 560.1566177480564
Total_Serialized_Execution_Time_s  : 2798.218
Mean_Query_Execution_Time_s        : 0.055845966550912
Median_Predicate_Execution_Time_s  : 0.0
Percentile95_Ms                    : 144.0
Number_of_Predicates               : 50106

Release                            : 891
Platform                           : x86-linux
Language                           : c
Total_Serialized_Execution_Time_Ms : 2659666
Mean_Predicate_Execution_Time_Ms   : 49.58178293128519
Median_Predicate_Execution_Time_Ms : 0.0
Standard_Deviation_Ms              : 633.0634252137475
Total_Serialized_Execution_Time_s  : 2659.666
Mean_Query_Execution_Time_s        : 0.0495817829312851
Median_Predicate_Execution_Time_s  : 0.0
Percentile95_Ms                    : 122.0
Number_of_Predicates               : 53642

🏁 Below are the slowest predicates for the last 2 releases vs this PR.


Release           : v2.43.0
Run               : 2025-03-24_15-27-45
Platform          : x86-windows
Language          : cpp
Suite             : autosar-default
Predicate         : UnusedVariables::maybeACompileTimeTemplateArgument/1#9ea118f0
Execution_Time_Ms : 60562

Release           : v2.43.0
Run               : 2025-03-24_15-27-45
Platform          : x86-windows
Language          : cpp
Suite             : autosar-default
Predicate         : _ExternalFlow::elementSpecMatchesSignature/6#c67d3446_Function::Function.getClassAndName/1#27b7404e___#shared
Execution_Time_Ms : 19609

Release           : v2.43.0
Run               : 2025-03-24_15-27-45
Platform          : x86-linux
Language          : c
Suite             : misra-default
Predicate         : _Call::FunctionCall#2b14a517_Call::FunctionCall.getTarget/0#dispred#935da4c5_Function::Function.getP__#antijoin_rhs
Execution_Time_Ms : 27923

Release           : v2.43.0
Run               : 2025-03-24_15-27-45
Platform          : x86-windows
Language          : cpp
Suite             : autosar-default
Predicate         : num#FunctionEquivalence::TParameter#9a1b3813
Execution_Time_Ms : 23515

Release           : v2.43.0
Run               : 2025-03-24_15-27-45
Platform          : x86-windows
Language          : cpp
Suite             : autosar-default
Predicate         : CheckedException::CheckedException#b0aa5ec8
Execution_Time_Ms : 18560

Release           : v2.43.0
Run               : 2025-03-24_15-27-45
Platform          : x86-windows
Language          : cpp
Suite             : autosar-default
Predicate         : _Declaration::Declaration.getADeclarationEntry/0#dispred#c5d61b67_Declaration::DeclarationEntry.isDe__#antijoin_rhs
Execution_Time_Ms : 24130

Release           : v2.43.0
Run               : 2025-03-24_15-27-45
Platform          : x86-windows
Language          : c
Suite             : misra-default
Predicate         : OutOfBounds::OOB::libraryFunctionNameParamTable/5#79217c12
Execution_Time_Ms : 30834

Release           : v2.43.0
Run               : 2025-03-24_15-27-45
Platform          : x86-linux
Language          : cpp
Suite             : autosar-default
Predicate         : _ExternalFlow::elementSpecMatchesSignature/6#c67d3446_Function::Function.getClassAndName/1#27b7404e___#shared
Execution_Time_Ms : 29420

Release           : v2.43.0
Run               : 2025-03-24_15-27-45
Platform          : x86-windows
Language          : c
Suite             : misra-default
Predicate         : QualifiedName::getUserTypeNameWithoutArgs/1#8cfc98e9
Execution_Time_Ms : 26730

Release           : v2.43.0
Run               : 2025-03-24_15-27-45
Platform          : x86-windows
Language          : c
Suite             : misra-default
Predicate         : _Macro::Macro.getName/0#dispred#e28b3699_Preprocessor::PreprocessorBranchDirective#bcd2bde4_Preproce__#antijoin_rhs
Execution_Time_Ms : 51594

Release           : v2.43.0
Run               : 2025-03-24_15-27-45
Platform          : x86-windows
Language          : c
Suite             : cert-default
Predicate         : OutOfBounds::OOB::libraryFunctionNameParamTable/5#79217c12
Execution_Time_Ms : 23351

Release           : v2.43.0
Run               : 2025-03-24_15-27-45
Platform          : x86-linux
Language          : cpp
Suite             : autosar-default
Predicate         : CheckedException::CheckedException#b0aa5ec8
Execution_Time_Ms : 32235

Release           : v2.43.0
Run               : 2025-03-24_15-27-45
Platform          : x86-windows
Language          : c
Suite             : misra-default
Predicate         : _ExternalFlow::elementSpecMatchesSignature/6#c67d3446_Function::Function.getClassAndName/1#27b7404e___#shared
Execution_Time_Ms : 53879

Release           : v2.43.0
Run               : 2025-03-24_15-27-45
Platform          : x86-linux
Language          : cpp
Suite             : autosar-default
Predicate         : QualifiedName::getUserTypeNameWithoutArgs/1#8cfc98e9
Execution_Time_Ms : 33492

Release           : v2.43.0
Run               : 2025-03-24_15-27-45
Platform          : x86-linux
Language          : cpp
Suite             : autosar-default
Predicate         : UnusedVariables::maybeACompileTimeTemplateArgument/1#9ea118f0
Execution_Time_Ms : 67141

Release           : v2.43.0
Run               : 2025-03-24_15-27-45
Platform          : x86-linux
Language          : cpp
Suite             : autosar-default
Predicate         : CharacterOutsideTheLanguageStandardBasicSourceCharacterSetUsedInTheSourceCode::getUniversalCharacterName/1#36dbaa42
Execution_Time_Ms : 35392

Release           : v2.43.0
Run               : 2025-03-24_15-27-45
Platform          : x86-linux
Language          : c
Suite             : misra-default
Predicate         : _ExternalFlow::elementSpecMatchesSignature/6#c67d3446_Function::Function.getClassAndName/1#27b7404e___#shared
Execution_Time_Ms : 55319

Release           : v2.43.0
Run               : 2025-03-24_15-27-45
Platform          : x86-linux
Language          : c
Suite             : misra-default
Predicate         : OutOfBounds::OOB::libraryFunctionNameParamTableSimpleString/5#6de8614f#cpe#1236
Execution_Time_Ms : 41063

Release           : v2.43.0
Run               : 2025-03-24_15-27-45
Platform          : x86-linux
Language          : c
Suite             : misra-default
Predicate         : OutOfBounds::OOB::libraryFunctionNameParamTable/5#79217c12
Execution_Time_Ms : 37658

Release           : v2.43.0
Run               : 2025-03-24_15-27-45
Platform          : x86-linux
Language          : c
Suite             : misra-default
Predicate         : _Macro::Macro.getName/0#dispred#e28b3699_Preprocessor::PreprocessorBranchDirective#bcd2bde4_Preproce__#antijoin_rhs
Execution_Time_Ms : 62282

Release           : 891
Run               : 2025-04-27_19-23-07
Platform          : x86-linux
Language          : cpp
Suite             : autosar-default
Predicate         : _ExternalFlow::elementSpecMatchesSignature/6#c67d3446_Function::Function.getClassAndName/1#27b7404e___#shared
Execution_Time_Ms : 28740

Release           : 891
Run               : 2025-04-27_19-23-07
Platform          : x86-linux
Language          : c
Suite             : misra-default
Predicate         : _ExternalFlow::elementSpecMatchesSignature/6#c67d3446_Function::Function.getClassAndName/1#27b7404e___#shared
Execution_Time_Ms : 58408

Release           : 891
Run               : 2025-04-27_19-23-07
Platform          : x86-linux
Language          : c
Suite             : misra-default
Predicate         : _Macro::Macro.getName/0#dispred#e28b3699_Preprocessor::PreprocessorBranchDirective#bcd2bde4_Preproce__#antijoin_rhs
Execution_Time_Ms : 56876

Release           : 891
Run               : 2025-04-27_19-23-07
Platform          : x86-linux
Language          : c
Suite             : misra-default
Predicate         : OutOfBounds::OOB::libraryFunctionNameParamTable/5#79217c12
Execution_Time_Ms : 43368

Release           : 891
Run               : 2025-04-27_19-23-07
Platform          : x86-linux
Language          : c
Suite             : misra-default
Predicate         : OutOfBounds::OOB::libraryFunctionNameParamTableSimpleString/5#6de8614f#cpe#1236
Execution_Time_Ms : 38914

Release           : 891
Run               : 2025-04-27_19-23-07
Platform          : x86-linux
Language          : cpp
Suite             : autosar-default
Predicate         : CharacterOutsideTheLanguageStandardBasicSourceCharacterSetUsedInTheSourceCode::getUniversalCharacterName/1#36dbaa42
Execution_Time_Ms : 31128

Release           : 891
Run               : 2025-04-27_19-23-07
Platform          : x86-linux
Language          : cpp
Suite             : autosar-default
Predicate         : QualifiedName::getUserTypeNameWithoutArgs/1#8cfc98e9
Execution_Time_Ms : 30979

Release           : 891
Run               : 2025-04-27_19-23-07
Platform          : x86-linux
Language          : cpp
Suite             : autosar-default
Predicate         : _Declaration::Declaration.getADeclarationEntry/0#dispred#c5d61b67_Declaration::DeclarationEntry.isDe__#antijoin_rhs
Execution_Time_Ms : 29390

Release           : 891
Run               : 2025-04-27_19-23-07
Platform          : x86-linux
Language          : cpp
Suite             : autosar-default
Predicate         : UnusedVariables::maybeACompileTimeTemplateArgument/1#9ea118f0
Execution_Time_Ms : 52057

Release           : 891
Run               : 2025-04-27_19-23-07
Platform          : x86-linux
Language          : c
Suite             : cert-default
Predicate         : Compatible::FunctionDeclarationTypeEquivalence<TypesCompatibleConfig,IncompatibleFunctionDeclarations::interestedInFunctions>::equalReturnTypes/2#70e5a9ff#bb
Execution_Time_Ms : 32503

predicate interestedInFunctions(FunctionDeclarationEntry f1, FunctionDeclarationEntry f2) {
f1.getDeclaration() = f2.getDeclaration() and
not f1 = f2 and
f1.getDeclaration() = f2.getDeclaration()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Redundant condition.

b = bFun.getParameterDeclarationEntry(i).getType()
)
}

predicate equalReturnTypes(FunctionDeclarationEntry f1, FunctionDeclarationEntry f2) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you need to add interestedInFunctions(f1,f2) here - the performance logs suggest this predicate is slow.


string toString() { result = this.(Type).toString() }
/** Whether two types will be compared, regardless of order (a, b) or (b, a). */
private predicate interestedInUnordered(Type t1, Type t2) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Unused predicate.

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.

2 participants