Skip to content

Commit

Permalink
Update 0001-gh-106-install.sh-Perfomance-Use-more-shell-builtins.diff
Browse files Browse the repository at this point in the history
  • Loading branch information
mbargull committed Feb 22, 2021
1 parent 016ba74 commit 219aa29
Showing 1 changed file with 62 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
From 3d7ed69185bda705171729e01c84c12143332236 Mon Sep 17 00:00:00 2001
From: Marcel Bargull <[email protected]>
Date: Fri, 19 Feb 2021 12:49:34 +0100
Subject: [PATCH] install.sh: Perfomance: Use more shell builtins

Replace echo/grep/cut/dirname/basename by variable substitutions and
case pattern matching to reduce the amount of subprocesses called for
every copied file.
---
install.sh | 91 +++++++++++++++++++--------------------------
1 file changed, 38 insertions(+), 53 deletions(-)

diff --git a/install.sh b/install.sh
index e68be89..3120e5d 100755
index e68be89..a1cce81 100755
--- a/install.sh
+++ b/install.sh
@@ -174,11 +174,11 @@ valopt() {
@@ -145,11 +145,16 @@ append_to_file() {

make_dir_recursive() {
local _dir="$1"
- local _line="$ umask 022 && mkdir -p \"$_dir\""
- umask 022 && mkdir -p "$_dir"
- local _retval=$?
- log_line "$_line"
- return $_retval
+ # Skip if the last invocation of make_dir_recursive had the same argument
+ if ! [ "$_dir" = "${_make_dir_recursive_cached_key:-}" ]
+ then
+ _make_dir_recursive_cached_key="$_dir"
+ local _line="$ umask 022 && mkdir -p \"$_dir\""
+ umask 022 && mkdir -p "$_dir"
+ local _retval=$?
+ log_line "$_line"
+ return $_retval
+ fi
}

putvar() {
@@ -174,11 +179,11 @@ valopt() {
eval $v="$default"
for arg in $CFG_ARGS
do
Expand All @@ -31,7 +41,7 @@ index e68be89..3120e5d 100755
done
putvar $v
else
@@ -280,10 +280,10 @@ validate_opt () {
@@ -280,10 +285,10 @@ validate_opt () {
done
for option in $VAL_OPTIONS
do
Expand All @@ -46,18 +56,47 @@ index e68be89..3120e5d 100755
done
if [ "$arg" = "--help" ]
then
@@ -302,8 +302,8 @@ validate_opt () {
@@ -302,9 +307,10 @@ validate_opt () {

absolutify() {
local file_path="$1"
- local file_path_dirname="$(dirname "$file_path")"
- local file_path_basename="$(basename "$file_path")"
- local file_abs_path="$(abs_path "$file_path_dirname")"
+ local file_path_dirname="${file_path%/*}"
+ local file_path_basename="${file_path##*/}"
local file_abs_path="$(abs_path "$file_path_dirname")"
+ abs_path_cached "$file_path_dirname"
+ local file_abs_path="$abs_path_cached_retval"
local file_path="$file_abs_path/$file_path_basename"
# This is the return value
@@ -442,8 +442,8 @@ uninstall_components() {
RETVAL="$file_path"
@@ -312,11 +318,21 @@ absolutify() {

# Prints the absolute path of a directory to stdout
abs_path() {
+ abs_path_cached "$@"
+ printf %s "$abs_path_cached_retval"
+}
+
+abs_path_cached() {
local path="$1"
- # Unset CDPATH because it causes havok: it makes the destination unpredictable
- # and triggers 'cd' to print the path to stdout. Route `cd`'s output to /dev/null
- # for good measure.
- (unset CDPATH && cd "$path" > /dev/null && pwd)
+ # Update return value only if the argument differs from the last invocation
+ if ! [ "$path" = "${_abs_path_cached_key:-}" ]
+ then
+ _abs_path_cached_key="$path"
+ # Unset CDPATH because it causes havok: it makes the destination unpredictable
+ # and triggers 'cd' to print the path to stdout. Route `cd`'s output to /dev/null
+ # for good measure.
+ abs_path_cached_retval="$(unset CDPATH && cd "$path" > /dev/null && pwd)"
+ fi
}

uninstall_legacy() {
@@ -442,8 +458,8 @@ uninstall_components() {
local _directive
while read _directive; do

Expand All @@ -68,7 +107,7 @@ index e68be89..3120e5d 100755

# Sanity checks
if [ ! -n "$_command" ]; then critical_err "malformed installation directive"; fi
@@ -549,8 +549,8 @@ install_components() {
@@ -549,8 +565,8 @@ install_components() {
local _directive
while read _directive; do

Expand All @@ -79,7 +118,7 @@ index e68be89..3120e5d 100755

# Sanity checks
if [ ! -n "$_command" ]; then critical_err "malformed installation directive"; fi
@@ -559,35 +559,23 @@ install_components() {
@@ -559,35 +575,23 @@ install_components() {
# Decide the destination of the file
local _file_install_path="$_dest_prefix/$_file"

Expand Down Expand Up @@ -132,7 +171,7 @@ index e68be89..3120e5d 100755

# HACK: Try to support overriding --docdir. Paths with the form
# "share/doc/$product/" can be redirected to a single --docdir
@@ -601,15 +589,14 @@ install_components() {
@@ -601,15 +605,14 @@ install_components() {
# this problem to be a big deal in practice.
if [ "$CFG_DOCDIR" != "<default>" ]
then
Expand All @@ -153,7 +192,7 @@ index e68be89..3120e5d 100755
critical_need_ok "directory creation failed"

# Make the path absolute so we can uninstall it later without
@@ -625,7 +612,7 @@ install_components() {
@@ -625,7 +628,7 @@ install_components() {

maybe_backup_path "$_file_install_path"

Expand All @@ -162,7 +201,7 @@ index e68be89..3120e5d 100755
then
run cp "$_src_dir/$_component/$_file" "$_file_install_path"
run chmod 755 "$_file_install_path"
@@ -770,8 +757,6 @@ verbose_msg
@@ -770,8 +773,6 @@ verbose_msg

need_cmd mkdir
need_cmd printf
Expand Down

0 comments on commit 219aa29

Please sign in to comment.