From f0fe96955af9bb0bacea06b93752fe51c72d3024 Mon Sep 17 00:00:00 2001 From: Chris Down Date: Fri, 6 Jan 2017 12:52:34 +0000 Subject: [PATCH] perf: Don't use printf with ordered_selections printf is really, really slow with large arrays of strings. Switching to this results in about a 50% performance improvement. --- clipmenu | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/clipmenu b/clipmenu index 90cbebf..4bdffe6 100755 --- a/clipmenu +++ b/clipmenu @@ -6,13 +6,11 @@ shopt -s nullglob LC_COLLATE=C declare -A selections -ordered_selections=() cache_file=/tmp/clipmenu.$USER/line_cache # We use tac since we want newest to oldest, and we append in clipmenud while IFS='|' read -r full_file first_line; do - ordered_selections+=("$first_line") selections[$first_line]=$full_file done < <(tac "$cache_file") @@ -20,7 +18,7 @@ done < <(tac "$cache_file") # whether `-l` is also in "$@", because the way that dmenu works allows a later # argument to override an earlier one. That is, if the user passes in `-l`, our # one will be ignored. -chosen_line=$(printf '%s\n' "${ordered_selections[@]}" | uniq | dmenu -l 8 "$@") +chosen_line=$(sed 's/^[^|]\+|//' "$cache_file" | tac | uniq | dmenu -l 8 "$@") [[ $chosen_line ]] || exit 1