From 46a4fa605e2c4b687c119f0ef0cdc970fb5af81e Mon Sep 17 00:00:00 2001 From: cem Date: Tue, 11 Dec 2018 01:38:50 +0000 Subject: [PATCH] rc.subr: Implement list_vars without using 'read' 'read' pessimistically read(2)s one byte at a time, which can be quite silly for large environments in slow emulators. In my boring user environment, truss shows that the number of read() syscalls to source rc.subr and invoke list_vars is reduced by something like 3400 to 60. ministat(1) shows a significant time difference of about -71% for my environment. Suggested by: jilles Discussed with: dteske, jhb, jilles Differential Revision: https://reviews.freebsd.org/D18481 --- libexec/rc/rc.subr | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/libexec/rc/rc.subr b/libexec/rc/rc.subr index 072eda2cebf..6688438360f 100644 --- a/libexec/rc/rc.subr +++ b/libexec/rc/rc.subr @@ -58,17 +58,29 @@ JID=0 # --------- # list_vars pattern -# List vars matching pattern. +# List variables matching glob pattern. # list_vars() { - set | { while read LINE; do - var="${LINE%%=*}" - case "$var" in - "$LINE"|*[!a-zA-Z0-9_]*) continue ;; - $1) echo $var + # Localize 'set' option below. + local - + local IFS=$'\n' line varname + + # Disable path expansion in unquoted 'for' parameters below. + set -o noglob + + for line in $(set); do + varname="${line%%=*}" + + case "$varname" in + "$line"|*[!a-zA-Z0-9_]*) + continue + ;; + $1) + echo $varname + ;; esac - done; } + done } # set_rcvar [var] [defval] [desc] -- 2.45.0