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

CppCheck 2.15 performance cleanup #5931

Merged
merged 4 commits into from
Oct 8, 2024

Conversation

fluffyfreak
Copy link
Contributor

Upgraded CppCheck to 2.15 and ran it over the codebase.

Tackled the most obvious performance warnings plus a few others.
LOTS of std::string being passed by copy instead of const references, along with vectors, maps, and other types which involve lots of copying.
Switched some code to 'emplace_back' instead of 'push_back'.

None of this will make a huge difference to performance but it's no longer just throwing it away.

Upgrade CppCheck to 2.15 and ran it over the codebase. Tackled the most obvious performance warnings plus a few others. LOTS of std::string being passed instead of const references, along with vectors, maps, and other types which involve lots of copying
Copy link
Member

@sturnclaw sturnclaw left a comment

Choose a reason for hiding this comment

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

He's ALIIIIIIIIVE!

...cough. Excuse my exuberance.

Someone has needed to go through and fix the pass-string-by-value Problem for quite some time - thank you very much for tackling this!

For future API design, I'd prefer if we use std::string_view for non-owning string references as it allows aliasing string data from any source (std::string, cstrings, array-of-char, etc.), neatly expresses the non-owner semantics in a much more visible way, and avoids the overhead of constructing a std::string object in cases where we don't actually need an "owned copy" of the string data. (For example, when pulling string parameters from Lua to pass to another function which is going to make an internal copy...)

There's no need to try to retrofit the existing API in this PR however; this is already a very significant improvement.

The opinionated migration to emplace_back is fine - although it tends to produce more incomprehensible error messages when it fails to compile and the overhead from push_back is negligible when operating on primitive types, it otherwise has no downsides so I'm good to sign off on it.

I originally thought I was going to be able to approve this immediately, but I found a few problem spots where the const-ing and copy elision was a bit too overzealous at the cost of API readability. Other than those, I look forward to merging this PR!

src/Input.h Show resolved Hide resolved
src/Intro.cpp Show resolved Hide resolved
src/galaxy/SystemPath.cpp Show resolved Hide resolved
src/graphics/Frustum.cpp Outdated Show resolved Hide resolved
src/graphics/Frustum.h Outdated Show resolved Hide resolved
src/lua/LuaMusic.cpp Outdated Show resolved Hide resolved
src/sound/Sound.cpp Outdated Show resolved Hide resolved
@impaktor
Copy link
Member

impaktor commented Oct 8, 2024

squash / fixup before merge or rebase to master, which hopefully removes the merge commit.

@fluffyfreak
Copy link
Contributor Author

squash / fixup before merge or rebase to master, which hopefully removes the merge commit.

Erm, I would love to, but having just tried 2 different ways all I did was smash up my history and lose my work 👍

@fluffyfreak fluffyfreak requested a review from sturnclaw October 8, 2024 19:47
@sturnclaw sturnclaw merged commit 770ed87 into pioneerspacesim:master Oct 8, 2024
4 checks passed
@impaktor
Copy link
Member

impaktor commented Oct 9, 2024

all I did was smash up my history and lose my work

There's always git reflog, so nothing is lost, until retention limit (3 months?) has passed.

I was going to suggest I could squash & merge this, but I see I'm too late.

Anyway, nice to see you back @fluffyfreak

@fluffyfreak fluffyfreak deleted the CppCleanup branch October 9, 2024 07:47
impaktor pushed a commit to impaktor/pioneer that referenced this pull request Oct 11, 2024
* In summary, stop passing raw std::string!

Upgrade CppCheck to 2.15 and ran it over the codebase. Tackled the most obvious performance warnings plus a few others. LOTS of std::string being passed instead of const references, along with vectors, maps, and other types which involve lots of copying
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants