Skip to content

Commit

Permalink
cache item info for crafting GUI's full-text search
Browse files Browse the repository at this point in the history
  • Loading branch information
Brambor committed Nov 16, 2024
1 parent 156cf13 commit 5e0c847
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/recipe_dictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,24 @@ std::vector<const recipe *> recipe_subset::recent() const
return res;
}

// Cache for the search function
std::map<const itype_id, std::string> item_info_cache;

Check failure on line 171 in src/recipe_dictionary.cpp

View workflow job for this annotation

GitHub Actions / build (src)

Global declaration of 'item_info_cache' in a cpp file should be static or have a previous declaration in a header. [cata-static-declarations,-warnings-as-errors]
time_point cache_valid_turn;

Check failure on line 172 in src/recipe_dictionary.cpp

View workflow job for this annotation

GitHub Actions / build (src)

Global declaration of 'cache_valid_turn' in a cpp file should be static or have a previous declaration in a header. [cata-static-declarations,-warnings-as-errors]

std::string cached_item_info( const itype_id &item_type )

Check failure on line 174 in src/recipe_dictionary.cpp

View workflow job for this annotation

GitHub Actions / build (src)

Global declaration of 'cached_item_info' in a cpp file should be static or have a previous declaration in a header. [cata-static-declarations,-warnings-as-errors]
{
if( cache_valid_turn != calendar::turn ) {
cache_valid_turn = calendar::turn;
item_info_cache.clear();
}
if( item_info_cache.count( item_type ) > 0 ) {
return item_info_cache.at( item_type );
}
const item result( item_type );
item_info_cache[item_type] = result.info( true );
return item_info_cache.at( item_type );
}

std::vector<const recipe *> recipe_subset::search(
const std::string_view txt, const search_type key,
const std::function<void( size_t, size_t )> &progress_callback ) const
Expand Down Expand Up @@ -212,8 +230,8 @@ std::vector<const recipe *> recipe_subset::search(
if( r->is_practice() ) {
return lcmatch( r->description.translated(), txt );
} else {
const item result( r->result() );
return lcmatch( remove_color_tags( result.info( true ) ), txt );
// Info is always rendered for avatar anyway, no need to cache per Character then.
return lcmatch( remove_color_tags( cached_item_info( r->result() ) ), txt );
}
}

Expand Down

0 comments on commit 5e0c847

Please sign in to comment.