[PATCH 1/5] Hardcode ANSI escapes to replace tput in logging functions

Trek trek00 at inbox.ru
Tue Apr 26 22:48:07 BST 2022


To avoid performance penalities, most of tput calls inside
init-functions are replaced with their respective ANSI escape
sequences. The fancy output is enabled without checking the
terminal capabilities, assuming it is VT100 compatible.

With this patch, log_end_msg is near 145x faster. With
50-ubuntu-logging enabled it is near 47x faster.

Thanks: Adam Borowski <kilobyte at angband.pl>
        Thorsten Glaser <t.glaser at tarent.de>
http://www.chiark.greenend.org.uk/pipermail/debian-init-diversity/2020-September/003557.html
---
 init-functions                     | 30 ++++++++++---------------
 init-functions.d/50-ubuntu-logging | 35 ++++++++++++++++--------------
 2 files changed, 31 insertions(+), 34 deletions(-)

diff --git a/init-functions b/init-functions
index 207b270..f2088b3 100644
--- a/init-functions
+++ b/init-functions
@@ -210,14 +210,9 @@ status_of_proc () {
 }
 
 log_use_fancy_output () {
-    TPUT=/usr/bin/tput
-    EXPR=/usr/bin/expr
     if  [ -t 1 ] &&
 	[ "x${TERM:-}" != "x" ] &&
-	[ "x${TERM:-}" != "xdumb" ] &&
-	[ -x $TPUT ] && [ -x $EXPR ] &&
-	$TPUT hpa 60 >/dev/null 2>&1 &&
-	$TPUT setaf 1 >/dev/null 2>&1
+	[ "x${TERM:-}" != "xdumb" ]
     then
         [ -z $FANCYTTY ] && FANCYTTY=1 || true
     else
@@ -349,23 +344,21 @@ log_end_msg () {
     log_end_msg_pre "$@"
 
     # Only do the fancy stuff if we have an appropriate terminal
-    # and if /usr is already mounted
     if log_use_fancy_output; then
-        RED=$( $TPUT setaf 1)
-        YELLOW=$( $TPUT setaf 3)
-        NORMAL=$( $TPUT op)
+        esc=''		# escape character, printf '\033'
+        red="$esc[31m"		# ANSI color escapes
+        yellow="$esc[33m"
+        normal="$esc[0m"
     else
-        RED=''
-        YELLOW=''
-        NORMAL=''
+        red= yellow= normal=
     fi
 
     if [ $1 -eq 0 ]; then
         echo "." || true
     elif [ $1 -eq 255 ]; then
-        /bin/echo -e " ${YELLOW}(warning).${NORMAL}" || true
+        echo " ${yellow}(warning).${normal}" || true
     else
-        /bin/echo -e " ${RED}failed!${NORMAL}" || true
+        echo " ${red}failed!${normal}" || true
     fi
     log_end_msg_post "$@"
     return $retval
@@ -400,9 +393,10 @@ log_action_end_msg () {
         echo "done${end}" || true
     else
         if log_use_fancy_output; then
-            RED=$( $TPUT setaf 1)
-            NORMAL=$( $TPUT op)
-            /bin/echo -e "${RED}failed${end}${NORMAL}" || true
+            esc=''		# escape character, printf '\033'
+            red="$esc[31m"	# ANSI color escapes
+            normal="$esc[0m"
+            echo "${red}failed${end}${normal}" || true
         else
             echo "failed${end}" || true
         fi
diff --git a/init-functions.d/50-ubuntu-logging b/init-functions.d/50-ubuntu-logging
index 4fdadcf..32bd160 100644
--- a/init-functions.d/50-ubuntu-logging
+++ b/init-functions.d/50-ubuntu-logging
@@ -15,9 +15,10 @@ log_success_msg () {
 
 log_failure_msg () {
     if log_use_fancy_output; then
-        RED=`$TPUT setaf 1`
-        NORMAL=`$TPUT op`
-        echo " $RED*$NORMAL $@" || true
+        esc=''		# escape character, printf '\033'
+        red="$esc[31m"		# ANSI color escapes
+        normal="$esc[0m"
+        echo " $red*$normal $@" || true
     else
         echo " * $@" || true
     fi
@@ -25,9 +26,10 @@ log_failure_msg () {
 
 log_warning_msg () {
     if log_use_fancy_output; then
-        YELLOW=`$TPUT setaf 3`
-        NORMAL=`$TPUT op`
-        echo " $YELLOW*$NORMAL $@" || true
+        esc=''		# escape character, printf '\033'
+        yellow="$esc[33m"	# ANSI color escapes
+        normal="$esc[0m"
+        echo " $yellow*$normal $@" || true
     else
         echo " * $@" || true
     fi
@@ -42,10 +44,11 @@ log_daemon_msg () {
         return 1
     fi
 
+    TPUT=/usr/bin/tput
     if log_use_fancy_output && $TPUT xenl >/dev/null 2>&1; then
         COLS=`$TPUT cols`
         if [ "$COLS" ] && [ "$COLS" -gt 6 ]; then
-            COL=`$EXPR $COLS - 7`
+            COL=$(( COLS - 7 ))
         else
             COLS=80
             COL=73
@@ -75,8 +78,9 @@ log_daemon_msg () {
         # Enough trailing spaces for ` [fail]' to fit in; if the message
         # is too long it wraps here rather than later, which is what we
         # want.
-        $TPUT hpa `$EXPR $COLS - 1` || true
-        printf ' ' || true
+        esc=''		# escape character, printf '\033'
+        movecur="$esc[${COLS}G"	# ANSI horizontal position absolute
+        printf "$movecur " || true
     else
         echo " * $@" || true
         COL=
@@ -102,16 +106,15 @@ log_end_msg () {
             fi
         fi
 
-        printf "\r" || true
-        $TPUT hpa $COL
+        esc=''			# escape character, printf '\033'
+        movecur="$esc[$(( COL + 1 ))G"	# ANSI horizontal position absolute
+        printf "\r$movecur" || true
         if [ "$1" -eq 0 ]; then
             echo "[ OK ]" || true
         else
-            printf '[' || true
-            $TPUT setaf 1 || true # red
-            printf fail || true
-            $TPUT op || true # normal
-            echo ']' || true
+            red="$esc[31m"	# ANSI color escapes
+            normal="$esc[0m"
+            echo "[${red}fail${normal}]" || true
         fi
     else
         if [ "$1" -eq 0 ]; then
-- 
2.20.1



More information about the Debian-init-diversity mailing list