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

Apply Clang-Tidy and VS static analysis suggestions. #78957

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

CLIDragon
Copy link
Contributor

Summary

Infrastructure "Apply Clang-Tidy and VS static analysis suggestions."

Purpose of change

Improve code readability and performance. On my machine this reduced mean time per do_turn from 135 microseconds to 131.5, around a 2.5% speedup.

Describe the solution

Apply changes. There are a lot of changes but they're grouped by commit and nearly all changes are trivial.

Describe alternatives you've considered

None.

Testing

The game compiles and tests run.

Additional context

@github-actions github-actions bot added NPC / Factions NPCs, AI, Speech, Factions, Ownership Info / User Interface Game - player communication, menus, etc. Translation I18n Missions Quests and missions Bionics CBM (Compact Bionic Modules) Map / Mapgen Overmap, Mapgen, Map extras, Map display Vehicles Vehicles, parts, mechanics & interactions Crafting / Construction / Recipes Includes: Uncrafting / Disassembling Mutations / Traits / Professions/ Hobbies Mutations / Traits / Professions/ Hobbies [C++] Changes (can be) made in C++. Previously named `Code` Monsters Monsters both friendly and unfriendly. Fields / Furniture / Terrain / Traps Objects that are part of the map or its features. Melee Melee weapons, tactics, techniques, reach attack Character / World Generation Issues and enhancements concerning stages of creating a character or a world Player Faction Base / Camp All about the player faction base/camp/site Mechanics: Weather Rain, snow, portal storms and non-temperature environment Items: Containers Things that hold other things Game: Achievements / Conducts / Scores Player goals and how they are tracked. Mechanics: Enchantments / Spells Enchantments and spells Limbs Limbs, mutable limbs, and code related to them. EOC: Effects On Condition Anything concerning Effects On Condition labels Jan 5, 2025
@github-actions github-actions bot requested review from dseguin and KorGgenT January 5, 2025 04:22
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Auto-requesting reviews from non-collaborators: @wapcaplet @Qrox @andrei8l

@github-actions github-actions bot added Code: Infrastructure / Style / Static Analysis Code internal infrastructure and style astyled astyled PR, label is assigned by github actions json-styled JSON lint passed, label assigned by github actions labels Jan 5, 2025
@moxian
Copy link
Contributor

moxian commented Jan 5, 2025

Do you know why are these issues not caught by our current clang-tidy CI setup? Is it a window-specific issue, a newer clang-tidy version that you are using or something else?

@CLIDragon
Copy link
Contributor Author

Do you know why are these issues not caught by our current clang-tidy CI setup? Is it a window-specific issue, a newer clang-tidy version that you are using or something else?

For the commits that don't have clang-tidy in them, they were caught by ReSharper or VS not clang-tidy. For the clang-tidy specific issues, I think it's just that we don't have those checks enabled.

@moxian
Copy link
Contributor

moxian commented Jan 5, 2025

For the clang-tidy specific issues, I think it's just that we don't have those checks enabled.

We should
We have enabled all performance-* checks except performance-avoid-endl, performance-noexcept-swap, performance-no-automatic-move.
Why do we not catch performance-enum-size, performance-unnecessary-copy-initialization, performance-unnecessary-value-param, performance-move-const-arg?

What is your clang-tidy version? How do you obtain the binary?

The performance-noexcept-swap and performance-no-automatic-move that you fix were "temporarily" disabled in #71721, so the fix is good, but in that case they should probably be un-blacklisted from .clang-tidy config.

