From 68d7411edff1a110b96fca853d6b22f1a1dce1c0 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Sat, 18 May 2019 03:28:54 +0200 Subject: [PATCH] (menu entries) Add sublabel_cache - this should prevent unnecessary function calls if the contents of a sublabel don't change over time --- menu/cbs/menu_cbs_sublabel.c | 6 +++--- menu/menu_entries.c | 26 +++++++++++++++++++------- menu/menu_entries.h | 2 ++ 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/menu/cbs/menu_cbs_sublabel.c b/menu/cbs/menu_cbs_sublabel.c index dc025c8d2d..02a137e9d3 100644 --- a/menu/cbs/menu_cbs_sublabel.c +++ b/menu/cbs/menu_cbs_sublabel.c @@ -55,7 +55,7 @@ static int (func_name)(file_list_t *list, unsigned type, unsigned i, const char *label, const char *path, char *s, size_t len) \ { \ strlcpy(s, msg_hash_to_str(lbl), len); \ - return 0; \ + return 1; \ } default_sublabel_macro(menu_action_sublabel_setting_audio_mixer_add_to_mixer_and_play, @@ -708,7 +708,7 @@ static int action_bind_sublabel_audio_mixer_stream( audio_mixer_stream_t *stream = audio_driver_mixer_get_stream(offset); if (!stream) - return 0; + return -1; switch (stream->state) { @@ -796,7 +796,7 @@ static int action_bind_sublabel_netplay_room( unsigned offset = i - 3; if (i < 1 || offset > (unsigned)netplay_room_count) - return 0; + return -1; ra_version = netplay_room_list[offset].retroarch_version; corename = netplay_room_list[offset].corename; diff --git a/menu/menu_entries.c b/menu/menu_entries.c index 2ddc72385e..c070ab664a 100644 --- a/menu/menu_entries.c +++ b/menu/menu_entries.c @@ -433,14 +433,26 @@ void menu_entry_get(menu_entry_t *entry, size_t stack_idx, char tmp[512]; tmp[0] = '\0'; - cbs->action_sublabel(list, - entry->type, (unsigned)i, - label, path, - tmp, - sizeof(tmp)); + if (!string_is_empty(cbs->action_sublabel_cache)) + entry->sublabel = strdup(cbs->action_sublabel_cache); + else + { + if (cbs->action_sublabel(list, + entry->type, (unsigned)i, + label, path, + tmp, + sizeof(tmp))) + { + /* if this function callback returns true, + * we know that the value won't change - so we + * can cache it instead. */ + strlcpy(cbs->action_sublabel_cache, + tmp, sizeof(cbs->action_sublabel_cache)); + } - if (!string_is_empty(tmp)) - entry->sublabel = strdup(tmp); + if (!string_is_empty(tmp)) + entry->sublabel = strdup(tmp); + } } } diff --git a/menu/menu_entries.h b/menu/menu_entries.h index ca94e50e95..6baddb93d3 100644 --- a/menu/menu_entries.h +++ b/menu/menu_entries.h @@ -80,6 +80,8 @@ typedef struct menu_ctx_list typedef struct menu_file_list_cbs { + char action_sublabel_cache[512]; + enum msg_hash_enums enum_idx; const char *action_iterate_ident; const char *action_deferred_push_ident;