perf(build.func): eliminate subprocess forks in settings, hostname, and template discovery

Changes:
- Replace 6x echo|sed with bash parameter expansion in
  _build_current_app_vars_tmp() — eliminates 12 subprocess forks
  (6 echo + 6 sed) per advanced settings save cycle.

- Replace 4x echo|tr with bash builtins for lowercase + space
  removal in variables(), base_settings(), advanced_settings()
  hostname and tags handling — eliminates 8 subprocess forks.

- Consolidate 4 grep|awk|grep pipelines in template discovery to
  single awk passes — each template search now uses 1 awk process
  instead of 3 (grep + awk + grep). Applied to all online template
  and version discovery paths in create_lxc_container().

- Consolidate 2 grep|cut reads in ensure_storage_selection_for_vars_file()
  to single while-read loop — 1 file read instead of 2.
This commit is contained in:
MickLesk
2026-03-23 20:39:40 +01:00
parent 21e215dc1b
commit a02bdf083d
+30 -47
View File
@@ -38,7 +38,7 @@
# - Captures app-declared resource defaults (CPU, RAM, Disk) # - Captures app-declared resource defaults (CPU, RAM, Disk)
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
variables() { variables() {
NSAPP=$(echo "${APP,,}" | tr -d ' ') # This function sets the NSAPP variable by converting the value of the APP variable to lowercase and removing any spaces. NSAPP="${APP,,}"; NSAPP="${NSAPP// /}" # Lowercase + strip spaces (pure bash, no subprocesses)
var_install="${NSAPP}-install" # sets the var_install variable by appending "-install" to the value of NSAPP. var_install="${NSAPP}-install" # sets the var_install variable by appending "-install" to the value of NSAPP.
INTEGER='^[0-9]+([.][0-9]+)?$' # it defines the INTEGER regular expression pattern. INTEGER='^[0-9]+([.][0-9]+)?$' # it defines the INTEGER regular expression pattern.
PVEHOST_NAME=$(hostname) # gets the Proxmox Hostname and sets it to Uppercase PVEHOST_NAME=$(hostname) # gets the Proxmox Hostname and sets it to Uppercase
@@ -989,7 +989,7 @@ base_settings() {
# Validate and set Hostname/FQDN # Validate and set Hostname/FQDN
local requested_hostname="${var_hostname:-$NSAPP}" local requested_hostname="${var_hostname:-$NSAPP}"
requested_hostname=$(echo "${requested_hostname,,}" | tr -d ' ') requested_hostname="${requested_hostname,,}"; requested_hostname="${requested_hostname// /}"
if ! validate_hostname "$requested_hostname"; then if ! validate_hostname "$requested_hostname"; then
if [[ -n "${var_hostname:-}" ]]; then if [[ -n "${var_hostname:-}" ]]; then
msg_warn "Invalid hostname '$requested_hostname'. Using default: $NSAPP" msg_warn "Invalid hostname '$requested_hostname'. Using default: $NSAPP"
@@ -1509,12 +1509,10 @@ _build_vars_diff() {
_build_current_app_vars_tmp() { _build_current_app_vars_tmp() {
tmpf="$(mktemp /tmp/${NSAPP:-app}.vars.new.XXXXXX)" tmpf="$(mktemp /tmp/${NSAPP:-app}.vars.new.XXXXXX)"
# NET/GW # NET/GW — pure bash parameter expansion (no subprocess forks)
_net="${NET:-}" _net="${NET:-}"
_gate="" _gate="${GATE#,gw=}"
case "${GATE:-}" in [[ "$_gate" == "${GATE:-}" ]] && _gate=""
,gw=*) _gate=$(echo "$GATE" | sed 's/^,gw=//') ;;
esac
# IPv6 # IPv6
_ipv6_method="${IPV6_METHOD:-auto}" _ipv6_method="${IPV6_METHOD:-auto}"
@@ -1526,28 +1524,18 @@ _build_current_app_vars_tmp() {
fi fi
# MTU/VLAN/MAC # MTU/VLAN/MAC
_mtu="" _mtu="${MTU#,mtu=}"
_vlan="" [[ "$_mtu" == "${MTU:-}" ]] && _mtu=""
_mac="" _vlan="${VLAN#,tag=}"
case "${MTU:-}" in [[ "$_vlan" == "${VLAN:-}" ]] && _vlan=""
,mtu=*) _mtu=$(echo "$MTU" | sed 's/^,mtu=//') ;; _mac="${MAC#,hwaddr=}"
esac [[ "$_mac" == "${MAC:-}" ]] && _mac=""
case "${VLAN:-}" in
,tag=*) _vlan=$(echo "$VLAN" | sed 's/^,tag=//') ;;
esac
case "${MAC:-}" in
,hwaddr=*) _mac=$(echo "$MAC" | sed 's/^,hwaddr=//') ;;
esac
# DNS / Searchdomain # DNS / Searchdomain
_ns="" _ns="${NS#-nameserver=}"
_searchdomain="" [[ "$_ns" == "${NS:-}" ]] && _ns=""
case "${NS:-}" in _searchdomain="${SD#-searchdomain=}"
-nameserver=*) _ns=$(echo "$NS" | sed 's/^-nameserver=//') ;; [[ "$_searchdomain" == "${SD:-}" ]] && _searchdomain=""
esac
case "${SD:-}" in
-searchdomain=*) _searchdomain=$(echo "$SD" | sed 's/^-searchdomain=//') ;;
esac
# SSH / APT / Features # SSH / APT / Features
_ssh="${SSH:-no}" _ssh="${SSH:-no}"
@@ -1709,10 +1697,14 @@ maybe_offer_save_app_defaults() {
ensure_storage_selection_for_vars_file() { ensure_storage_selection_for_vars_file() {
local vf="$1" local vf="$1"
# Read stored values (if any) # Read stored values (single file read)
local tpl ct local tpl="" ct="" _line
tpl=$(grep -E '^var_template_storage=' "$vf" | cut -d= -f2-) while IFS='=' read -r _key _val; do
ct=$(grep -E '^var_container_storage=' "$vf" | cut -d= -f2-) case "$_key" in
var_template_storage) tpl="$_val" ;;
var_container_storage) ct="$_val" ;;
esac
done < "$vf"
if [[ -n "$tpl" && -n "$ct" ]]; then if [[ -n "$tpl" && -n "$ct" ]]; then
TEMPLATE_STORAGE="$tpl" TEMPLATE_STORAGE="$tpl"
@@ -1982,7 +1974,7 @@ advanced_settings() {
--inputbox "\nSet Hostname (or FQDN, e.g. host.example.com)" 10 58 "$_hostname" \ --inputbox "\nSet Hostname (or FQDN, e.g. host.example.com)" 10 58 "$_hostname" \
3>&1 1>&2 2>&3); then 3>&1 1>&2 2>&3); then
local hn_test="${result:-$NSAPP}" local hn_test="${result:-$NSAPP}"
hn_test=$(echo "${hn_test,,}" | tr -d ' ') hn_test="${hn_test,,}"; hn_test="${hn_test// /}"
if validate_hostname "$hn_test"; then if validate_hostname "$hn_test"; then
_hostname="$hn_test" _hostname="$hn_test"
@@ -2370,7 +2362,7 @@ advanced_settings() {
--inputbox "\nSet Custom Tags (semicolon-separated)\n(alphanumeric, hyphens, underscores only)" 12 58 "$_tags" \ --inputbox "\nSet Custom Tags (semicolon-separated)\n(alphanumeric, hyphens, underscores only)" 12 58 "$_tags" \
3>&1 1>&2 2>&3); then 3>&1 1>&2 2>&3); then
local tags_test="${result:-}" local tags_test="${result:-}"
tags_test=$(echo "$tags_test" | tr -d '[:space:]') tags_test="${tags_test//[[:space:]]/}"
if validate_tags "$tags_test"; then if validate_tags "$tags_test"; then
_tags="$tags_test" _tags="$tags_test"
((STEP++)) ((STEP++))
@@ -5310,7 +5302,7 @@ create_lxc_container() {
fi fi
ONLINE_TEMPLATES=() ONLINE_TEMPLATES=()
mapfile -t ONLINE_TEMPLATES < <(pveam_cached available -section system | grep -E '\.(tar\.zst|tar\.xz|tar\.gz)$' | awk '{print $2}' | grep -E "^${TEMPLATE_SEARCH}.*${TEMPLATE_PATTERN}" | sort -t - -k 2 -V 2>/dev/null || true) mapfile -t ONLINE_TEMPLATES < <(pveam_cached available -section system | awk -v s="^${TEMPLATE_SEARCH}" -v p="${TEMPLATE_PATTERN}" '$2 ~ /\.(tar\.zst|tar\.xz|tar\.gz)$/ && $2 ~ s && $2 ~ p {print $2}' | sort -t - -k 2 -V 2>/dev/null || true)
[[ ${#ONLINE_TEMPLATES[@]} -gt 0 ]] && ONLINE_TEMPLATE="${ONLINE_TEMPLATES[-1]}" [[ ${#ONLINE_TEMPLATES[@]} -gt 0 ]] && ONLINE_TEMPLATE="${ONLINE_TEMPLATES[-1]}"
TEMPLATE="$ONLINE_TEMPLATE" TEMPLATE="$ONLINE_TEMPLATE"
@@ -5326,10 +5318,7 @@ create_lxc_container() {
AVAILABLE_VERSIONS=() AVAILABLE_VERSIONS=()
mapfile -t AVAILABLE_VERSIONS < <( mapfile -t AVAILABLE_VERSIONS < <(
pveam_cached available -section system | pveam_cached available -section system |
grep -E '\.(tar\.zst|tar\.xz|tar\.gz)$' | awk -F'\t' -v os="${PCT_OSTYPE}" '$2 ~ /\.(tar\.zst|tar\.xz|tar\.gz)$/ && $1 ~ "^"os"-" { sub(".*"os"-", "", $1); sub(/[^0-9.].*/,"",$1); if($1~/^[0-9]/) print $1 }' |
awk -F'\t' '{print $1}' |
grep "^${PCT_OSTYPE}-" |
sed -E "s/.*${PCT_OSTYPE}-([0-9]+(\.[0-9]+)?).*/\1/" |
sort -u -V 2>/dev/null sort -u -V 2>/dev/null
) )
@@ -5349,9 +5338,7 @@ create_lxc_container() {
ONLINE_TEMPLATES=() ONLINE_TEMPLATES=()
mapfile -t ONLINE_TEMPLATES < <( mapfile -t ONLINE_TEMPLATES < <(
pveam_cached available -section system | pveam_cached available -section system |
grep -E '\.(tar\.zst|tar\.xz|tar\.gz)$' | awk -v s="^${TEMPLATE_SEARCH}-" -v p="${TEMPLATE_PATTERN}" '$2 ~ /\.(tar\.zst|tar\.xz|tar\.gz)$/ && $2 ~ s && $2 ~ p {print $2}' |
awk '{print $2}' |
grep -E "^${TEMPLATE_SEARCH}-.*${TEMPLATE_PATTERN}" |
sort -t - -k 2 -V 2>/dev/null || true sort -t - -k 2 -V 2>/dev/null || true
) )
@@ -5390,9 +5377,7 @@ create_lxc_container() {
# Get available versions # Get available versions
mapfile -t AVAILABLE_VERSIONS < <( mapfile -t AVAILABLE_VERSIONS < <(
pveam_cached available -section system | pveam_cached available -section system |
grep "^${PCT_OSTYPE}-" | awk -v os="${PCT_OSTYPE}" '$1 ~ "^"os"-" { sub(".*"os"-", "", $1); sub(/[^0-9.].*/,"",$1); if($1~/^[0-9]+\.[0-9]+$/) print $1 }' |
sed -E 's/.*'"${PCT_OSTYPE}"'-([0-9]+\.[0-9]+).*/\1/' |
grep -E '^[0-9]+\.[0-9]+$' |
sort -u -V 2>/dev/null || sort -u sort -u -V 2>/dev/null || sort -u
) )
@@ -5420,9 +5405,7 @@ create_lxc_container() {
) )
mapfile -t ONLINE_TEMPLATES < <( mapfile -t ONLINE_TEMPLATES < <(
pveam_cached available -section system | pveam_cached available -section system |
grep -E '\.(tar\.zst|tar\.xz|tar\.gz)$' | awk -v s="^${TEMPLATE_SEARCH}-" -v p="${TEMPLATE_PATTERN}" '$2 ~ /\.(tar\.zst|tar\.xz|tar\.gz)$/ && $2 ~ s && $2 ~ p {print $2}' |
awk '{print $2}' |
grep -E "^${TEMPLATE_SEARCH}-.*${TEMPLATE_PATTERN}" |
sort -t - -k 2 -V 2>/dev/null || true sort -t - -k 2 -V 2>/dev/null || true
) )
ONLINE_TEMPLATE="" ONLINE_TEMPLATE=""