Skip to content

Commit 55f8f3c

Browse files
committed
Perf: replace remaining external commands with bash builtins
- Replace tail-1-in-a-loop for trailing blank line stripping in render_full with pure parameter expansion - Replace sed for bold/italic stripping in render_full sub-headings and setext headings with bash regex loops - Replace sed for backtick syntax highlighting with bash regex loop - Replace awk dedup in build_display_list with bash loop comparison After all three phases, zero external commands remain in hot paths. Stress benchmark: ~1100ms → ~132ms (88% improvement).
1 parent ce4200e commit 55f8f3c

1 file changed

Lines changed: 37 additions & 9 deletions

File tree

hdi

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -424,9 +424,18 @@ build_display_list() {
424424
done <<< "$title_cmds"
425425
fi
426426

427-
# Deduplicate commands
427+
# Deduplicate commands (pure bash, no awk subprocess)
428428
if [[ -n "$cmds" ]]; then
429-
cmds=$(printf '%s' "$cmds" | awk 'NF && !seen[$0]++')
429+
local _deduped="" _dup
430+
while IFS= read -r _cmd; do
431+
[[ -z "$_cmd" ]] && continue
432+
_dup=false
433+
while IFS= read -r _existing; do
434+
[[ "$_existing" == "$_cmd" ]] && _dup=true && break
435+
done <<< "$_deduped"
436+
$_dup || _deduped+="$_cmd"$'\n'
437+
done <<< "$cmds"
438+
cmds="$_deduped"
430439
fi
431440

432441
# Section header
@@ -493,9 +502,15 @@ render_full() {
493502
local title="${SECTION_TITLES[$i]}"
494503
local content="${SECTION_BODIES[$i]}"
495504

496-
# Strip trailing blank lines
497-
while [[ "$content" == *$'\n' ]] && [[ "$(printf '%s' "$content" | tail -1)" =~ ^[[:space:]]*$ ]]; do
498-
content="${content%$'\n'}"
505+
# Strip trailing blank lines (pure bash, no tail subprocess)
506+
while [[ "$content" == *$'\n' ]]; do
507+
local _stripped="${content%$'\n'}"
508+
local _last_line="${_stripped##*$'\n'}"
509+
if [[ "$_last_line" =~ ^[[:space:]]*$ ]]; then
510+
content="$_stripped"
511+
else
512+
break
513+
fi
499514
done
500515
[[ -z "$content" ]] && continue
501516

@@ -520,12 +535,22 @@ render_full() {
520535
if [[ "$sub_heading" =~ ^(.*[^#[:space:]])[[:space:]]*#+ ]]; then
521536
sub_heading="${BASH_REMATCH[1]}"
522537
fi
523-
sub_heading=$(echo "$sub_heading" | sed -E 's/\*{1,2}([^*]+)\*{1,2}/\1/g; s/_{1,2}([^_]+)_{1,2}/\1/g')
538+
# Strip bold/italic (pure bash, no sed)
539+
while [[ "$sub_heading" =~ ^(.*)\*\*([^*]+)\*\*(.*) ]]; do sub_heading="${BASH_REMATCH[1]}${BASH_REMATCH[2]}${BASH_REMATCH[3]}"; done
540+
while [[ "$sub_heading" =~ ^(.*)\*([^*]+)\*(.*) ]]; do sub_heading="${BASH_REMATCH[1]}${BASH_REMATCH[2]}${BASH_REMATCH[3]}"; done
541+
while [[ "$sub_heading" =~ ^(.*)__([^_]+)__(.*) ]]; do sub_heading="${BASH_REMATCH[1]}${BASH_REMATCH[2]}${BASH_REMATCH[3]}"; done
542+
while [[ "$sub_heading" =~ ^(.*)_([^_]+)_(.*) ]]; do sub_heading="${BASH_REMATCH[1]}${BASH_REMATCH[2]}${BASH_REMATCH[3]}"; done
524543
printf " %s%s%s\n" "$MAGENTA$BOLD" "$sub_heading" "$RESET"
525544
return
526545
fi
527-
local formatted
528-
formatted=$(echo "$l" | sed -E "s/\`([^\`]+)\`/${GREEN}\1${RESET}/g")
546+
# Highlight backtick content (pure bash, no sed)
547+
local formatted=""
548+
local _rem="$l"
549+
while [[ "$_rem" =~ ^([^\`]*)\`([^\`]+)\`(.*) ]]; do
550+
formatted+="${BASH_REMATCH[1]}${GREEN}${BASH_REMATCH[2]}${RESET}"
551+
_rem="${BASH_REMATCH[3]}"
552+
done
553+
formatted+="$_rem"
529554
printf " %s\n" "$formatted"
530555
}
531556

@@ -557,7 +582,10 @@ render_full() {
557582
# Setext heading detection: prev_line followed by ===+ or ---+
558583
if $rf_have_prev && [[ "$line" =~ ^[[:space:]]*(={3,}|-{3,})[[:space:]]*$ ]]; then
559584
local sh_text="$rf_prev"
560-
sh_text=$(echo "$sh_text" | sed -E 's/\*{1,2}([^*]+)\*{1,2}/\1/g; s/_{1,2}([^_]+)_{1,2}/\1/g')
585+
while [[ "$sh_text" =~ ^(.*)\*\*([^*]+)\*\*(.*) ]]; do sh_text="${BASH_REMATCH[1]}${BASH_REMATCH[2]}${BASH_REMATCH[3]}"; done
586+
while [[ "$sh_text" =~ ^(.*)\*([^*]+)\*(.*) ]]; do sh_text="${BASH_REMATCH[1]}${BASH_REMATCH[2]}${BASH_REMATCH[3]}"; done
587+
while [[ "$sh_text" =~ ^(.*)__([^_]+)__(.*) ]]; do sh_text="${BASH_REMATCH[1]}${BASH_REMATCH[2]}${BASH_REMATCH[3]}"; done
588+
while [[ "$sh_text" =~ ^(.*)_([^_]+)_(.*) ]]; do sh_text="${BASH_REMATCH[1]}${BASH_REMATCH[2]}${BASH_REMATCH[3]}"; done
561589
printf " %s%s%s\n" "$MAGENTA$BOLD" "$sh_text" "$RESET"
562590
rf_have_prev=false; rf_prev=""
563591
continue

0 commit comments

Comments
 (0)