Skip to content

Fix poor performance of sameSource predicate #907

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 2 commits into from
Jun 6, 2025

Conversation

smowton
Copy link
Contributor

@smowton smowton commented Jun 4, 2025

Description

User at github/codeql#19633 observed that sameSource could be a potentially-large cost in running the codeql/[email protected] suite.

I noticed that while the inline pragma made clear that the intent was never to materialise sameSource in its entirety but rather only to identify if two file-accesses referred to the same source once the two candidates had been otherwise constrained, in practice a common-subexpression pass was lifting the general square of all possibly-aliasing file accesses into a #shared predicate.

Switching to inline_late instead defeats this potential misoptimisation, and at least for the linked discussion makes the suite once again run on a practical timescale.

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:
    • FIO39-C
    • FIO50-CPP
    • A27-0-3
    • RULE-30-0-2

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!)

@Copilot Copilot AI review requested due to automatic review settings June 4, 2025 11:26
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 addresses performance issues in the sameSource predicate by preventing premature inlining and constraining its bindings.

  • Switches from inline to inline_late to avoid heavy common-subexpression expansion
  • Adds a bindingset[a, b] directive to limit predicate instantiations
Comments suppressed due to low confidence (1)

cpp/common/src/codingstandards/cpp/rules/iofstreammissingpositioning/IOFstreamMissingPositioning.qll:54

  • [nitpick] Consider adding a comment above this pragma to explain that inline_late is used to prevent early inlining and avoid expensive common-subexpression optimizations that degrade performance.
pragma[inline_late]

@smowton smowton force-pushed the smowton/fix/same-source-perf branch from 1d2a7f2 to 985a2d1 Compare June 4, 2025 11:28
@lcartey
Copy link
Collaborator

lcartey commented Jun 4, 2025

/test-performance

Copy link

github-actions bot commented Jun 4, 2025

🏁 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.44.0
Platform                           : x86-linux
Language                           : c
Total_Serialized_Execution_Time_Ms : 2569320
Mean_Predicate_Execution_Time_Ms   : 47.18764348289225
Median_Predicate_Execution_Time_Ms : 0.0
Standard_Deviation_Ms              : 623.3848493198769
Total_Serialized_Execution_Time_s  : 2569.32
Mean_Query_Execution_Time_s        : 0.0471876434828922
Median_Predicate_Execution_Time_s  : 0.0
Percentile95_Ms                    : 115.0
Number_of_Predicates               : 54449

Release                            : v2.44.0
Platform                           : x86-linux
Language                           : cpp
Total_Serialized_Execution_Time_Ms : 2890004
Mean_Predicate_Execution_Time_Ms   : 57.72042581237892
Median_Predicate_Execution_Time_Ms : 0.0
Standard_Deviation_Ms              : 583.4436271796267
Total_Serialized_Execution_Time_s  : 2890.004
Mean_Query_Execution_Time_s        : 0.0577204258123789
Median_Predicate_Execution_Time_s  : 0.0
Percentile95_Ms                    : 148.0
Number_of_Predicates               : 50069

Release                            : v2.44.0
Platform                           : x86-windows
Language                           : c
Total_Serialized_Execution_Time_Ms : 2843064
Mean_Predicate_Execution_Time_Ms   : 52.82443655822077
Median_Predicate_Execution_Time_Ms : 0.0
Standard_Deviation_Ms              : 555.7550278554764
Total_Serialized_Execution_Time_s  : 2843.064
Mean_Query_Execution_Time_s        : 0.0528244365582207
Median_Predicate_Execution_Time_s  : 0.0
Percentile95_Ms                    : 157.0
Number_of_Predicates               : 53821

Release                            : v2.44.0
Platform                           : x86-windows
Language                           : cpp
Total_Serialized_Execution_Time_Ms : 3041503
Mean_Predicate_Execution_Time_Ms   : 60.87876301040833
Median_Predicate_Execution_Time_Ms : 1.0
Standard_Deviation_Ms              : 552.5159941493636
Total_Serialized_Execution_Time_s  : 3041.503
Mean_Query_Execution_Time_s        : 0.0608787630104083
Median_Predicate_Execution_Time_s  : 0.001
Percentile95_Ms                    : 182.04999999999563
Number_of_Predicates               : 49960