(separately, the CI is failing since you probably want to add #include <cstdint> for files with new enum sizes)

@andrei8l
Copy link
Contributor

andrei8l commented Jan 5, 2025

Why do we not catch performance-enum-size, performance-unnecessary-copy-initialization, performance-unnecessary-value-param, performance-move-const-arg?

performance-enum-size was added in version 18 and we use 17. The quality of the other checks was improved in version 19.

I don't think it makes much sense to address these unless our CI version detects them or they cause significant issues. performance-enum-size in particular is little more than churn.

@CLIDragon
Copy link
Contributor Author

I don't think it makes much sense to address these unless our CI version detects them or they cause significant issues. performance-enum-size in particular is little more than churn.

performance-unnecessary-copy-initialization, performance-unnecessary-value-param, and performance-move-const-arg all have genuine performance benefits. #78945 is a recent example but there are certainly more. performance-enum-size only causes churn because it has never been addressed in any kind of systematic manner. If it were integrated into the CI it would be a barely noticeable change which may improve performance and will definitely reduce memory usage.

@CLIDragon
Copy link
Contributor Author

CLIDragon commented Jan 6, 2025

clang-tidy is failing because padding on spell_type is 34 instead of 2 bytes. Given that the size of spell_type is 11544 bytes, I don't think 32 bytes is going to make a huge difference so I am leaning towards ignoring it.

@@ -1251,7 +1251,7 @@ tripoint_abs_omt overmapbuffer::find_random(
return find_random( origin, params );
}

shared_ptr_fast<npc> overmapbuffer::find_npc( character_id id )
shared_ptr_fast<npc> overmapbuffer::find_npc( character_id id ) const
{
for( auto &it : overmaps ) {
Copy link
Contributor

@Brambor Brambor Jan 6, 2025

Choose a reason for hiding this comment

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

I assume you will fix these (see build errors). Commenting so that it does not fly under the radar.

@Brambor
Copy link
Contributor

Brambor commented Jan 6, 2025

I like the padding warnings. They taught us that in school but I never knew you could turn on a warning! (And never knew why you couldn't turn it on.)

@moxian Is setting up clang on a local machine manageable these days? can MSVC do it for a single file without compiling the whole project for like 6 hours?

@moxian
Copy link
Contributor

moxian commented Jan 6, 2025

Is setting up clang on a local machine manageable these days? can MSVC do it for a single file without compiling the whole project for like 6 hours?

Stock clang-tidy definitely is, but I haven't managed to build our cata plugin still.

I'm away from my proper PC for the next couple of days so I can't really give a proper answer but the short of it is:

  • grab clang-tidy exe from wherever (perhaps build from source, it's not too bad)
  • create compilation database
  • one way is to install clang power tools VS plugin. Which, actually, can just analyze individual files (which is probably the thing you actually want).. But it can also export compilation database at a click of a button (i didn't like the plugin tho, so I went a bit further down this rabbit hole)
  • the other way is
    • cd build; cmake -G ninja -DDCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake -DTILES=1 -DSOUNDS=1 -DLOCALIZE=1 .. .
    • Then keep feeding it extra vars it asks for (the paths to your clang and clang++ and llvm-rc and ninja (you can replace the -G Ninja with -G "unix makefiles" and use make there instead)
    • then keep doingvcpkg install sdl2 and similar in your vcpkg directory (so C:/vcpkg/) as it keeps complaining
      • there is definitely a better way to do this with vcpkg manifests but it had some subtleties to it and idr the specifics right now and have no way to test
  • now that you have build/compile_commands.json you can feed it to clang tidy . Either move it to the root dir (i.e. mv build/compile_commands.json .) or give it to clang-tidy via -p build flag
  • invoke the thing on a single file: clang-tidy -p build src/achievements.cpp

@CLIDragon
Copy link
Contributor Author

CLIDragon commented Jan 6, 2025

I missed it before, but widget is also failing due to excessive padding and cube_direction needs to include <cstdint>

@akrieger
Copy link
Member

Can you separate out the enum class changes to a separate PR because they're the bulk of this change and probably the least interesting parts (and makes it harder to see what may be functionally contributing to the performance wins)?

@@ -5730,7 +5730,7 @@ void outfit_swap_actor::finish( player_activity &act, Character &who )
// Taken-off items are put in this temporary list, then naturally deleted from the world when the function returns.
std::list<item> it_list;
for( item_location &worn_item : who.get_visible_worn_items() ) {
item outfit_component( *worn_item );
const item &outfit_component( *worn_item );
Copy link
Member

Choose a reason for hiding this comment

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

Is this safe? does taking off the worn item not invalidate the item location and then invalidate the outfit component?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
astyled astyled PR, label is assigned by github actions Bionics CBM (Compact Bionic Modules) [C++] Changes (can be) made in C++. Previously named `Code` Character / World Generation Issues and enhancements concerning stages of creating a character or a world Code: Infrastructure / Style / Static Analysis Code internal infrastructure and style Crafting / Construction / Recipes Includes: Uncrafting / Disassembling EOC: Effects On Condition Anything concerning Effects On Condition Fields / Furniture / Terrain / Traps Objects that are part of the map or its features. Game: Achievements / Conducts / Scores Player goals and how they are tracked. Info / User Interface Game - player communication, menus, etc. Items: Containers Things that hold other things json-styled JSON lint passed, label assigned by github actions Limbs Limbs, mutable limbs, and code related to them. Map / Mapgen Overmap, Mapgen, Map extras, Map display Mechanics: Enchantments / Spells Enchantments and spells Mechanics: Weather Rain, snow, portal storms and non-temperature environment Melee Melee weapons, tactics, techniques, reach attack Missions Quests and missions Monsters Monsters both friendly and unfriendly. Mutations / Traits / Professions/ Hobbies Mutations / Traits / Professions/ Hobbies NPC / Factions NPCs, AI, Speech, Factions, Ownership Player Faction Base / Camp All about the player faction base/camp/site Translation I18n Vehicles Vehicles, parts, mechanics & interactions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants