clipmenu

Simple clipboard management using dmenu
git clone https://git.sinitax.com/cdown/clipmenu
Log | Files | Refs | README | LICENSE | sfeed.txt

commit 8885306970fb0e5ef16620451b9fdd7061db2de6
parent 6edf3c437bb9bafb41e0e7383ff4def6a794cd13
Author: Chris Down <chris@chrisdown.name>
Date:   Fri,  6 Jan 2017 14:53:59 +0000

perf: Don't use date, use CRC instead

We don't need to sort by date now that we record in $cache_file, which
does it naturally for us.

Diffstat:
Mclipmenu | 27++++++---------------------
Mclipmenud | 8++++----
Mtest/test-perf | 8++++----
3 files changed, 14 insertions(+), 29 deletions(-)

diff --git a/clipmenu b/clipmenu @@ -2,35 +2,20 @@ shopt -s nullglob -# We use this to make sure the cache files are sorted bytewise -export LC_COLLATE=C - -cache_file=/tmp/clipmenu.$USER/line_cache +cache_dir=/tmp/clipmenu.$USER +cache_file=$cache_dir/line_cache # It's okay to hardcode `-l 8` here as a sensible default without checking # 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=$(sed 's/^[^|]\+|//' "$cache_file" | tac | uniq | dmenu -l 8 "$@") +chosen_line=$(tac "$cache_file" | uniq | dmenu -l 8 "$@") [[ $chosen_line ]] || exit 1 -# Naive, but performant path, only follow expensive path if it doesn't work -out_line=$(grep -F "|$chosen_line" "$cache_file") -out_length=$(wc -l <<< "$out_line") - -if (( out_length == 1 )); then - # Cheap path succeded - file=${out_line%%|*} -elif (( out_length > 1 )); then - # Cheap path failed - while IFS='|' read -r full_file first_line; do - if [[ $first_line == "$chosen_line" ]]; then - file=$full_file - break - fi - done <<< "$out_line" -else +file=$cache_dir/$(cksum <<< "$chosen_line") + +if ! [[ -f "$file" ]]; then # We didn't find this in cache printf 'FATAL: %s not in cache, run clipmenu-fsck\n' "$chosen_line" >&2 exit 2 diff --git a/clipmenud b/clipmenud @@ -120,17 +120,17 @@ while sleep "${CLIPMENUD_SLEEP:-0.5}"; do rm -- "${last_filename[$selection]}" fi - filename="$cache_dir/$(LC_ALL=C date +%F-%T.%N)" last_data[$selection]=$data last_filename[$selection]=$filename + first_line=$(get_first_line "$data") + filename="$cache_dir/$(cksum <<< "$first_line")" debug "Writing $data to $filename" printf '%s' "$data" > "$filename" - debug "Writing new entry to $cache_file" - printf '%s|%s\n' \ - "$filename" "$(get_first_line "$data")" >> "$cache_file" + debug "Writing $first_line to $cache_file" + printf '%s\n' "$first_line" >> "$cache_file" if ! (( NO_OWN_CLIPBOARD )) && [[ $selection != primary ]]; then # Take ownership of the clipboard, in case the original application diff --git a/test/test-perf b/test/test-perf @@ -52,16 +52,16 @@ if ! (( NO_RECREATE )); then line_len=$(( (RANDOM % 10000) + 1 )) num_lines=$(( (RANDOM % 10) + 1 )) - fn=$dir/$(LC_ALL=C date +%F-%T.%N) data=$( tr -dc 'a-zA-Z0-9' < /dev/urandom | fold -w "$line_len" | head -"$num_lines" ) + read -r first_line_raw <<< "$data" + printf -v first_line '%s (%s lines)\n' "$first_line_raw" "$num_lines" + printf '%s' "$first_line" >> "$cache_file" + fn=$dir/$(cksum <<< "$first_line") printf '%s' "$data" > "$fn" - read -r first_line <<< "$data" - printf '%s|%s (%s lines)\n' \ - "$fn" "$first_line" "$num_lines" >> "$cache_file" done printf 'done\n'