Release                            : v2.45.0
Platform                           : x86-linux
Language                           : c
Total_Serialized_Execution_Time_Ms : 2581075
Mean_Predicate_Execution_Time_Ms   : 47.971805070255
Median_Predicate_Execution_Time_Ms : 0.0
Standard_Deviation_Ms              : 620.328032025375
Total_Serialized_Execution_Time_s  : 2581.075
Mean_Query_Execution_Time_s        : 0.047971805070255
Median_Predicate_Execution_Time_s  : 0.0
Percentile95_Ms                    : 117.0
Number_of_Predicates               : 53804

Release                            : v2.45.0
Platform                           : x86-linux
Language                           : cpp
Total_Serialized_Execution_Time_Ms : 2908122
Mean_Predicate_Execution_Time_Ms   : 58.0788065186132
Median_Predicate_Execution_Time_Ms : 0.0
Standard_Deviation_Ms              : 584.9817986910139
Total_Serialized_Execution_Time_s  : 2908.122
Mean_Query_Execution_Time_s        : 0.0580788065186132
Median_Predicate_Execution_Time_s  : 0.0
Percentile95_Ms                    : 152.0
Number_of_Predicates               : 50072

Release                            : v2.45.0
Platform                           : x86-windows
Language                           : c
Total_Serialized_Execution_Time_Ms : 2872385
Mean_Predicate_Execution_Time_Ms   : 53.41785687718515
Median_Predicate_Execution_Time_Ms : 0.0
Standard_Deviation_Ms              : 560.427241699905
Total_Serialized_Execution_Time_s  : 2872.385
Mean_Query_Execution_Time_s        : 0.0534178568771851
Median_Predicate_Execution_Time_s  : 0.0
Percentile95_Ms                    : 148.0
Number_of_Predicates               : 53772

Release                            : v2.45.0
Platform                           : x86-windows
Language                           : cpp
Total_Serialized_Execution_Time_Ms : 3013661
Mean_Predicate_Execution_Time_Ms   : 59.78062762834246
Median_Predicate_Execution_Time_Ms : 1.0
Standard_Deviation_Ms              : 532.4892044266705
Total_Serialized_Execution_Time_s  : 3013.661
Mean_Query_Execution_Time_s        : 0.0597806276283424
Median_Predicate_Execution_Time_s  : 0.001
Percentile95_Ms                    : 176.0
Number_of_Predicates               : 50412

Release                            : 907
Platform                           : x86-linux
Language                           : cpp
Total_Serialized_Execution_Time_Ms : 2901541
Mean_Predicate_Execution_Time_Ms   : 57.87340434019467
Median_Predicate_Execution_Time_Ms : 0.0
Standard_Deviation_Ms              : 583.4013893033987
Total_Serialized_Execution_Time_s  : 2901.541
Mean_Query_Execution_Time_s        : 0.0578734043401946
Median_Predicate_Execution_Time_s  : 0.0
Percentile95_Ms                    : 148.0
Number_of_Predicates               : 50136

Release                            : 907
Platform                           : x86-linux
Language                           : c
Total_Serialized_Execution_Time_Ms : 2583105
Mean_Predicate_Execution_Time_Ms   : 48.17158681908882
Median_Predicate_Execution_Time_Ms : 0.0
Standard_Deviation_Ms              : 664.5607043823868
Total_Serialized_Execution_Time_s  : 2583.105
Mean_Query_Execution_Time_s        : 0.0481715868190888
Median_Predicate_Execution_Time_s  : 0.0
Percentile95_Ms                    : 112.0
Number_of_Predicates               : 53623

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


Release           : v2.45.0
Run               : 2025-05-21_14-04-43
Platform          : x86-linux
Language          : cpp
Suite             : autosar-default
Predicate         : UnusedVariables::maybeACompileTimeTemplateArgument/1#9ea118f0
Execution_Time_Ms : 55520

Release           : v2.45.0
Run               : 2025-05-21_14-04-43
Platform          : x86-linux
Language          : c
Suite             : misra-default
Predicate         : OutOfBounds::OOB::libraryFunctionNameParamTable/5#79217c12
Execution_Time_Ms : 44262

Release           : v2.45.0
Run               : 2025-05-21_14-04-43
Platform          : x86-linux
Language          : c
Suite             : misra-default
Predicate         : OutOfBounds::OOB::libraryFunctionNameParamTableSimpleString/5#6de8614f#cpe#1236
Execution_Time_Ms : 38541

Release           : v2.45.0
Run               : 2025-05-21_14-04-43
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 : 27633

Release           : v2.45.0
Run               : 2025-05-21_14-04-43
Platform          : x86-linux
Language          : c
Suite             : misra-default
Predicate         : _Macro::Macro.getName/0#dispred#e28b3699_Preprocessor::PreprocessorBranchDirective#bcd2bde4_Preproce__#antijoin_rhs
Execution_Time_Ms : 55790

Release           : v2.45.0
Run               : 2025-05-21_14-04-43
Platform          : x86-windows
Language          : c
Suite             : misra-default
Predicate         : _Macro::Macro.getName/0#dispred#e28b3699_Preprocessor::PreprocessorBranchDirective#bcd2bde4_Preproce__#antijoin_rhs
Execution_Time_Ms : 49143

Release           : v2.45.0
Run               : 2025-05-21_14-04-43
Platform          : x86-windows
Language          : c
Suite             : misra-default
Predicate         : OutOfBounds::OOB::libraryFunctionNameParamTableSimpleString/5#6de8614f#cpe#1236
Execution_Time_Ms : 35851

Release           : v2.45.0
Run               : 2025-05-21_14-04-43
Platform          : x86-windows
Language          : c
Suite             : misra-default
Predicate         : OutOfBounds::OOB::libraryFunctionNameParamTable/5#79217c12
Execution_Time_Ms : 39037

Release           : v2.45.0
Run               : 2025-05-21_14-04-43
Platform          : x86-windows
Language          : cpp
Suite             : autosar-default
Predicate         : _Declaration::Declaration.getADeclarationEntry/0#dispred#c5d61b67_Declaration::DeclarationEntry.isDe__#antijoin_rhs
Execution_Time_Ms : 23932

Release           : v2.45.0
Run               : 2025-05-21_14-04-43
Platform          : x86-windows
Language          : cpp
Suite             : autosar-default
Predicate         : UnusedVariables::maybeACompileTimeTemplateArgument/1#9ea118f0
Execution_Time_Ms : 66556

Release           : v2.45.0
Run               : 2025-05-21_14-04-43
Platform          : x86-linux
Language          : c
Suite             : misra-default
Predicate         : _ExternalFlow::elementSpecMatchesSignature/6#c67d3446_Function::Function.getClassAndName/1#27b7404e___#shared
Execution_Time_Ms : 64202

Release           : v2.45.0
Run               : 2025-05-21_14-04-43
Platform          : x86-windows
Language          : c
Suite             : misra-default
Predicate         : _Class::Class.getALinkTarget/0#dispred#29b2b38a#bf_Class::Class.getALinkTarget/0#dispred#29b2b38a#bf__#shared
Execution_Time_Ms : 24718

Release           : v2.45.0
Run               : 2025-05-21_14-04-43
Platform          : x86-windows
Language          : c
Suite             : misra-default
Predicate         : _ExternalFlow::elementSpecMatchesSignature/6#c67d3446_Function::Function.getClassAndName/1#27b7404e___#shared
Execution_Time_Ms : 58913

Release           : v2.45.0
Run               : 2025-05-21_14-04-43
Platform          : x86-windows
Language          : cpp
Suite             : autosar-default
Predicate         : _ExternalFlow::elementSpecMatchesSignature/6#c67d3446_Function::Function.getClassAndName/1#27b7404e___#shared
Execution_Time_Ms : 29274

Release           : v2.45.0
Run               : 2025-05-21_14-04-43
Platform          : x86-windows
Language          : cpp
Suite             : autosar-default
Predicate         : #select
Execution_Time_Ms : 17217

Release           : v2.45.0
Run               : 2025-05-21_14-04-43
Platform          : x86-windows
Language          : cpp
Suite             : autosar-default
Predicate         : FunctionEquivalence::typeSig/1#194ac728
Execution_Time_Ms : 18465

Release           : v2.45.0
Run               : 2025-05-21_14-04-43
Platform          : x86-linux
Language          : cpp
Suite             : autosar-default
Predicate         : CharacterOutsideTheLanguageStandardBasicSourceCharacterSetUsedInTheSourceCode::getUniversalCharacterName/1#36dbaa42
Execution_Time_Ms : 33253

