Skip to content
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

A5-1-9: Improve performance, address duplication #857

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

Conversation

lcartey
Copy link
Collaborator

@lcartey lcartey commented Feb 11, 2025

Description

Addresses three performance issues with A5-1-9:

  • HC_ParamCons was incorrectly defined, referring to uses of the parameters when calculating the hash, instead of properties of the parameter (name and type). This causes exponential blow up in some databases (with lambdas with a lot of parameters and/or lots of uses).
  • An optimiser issue where Variable.getName() was being magic-ked with an insufficiently restrictive set of variables, which ultimately led to the calculation of Function.getParameter(int i) creating a cross-product on indexes.
  • HC_BlockStmt was calculating for a blocks, but hashStmtCons then filtered out any blocks not in lambdas. I've moved this restriction into HC_BlockStmt, which has helped enable us to run even on large databases (e.g. grpc/grpc). Ideally we would apply the lambda restriction to all elements in the LambdaEquivalence.qll, but this seems to be sufficient for now.

Also fixes #856, by excluding results that come from the same macro expansion.

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:
    • A5-1-9

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

The hash cons value for parameters was incorrectly calculated with
parameter uses (e.g accesses to the parameter). The correct
approach is to use the variable name and type.

This caused performance issues, because the hash cons for a
function was made up of all combinations of the accesses to the
parameters. For lambdas with many parameters and many accesses,
this was problematic.
Move the exclusion of non-lambda blocks to the calculation of
HC_BlockStmt, to avoid generating newtype instances for non-lambda
instances.

Choose a reason for hiding this comment

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

PR Overview

This pull request improves the performance of query A5-1-9 by addressing issues with parameter hashing, variable name resolution, and block statement processing that previously led to performance bottlenecks and false positives. Additionally, the change note documents these improvements and notes the exclusion of results from the same macro expansion.

Changes

File Description
change_notes/2025-02-10-improve-perf-a5-1-9.md Change note detailing performance improvements for IdenticalLambdaExpressions.ql and adjustments to reduce false positives

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Tip: Copilot only keeps its highest confidence comments to reduce noise and keep you focused. Learn more

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.

A5-1-9: Avoid reporting duplicate results in macro expansions
1 participant