Skip to content

avoid duplicated evaluation in ProgramMemoryState::removeModifiedVars()#6628

Merged
firewave merged 1 commit intocppcheck-opensource:mainfrom
firewave:pm-cond
Jul 26, 2024
Merged

avoid duplicated evaluation in ProgramMemoryState::removeModifiedVars()#6628
firewave merged 1 commit intocppcheck-opensource:mainfrom
firewave:pm-cond

Conversation

@firewave
Copy link
Copy Markdown
Collaborator

No description provided.

@firewave
Copy link
Copy Markdown
Collaborator Author

firewave commented Jul 21, 2024

-D __GNUC__ --check-level=exhaustive lib/utils.cpp

Clang 18 999,900,672 -> 874,701,202
GCC 14 1,026,753,345 -> 892,531,811

@pfultz2 please have a look

@firewave firewave marked this pull request as draft July 21, 2024 18:58
@firewave firewave marked this pull request as ready for review July 21, 2024 19:12
@firewave firewave changed the title programmemory.cpp: avoid duplicated evaluation in ProgramMemoryState::removeModifiedVars() avoid duplicated evaluation in ProgramMemoryState::removeModifiedVars() Jul 21, 2024
@firewave
Copy link
Copy Markdown
Collaborator Author

firewave commented Jul 21, 2024

This should help with https://trac.cppcheck.net/ticket/12271.

@chrchr-github
Copy link
Copy Markdown
Collaborator

Looks like this (anti-) pattern also exists in fillProgramMemoryFromAssignments() and valueflow.cpp::evaluateInt()

@firewave
Copy link
Copy Markdown
Collaborator Author

Looks like this (anti-) pattern also exists in fillProgramMemoryFromAssignments() and valueflow.cpp::evaluateInt()

Yes, I did not touch those because they are not in the hot path. Well one of them shows up in the profiler but changing it let to strange results with one of the compilers so I left it for now.

Comment thread lib/programmemory.cpp
{
if (!condition)
return false;
MathLib::bigint result = 0;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

To be on the safe side, we should initialize result before the first return.

Comment thread lib/programmemory.cpp Outdated
return {1};
if (result == 0)
return {0};
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think it would be better to just call execute and use the isTrue and isFalse functions:

auto result = execute(cond, pm, *settings);
if(isTrue(result))
    return {1};
if(isFalse(result))
    return {0};
return {};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Then there is no need to refactor the evaluateCondition function.

Copy link
Copy Markdown
Contributor

@pfultz2 pfultz2 Jul 25, 2024

Choose a reason for hiding this comment

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

And this same code could be used in evaluateInt as well, we just need to expose the isTrue and isFalse functions.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done. As mentioned before I will not touch other code until I encounter it in a profile.

…::removeModifiedVars()`

Co-authored-by: Paul Fultz II <pfultz2@yahoo.com>
@firewave firewave merged commit 8368943 into cppcheck-opensource:main Jul 26, 2024
@firewave firewave deleted the pm-cond branch July 26, 2024 04:05
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