Release           : v2.45.0
Run               : 2025-05-21_14-04-43
Platform          : x86-linux
Language          : cpp
Suite             : autosar-default
Predicate         : _ExternalFlow::elementSpecMatchesSignature/6#c67d3446_Function::Function.getClassAndName/1#27b7404e___#shared
Execution_Time_Ms : 27541

Release           : v2.45.0
Run               : 2025-05-21_14-04-43
Platform          : x86-linux
Language          : cpp
Suite             : autosar-default
Predicate         : QualifiedName::getUserTypeNameWithoutArgs/1#8cfc98e9
Execution_Time_Ms : 32664

Release           : v2.45.0
Run               : 2025-05-21_14-04-43
Platform          : x86-linux
Language          : cpp
Suite             : autosar-default
Predicate         : _Declaration::Declaration.getADeclarationEntry/0#dispred#c5d61b67_Declaration::DeclarationEntry.isDe__#antijoin_rhs
Execution_Time_Ms : 31231

Release           : 907
Run               : 2025-06-04_11-36-33
Platform          : x86-linux
Language          : cpp
Suite             : autosar-default
Predicate         : _Declaration::Declaration.getADeclarationEntry/0#dispred#c5d61b67_Declaration::DeclarationEntry.isDe__#antijoin_rhs
Execution_Time_Ms : 32487

Release           : 907
Run               : 2025-06-04_11-36-33
Platform          : x86-linux
Language          : cpp
Suite             : autosar-default
Predicate         : CheckedException::CheckedException#b0aa5ec8
Execution_Time_Ms : 28314

Release           : 907
Run               : 2025-06-04_11-36-33
Platform          : x86-linux
Language          : cpp
Suite             : autosar-default
Predicate         : QualifiedName::getUserTypeNameWithoutArgs/1#8cfc98e9
Execution_Time_Ms : 30298

Release           : 907
Run               : 2025-06-04_11-36-33
Platform          : x86-linux
Language          : cpp
Suite             : autosar-default
Predicate         : CharacterOutsideTheLanguageStandardBasicSourceCharacterSetUsedInTheSourceCode::getUniversalCharacterName/1#36dbaa42
Execution_Time_Ms : 29719

Release           : 907
Run               : 2025-06-04_11-36-33
Platform          : x86-linux
Language          : cpp
Suite             : autosar-default
Predicate         : UnusedVariables::maybeACompileTimeTemplateArgument/1#9ea118f0
Execution_Time_Ms : 54479

Release           : 907
Run               : 2025-06-04_11-36-33
Platform          : x86-linux
Language          : c
Suite             : misra-default
Predicate         : _Macro::Macro.getName/0#dispred#e28b3699_Preprocessor::PreprocessorBranchDirective#bcd2bde4_Preproce__#antijoin_rhs
Execution_Time_Ms : 60969

Release           : 907
Run               : 2025-06-04_11-36-33
Platform          : x86-linux
Language          : c
Suite             : misra-default
Predicate         : _ExternalFlow::elementSpecMatchesSignature/6#c67d3446_Function::Function.getClassAndName/1#27b7404e___#shared
Execution_Time_Ms : 59181

Release           : 907
Run               : 2025-06-04_11-36-33
Platform          : x86-linux
Language          : c
Suite             : misra-default
Predicate         : OutOfBounds::OOB::libraryFunctionNameParamTable/5#79217c12
Execution_Time_Ms : 55842

Release           : 907
Run               : 2025-06-04_11-36-33
Platform          : x86-linux
Language          : c
Suite             : misra-default
Predicate         : OutOfBounds::OOB::libraryFunctionNameParamTableSimpleString/5#6de8614f#cpe#1236
Execution_Time_Ms : 38484

Release           : 907
Run               : 2025-06-04_11-36-33
Platform          : x86-linux
Language          : c
Suite             : misra-default
Predicate         : _Class::Class.getALinkTarget/0#dispred#29b2b38a#bf_Class::Class.getALinkTarget/0#dispred#29b2b38a#bf__#shared
Execution_Time_Ms : 28937

@MichaelRFairhurst
Copy link
Contributor

Performance results appear reasonable to me.

@lcartey lcartey enabled auto-merge June 6, 2025 09:52
@lcartey lcartey added this pull request to the merge queue Jun 6, 2025
Merged via the queue into github:main with commit 42985d7 Jun 6, 2025
21 checks passed
@MichaelRFairhurst
Copy link
Contributor

Thank you @smowton ! 🎉 🎉 🎉

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.

3 participants