-
Notifications
You must be signed in to change notification settings - Fork 69
Improve performance of m0-1-3, unused local vars. #974
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
base: main
Are you sure you want to change the base?
Improve performance of m0-1-3, unused local vars. #974
Conversation
There was a problem hiding this 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 improves the performance of rule M0-1-3 (unused local variables) by restructuring how constexpr variables used in template arguments and static asserts are handled. The optimization reduces query execution time on opencv from 56 to 27 seconds and decreases tuple count from ~22 billion to ~500 million by avoiding expensive cross-product joins.
Key Changes:
- Separated constant value collection into a single-pass predicate (
excludedConstantValue) that gathers all values used in template arguments and static asserts globally - Refactored
getUseCountConservativelyto remove the expensive per-variable template instantiation and static assert counting logic - Introduced
excludeVariableByValueandisConservativelyUnusedpredicates to check variable exclusion separately from use counting
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| cpp/autosar/src/rules/M0-1-3/UnusedLocalVariable.ql | Refactored query to collect template argument and static assert values in a single pass, then check constexpr variables against this set rather than counting individual uses |
| change_notes/2025-11-19-improve-performance-m0-1-3-unused-local-variables.md | Documents the performance improvement for the unused local variable query |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
/test-performance |
|
🏁 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. |
|
🏁 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. 🏁 Below are the slowest predicates for the last 2 releases vs this PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| * See `excludedConstantValue` for more details. | ||
| */ | ||
| predicate excludeVariableByValue(Variable variable) { | ||
| variable.isConstexpr() and |
Copilot
AI
Nov 20, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The isConstexpr() check may be too restrictive. The original implementation worked for both const and constexpr variables (since getConstExprValue returns a value for both), but the new implementation only handles constexpr variables. This could cause const variables used in template instantiations to be incorrectly reported as unused.
Consider changing line 41 to:
(variable.isConst() or variable.isConstexpr()) andOr simply remove the check entirely since getConstExprValue already requires the variable to be const or constexpr.
| variable.isConstexpr() and | |
| (variable.isConst() or variable.isConstexpr()) and |
Description
Prior implementation was inefficient for a few reasons
Improved:
This reduces the execution time on opencv from 56 seconds to 27 seconds, but more importantly, it reduced the number of tuples from ~22 bililon to ~500million. This is a cross product that is mostly harmless in some databases but highly problematic in others.
Change request type
.ql,.qll,.qlsor unit tests)Rules with added or modified queries
M0-1-3Release change checklist
A change note (development_handbook.md#change-notes) is required for any pull request which modifies:
If you are only adding new rule queries, a change note is not required.
Author: Is a change note required?
🚨🚨🚨
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.
Reviewer: Confirm that either a change note is not required or the change note is required and has been added.
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
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.
Reviewer
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.