Skip to content

Commit

Permalink
speed up __sd_directory_help
Browse files Browse the repository at this point in the history
Unscientifically, running `sd` with no arguments for my ~/sd went from
140ms to 80ms.

About 40ms saved by using a glob instead of a find (I don't know what I was
thinking there), and another 20ms by pushing the output to a separate loop. I
assume it changed the way it could buffer the output, but I don't really
understand why that is.
  • Loading branch information
ianthehenry committed Dec 1, 2021
1 parent 9c69654 commit f31d8c0
Showing 1 changed file with 29 additions and 14 deletions.
43 changes: 29 additions & 14 deletions sd
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ __sd_edit() {

__sd_directory_help() {
local target="$1"
local file helpfile help command no_commands
local i file helpfile help command
local -a commands helps
if [[ -e "$target.help" ]]; then
__sd_cat "$target.help"
echo
Expand All @@ -27,31 +28,39 @@ __sd_directory_help() {
echo "$command commands"
echo
fi
no_commands=true
# this loop is incredibly slow. on zsh we can do a glob instead of a find, and
# it's noticeably faster.
for file in $(find "$target" -maxdepth 1 -mindepth 1 -not -name '.*' -exec test -x {} \; -print); do
commands=()
helps=()
for file in "$target"/*; do
if [[ ! -x "$file" ]]; then
continue
fi
command=$(basename "$file")
helpfile="$file.help"
if [[ -f "$helpfile" ]]; then
help=$(head -n1 "$helpfile")
elif [[ -d "$file" ]]; then
help="$command commands"
command="$command ..."
else
help=$(sed -nE -e '/^#!/d' -e '/^#/{s/^# *//; p; q;}' "$file")
fi
# this should really be a two-pass thing to calculate
# the longest filename instead of hardcoding the spacing...
# but oh well whatever
if [[ -d "$file" ]]; then
command="$command ..."
fi
printf '%-10s -- %s\n' "$command" "$help"
no_commands=false
commands+=("$command")
helps+=("$help")
done

if [[ "$no_commands" = true ]]; then
if [[ "${#commands[@]}" -eq 0 ]]; then
echo "(no subcommands found)"
else
local max_length=0
local length
for command in "${commands[@]}"; do
length="${#command}"
max_length=$(( length > max_length ? length : max_length))
done

for ((i = 0; i < ${#commands[@]}; i++)); do
printf "%-${max_length}s -- %s\n" "${commands[i]}" "${helps[i]}"
done
fi
}

Expand Down Expand Up @@ -127,6 +136,12 @@ EOF
sd() (
set -euo pipefail

if [[ -n ${ZSH_EVAL_CONTEXT+x} ]]; then
# we need 0-indexed arrays so that the for loop
# in __sd_directory_help works properly
set -o KSH_ARRAYS
fi

local target=${SD_ROOT:-$HOME/sd}
local arg

Expand Down

0 comments on commit f31d8c0

Please sign in to comment.