Skip to content

C++: Improve Element::toString() performance#1366

Merged
jbj merged 3 commits intogithub:masterfrom
zlaski-semmle:zlaski/tostring-performance
Jun 5, 2019
Merged

C++: Improve Element::toString() performance#1366
jbj merged 3 commits intogithub:masterfrom
zlaski-semmle:zlaski/tostring-performance

Conversation

@zlaski-semmle
Copy link
Contributor

by removing recursion in TypeMention::toString().

@zlaski-semmle zlaski-semmle requested a review from a team as a code owner May 24, 2019 15:20
@jbj
Copy link
Contributor

jbj commented May 27, 2019

A bunch of tests are failing because their expected file includes the output of TypeMention.toString(). The test of TypeMention itself remains correct but has become much less informative. Please update the ql file for that test to add a column with the type name.

All the other tests look fine, but you'll need to update their expected file to match the new output. @geoffw0, can you confirm that it's fine to change the dependency-tests output like this?

Now that we've seen how much this change can affect test output, I think the change deserves to be mentioned in the change note. Please add a change note to describe how this change affects qltest files that select every Element (which should be rare).

/** A source code location referring to a type */
class TypeMention extends Locatable, @type_mention {
override string toString() {result = "mention of " + getMentionedType()}
override string toString() {result = "type mention"}
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be sufficient to change this to result = "mention of " + getMentionedType().getName() to avoid the recursion? That would hopefully keep the more informative to-string.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, that would turn this into a PR that we can merge right away, without changes to tests or change note. I prefer result = "type mention" because then we avoid the time and space overhead of creating a new string for each type.

Another option would be result = getMentionedType().getName(), which would make the string informative again. It would be in line with variable accesses, whose toString is just the name of the variable. But as far as I know, nothing benefits from TypeMention.toString being informative -- except for one test that's easily fixed.

@geoffw0
Copy link
Contributor

geoffw0 commented May 28, 2019

@geoffw0, can you confirm that it's fine to change the dependency-tests output like this?

I would imagine they're fine but I failed to find the actual test failures in the logs. I suggest adding the .expected changes (and/or trying out @pavgust's suggestion) and I can review more easily at that point, with the expectation that they're likely to be fine.

Copy link
Contributor

@jbj jbj left a comment

Choose a reason for hiding this comment

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

The tests couldn't run because of conflicts in the change note, so you'll need to rebase. When the tests do run, I expect they'll fail because the expected files in the internal repo also need to be upgraded. You'll need to make a PR to the internal repo as well, making its submodule point to the head of this PR, which should be freshly rebased on ql:master (not on the current target of the submodule pointer). Here's the guide: https://docs.google.com/document/d/1zOra3INgr06hxeyH3AFHn9JmSm8yJNnpH3BVQOBL4-k/edit#heading=h.4dw5b4p0d86g

- The taint tracking library now includes taint-specific edges for functions modeled in `semmle.code.cpp.models.interfaces.DataFlow`.
- The taint tracking library adds flow through library functions that are modeled in `semmle.code.cpp.models.interfaces.Taint`. Queries can add subclasses of `TaintFunction` to specify additional flow.
- There is a new `FoldExpr` class, representing C++17 fold expressions.
- The predicate `TypeMention.toString()` has been simplified to always return the string "`type mention`". This greatly improves performance when using `Expr.toString()` or its descendants.
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo: Expr should be Element.

Also, let's not overstate the performance implications of this change. I like to avoid mentioning performance improvements and regressions in the change notes because even though it varies between releases, performance stays within the same ballpark over time while the quality of our results goes up.

What's important for customers to know is that if they have qltests that query every Element, then those tests will need their expected output updated.

@zlaski-semmle zlaski-semmle force-pushed the zlaski/tostring-performance branch from 4b6ec7b to a952b5b Compare June 3, 2019 17:33
@zlaski-semmle zlaski-semmle added the depends on internal PR This PR should only be merged in sync with an internal Semmle PR label Jun 3, 2019
@zlaski-semmle
Copy link
Contributor Author

Must be committed together with https://git.semmle.com/Semmle/code/pull/32371

@zlaski-semmle zlaski-semmle force-pushed the zlaski/tostring-performance branch from a952b5b to 8c09900 Compare June 4, 2019 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C++ depends on internal PR This PR should only be merged in sync with an internal Semmle PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants