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

Cache item info for crafting GUI full-text search #77914

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
static std::map<const itype_id, std::string> item_info_cache;
static time_point cache_valid_turn;

static std::string cached_item_info( const itype_id &item_type )
{
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
Loading