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

AIM cache filter function once #78480

Merged
merged 1 commit into from
Dec 11, 2024

Conversation

Brambor
Copy link
Contributor

@Brambor Brambor commented Dec 10, 2024

Summary

Performance "AIM cache filter function once"

Purpose of change

Fitler functions can be separated into two phases, preparation and run, example:

preparation

std::unordered_set<bodypart_id> filtered_bodyparts;
std::unordered_set<sub_bodypart_id> filtered_sub_bodyparts;
for( const body_part &bp : all_body_parts ) {
const bodypart_str_id &bp_str_id = convert_bp( bp );
if( lcmatch( body_part_name( bp_str_id, 1 ), filter )
|| lcmatch( body_part_name( bp_str_id, 2 ), filter ) ) {
filtered_bodyparts.insert( bp_str_id->id );
}
for( const sub_bodypart_str_id &sbp : bp_str_id->sub_parts ) {
if( lcmatch( sbp->name.translated(), filter )
|| lcmatch( sbp->name_multiple.translated(), filter ) ) {
filtered_sub_bodyparts.insert( sbp->id );
}
}
}

run

return [filtered_bodyparts, filtered_sub_bodyparts]( const item & i ) {
return std::any_of( filtered_bodyparts.begin(), filtered_bodyparts.end(),
[&i]( const bodypart_id & bp ) {
return i.covers( bp );
} )
|| std::any_of( filtered_sub_bodyparts.begin(), filtered_sub_bodyparts.end(),
[&i]( const sub_bodypart_id & sbp ) {
return i.covers( sbp );
} );

I thought it AIM executes the preparation once, but it does not. So I am making it so.

I want to do filter functions with expansive preparation like here #78384 but for AIM. Without this, the game slows down to a crawl.

Describe the solution

  • I noticed that the current cache is really bad, maybe even non sensical, so I deleted it. It stored filter function for every item name. All the filters are identical.
  • Store the filter function only once.
  • Prepare filter function on filter change, rather than asking every time if we have cache, just have them.
  • Setters and getters for filter and make filter private. To find, fix and forbid future access to setting filter. Now filter is set only from set_filter and always updates the filter function.

Describe alternatives you've considered

Testing

  • I did some performance testing. It has nearly no effect on performance (Took < ~1.2 % when moving all items). But it is about 20 times faster for my test.
    • Slow old_slow
    • Fast new_fast
  • It has huge impact for the upcoming PR. In there, I prepare and catch exceptions.

The test is in this save
TEST AIM move.zip
Move the things through AIM with filters v:h on both panes.

To measure the performance, place breakpoint A here

advanced_inventory_pane &spane = panes[src];

breakpoint B here
const std::string action = is_processing() ? "MOVE_ALL_ITEMS" : ctxt.handle_input();

  1. Load game.
  2. Deactivate B. (Otherwise you will hit it by being in AIM).
  3. Open AIM.
  4. Initialize move all.
  5. Observe: Game stops at breakpoint A.
  6. Activate breakpoint B.
  7. Continue the execution.
  8. Observe: breakpoint B is hit.
  9. Look at CPU profiling obtained between the breakpoints.

Additional context

I went through the View menu (Look around), it seems to do the preparation part only once.

@github-actions github-actions bot added [C++] Changes (can be) made in C++. Previously named `Code` Code: Performance Performance boosting code (CPU, memory, etc.) astyled astyled PR, label is assigned by github actions json-styled JSON lint passed, label assigned by github actions labels Dec 10, 2024
@Night-Pryanik Night-Pryanik merged commit c80d84e into CleverRaven:master Dec 11, 2024
28 of 30 checks passed
@github-actions github-actions bot added the BasicBuildPassed This PR builds correctly, label assigned by github actions label Dec 11, 2024
@Brambor Brambor deleted the aim-upgrade-cache branch December 11, 2024 23:27
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 BasicBuildPassed This PR builds correctly, label assigned by github actions [C++] Changes (can be) made in C++. Previously named `Code` Code: Performance Performance boosting code (CPU, memory, etc.) json-styled JSON lint passed, label assigned by github actions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants