From e6d8d38e05dd2c29af49c9139ad49c1fef082312 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Wagner?= Date: Sun, 6 Aug 2023 19:12:38 +0200 Subject: [PATCH 1/3] add basic stopwatch functionality --- astro | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/astro b/astro index c9176e2..44aff36 100755 --- a/astro +++ b/astro @@ -120,10 +120,23 @@ mkdir -p "$cachedir" pagefile="$(mktemp -p "$cachedir" -t curpage.XXXXXX)" histfile="$(mktemp -p "$cachedir" -t history.XXXXXX)" linksfile="$(mktemp -p "$cachedir" -t links.XXXXXX)" +tracefile="$(mktemp -p "$cachedir" -t trace.XXXXXX)" # Restore terminal trap 'tput rmcup && rm -f $histfile $linksfile > /dev/null 2>&1; exit' EXIT INT HUP +stop() { + [ "$trace" ] || return + if [ -z "$stopwatch" ] + then + stopwatch=$(date +%s.%N) + else + dur=$(echo "$(date +%s.%N) - $stopwatch" | bc) + printf "%s took %s seconds\n" "$1" "$dur" >> "$tracefile" + unset stopwatch + fi +} + getprevious() { sed -i '$d' "$histfile" prev="$(tail -n 1 "$histfile")" @@ -163,9 +176,12 @@ parseurl() { } typesetgmi() { + stop + NR=$'\r' while IFS='' read -r line do line="$(echo "$line" | tr -d '\r')" + #line="${line%$NR}" # shellcheck disable=SC2016 echo "$line" | grep -q '^```' && pre=$((1 - pre)) && line="" @@ -181,6 +197,10 @@ typesetgmi() { "## "*) sty="$sty_header2" && line="$(echo "$line" | cut -c 4-)" ;; "# "*) sty="$sty_header1" && line="$(echo "$line" | cut -c 3-)" ;; "> "*) sty="$sty_quote" && line="$(echo "$line" | cut -c 3-)" ;; + #"### "*) sty="$sty_header3" && line="${line#????}" ;; + #"## "*) sty="$sty_header2" && line="${line#???}" ;; + #"# "*) sty="$sty_header1" && line="${line#??}" ;; + #"> "*) sty="$sty_quote" && line="${line#??}" ;; "=>"*) link="$(echo "$line" | sed -e 's/^=> *\(\S\+\)\(\s*.*\)/\1 \2/g')" echo "$link" >> "$linksfile" @@ -191,8 +211,7 @@ typesetgmi() { [ -z "$line" ] && line="$link" sty="$sty_linkt" - line="$(echo "$sty_linkb$sty_linkt" | sed "s/%linkcount/$linkcount/g")$line" - #debug "link line: $line" + line="$(echo "$sty_linkb$sty_linkt" | sed "s/%linkcount/$linkcount/")$line" ;; '* '*) sty="$sty_listt" && line="$sty_listb$sty_listt$(echo "$line" | cut -c 2-)";; *) sty="";; @@ -206,10 +225,12 @@ typesetgmi() { done } done + stop "typeset" } # borrowed from https://gist.github.com/cdown/1163649 urlencode() { + stop old_lang=$LANG LANG=C @@ -230,6 +251,7 @@ urlencode() { LC_COLLATE=$old_lc_collate LANG=$old_lang + stop "urlencode" } # Fetches the gemini response from server @@ -268,17 +290,23 @@ EOF debug "using client cert for domain: $certfile" fi + [ "$trace" ] && echo "url: $1://$2:$3/$4$5" >> "$tracefile" + + stop echo "$1://$2:$3/$4$5" | eval openssl s_client \ -connect "$2:$3" "$certfile" -crlf -quiet \ -ign_eof 2> /dev/null > "$pagefile" + stop "openssl fetch" + stop # First line is status and meta information read -r status meta < "$pagefile" status="$(echo "$status" | tr -d '\r\n')" meta="$(echo "$meta" | tr -d '\r\n')" sed -i '1d' "$pagefile" + stop "status extract" debug "response status - meta: $status - $meta" - + # Validate case "$status" in 10) From be7208a364ce8edd84b87fc19df4f6df74b43bed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Wagner?= Date: Sun, 6 Aug 2023 19:45:11 +0200 Subject: [PATCH 2/3] replace `sed` calls in link rendering This reduces the time of rendering geminispace.info/known-hosts (~2200 links on this page by the time of writing) to half (~24secs to 12secs on my testmachine). --- astro | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/astro b/astro index 9ab8171..ede850c 100755 --- a/astro +++ b/astro @@ -175,11 +175,9 @@ parseurl() { typesetgmi() { stop - NR=$'\r' while IFS='' read -r line || [ -n "$line" ]; do line="$(echo "$line" | tr -d '\r')" - #line="${line%$NR}" # shellcheck disable=SC2016 echo "$line" | grep -q '^```' && pre=$((1 - pre)) && line="" @@ -200,7 +198,8 @@ typesetgmi() { #"# "*) sty="$sty_header1" && line="${line#??}" ;; #"> "*) sty="$sty_quote" && line="${line#??}" ;; "=>"*) - link="$(echo "$line" | sed -e 's/^=> *\(\S\+\)\(\s*.*\)/\1 \2/g')" + #link="$(echo "$line" | sed -e 's/^=> *\(\S\+\)\(\s*.*\)/\1 \2/g')" + link=${line#???} echo "$link" >> "$linksfile" linkcount=$((linkcount+1)) @@ -209,7 +208,8 @@ typesetgmi() { [ -z "$line" ] && line="$link" sty="$sty_linkt" - line="$(echo "$sty_linkb$sty_linkt" | sed "s/%linkcount/$linkcount/")$line" + #line="$(echo "$sty_linkb$sty_linkt" | sed "s/%linkcount/$linkcount/")$line" + line="$sty_linkb$sty_linkt $linkcount $line" ;; '* '*) sty="$sty_listt" && line="$sty_listb$sty_listt$(echo "$line" | cut -c 2-)";; *) sty="";; @@ -304,7 +304,7 @@ EOF sed -i '1d' "$pagefile" stop "status extract" debug "response status - meta: $status - $meta" - + # Validate case "$status" in 10) From 55bb291ea1395cacc44282bdd04d0a42d9bd750b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Wagner?= Date: Mon, 7 Aug 2023 19:40:13 +0200 Subject: [PATCH 3/3] perf optimization link rendering --- README.md | 4 ++-- astro | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 6efee53..b2fd396 100644 --- a/README.md +++ b/README.md @@ -75,8 +75,8 @@ sty_header1='\033[35;7;1m' sty_header2='\033[35;4;1m' sty_header3='\033[35;4m' sty_quote='\033[2;3m ' -sty_linkb='\033[35m%linkcount =>' -sty_linkt='\033[36;3m ' +sty_linkb='\033[35m' +sty_linkt=' => \033[36;3m ' sty_listb='\033[35;1m •' sty_listt='\033[0m' ``` diff --git a/astro b/astro index bad6494..f2e6587 100755 --- a/astro +++ b/astro @@ -103,8 +103,8 @@ sty_header1='\\033[35;7;1m' sty_header2='\\033[35;4;1m' sty_header3='\\033[35;4m' sty_quote='\\033[2;3m ' -sty_linkb='\\033[35m%linkcount =>' -sty_linkt='\\033[36;3m ' +sty_linkb='\\033[35m' +sty_linkt=' => \\033[36;3m ' sty_listb='\\033[35;1m •' sty_listt='\\033[0m' EOF @@ -194,16 +194,15 @@ typesetgmi() { "# "*) sty="$sty_header1" && line="${line#'# '}" ;; "> "*) sty="$sty_quote" && line="${line#> }" ;; "=>"*) - #link="$(echo "$line" | sed -e 's/^=> *\(\S\+\)\(\s*.*\)/\1 \2/g')" - link=${line#???} - echo "$link" >> "$linksfile" + link=${line#'=>'} + echo "${link#' '}" >> "$linksfile" linkcount=$((linkcount+1)) # shellcheck disable=SC2086 line="$(echo $link | cut -d' ' -f2-)" [ -z "$line" ] && line="$link" - sty="$(echo "$sty_linkb$sty_linkt" | sed "s/%linkcount/$linkcount/g")" + sty="$sty_linkb${linkcount}$sty_linkt" ;; '* '*) sty="$sty_listt" && line="$sty_listb$sty_listt${line#* }";; *) sty="";; @@ -212,6 +211,7 @@ typesetgmi() { while IFS='' read -r txt do printf "%*s" "$margin" "" + # shellcheck disable=SC2059 printf "$sty" echo "$txt" done