Skip to content

Commit 53d557b

Browse files
committed
install.sh: Cache abs_path and make_dir_recursive
Since the manifests have sorted file lists, consecutive invocations of abs_path (make_dir_recursive) likely do redundant work. Avoiding these redundancies reduces the amount of subprocessing per file further.
1 parent 3d7ed69 commit 53d557b

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

install-template.sh

+26-10
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,16 @@ append_to_file() {
145145

146146
make_dir_recursive() {
147147
local _dir="$1"
148-
local _line="$ umask 022 && mkdir -p \"$_dir\""
149-
umask 022 && mkdir -p "$_dir"
150-
local _retval=$?
151-
log_line "$_line"
152-
return $_retval
148+
# Skip if the last invocation of make_dir_recursive had the same argument
149+
if ! [ "$_dir" = "${_make_dir_recursive_cached_key:-}" ]
150+
then
151+
_make_dir_recursive_cached_key="$_dir"
152+
local _line="$ umask 022 && mkdir -p \"$_dir\""
153+
umask 022 && mkdir -p "$_dir"
154+
local _retval=$?
155+
log_line "$_line"
156+
return $_retval
157+
fi
153158
}
154159

155160
putvar() {
@@ -304,19 +309,30 @@ absolutify() {
304309
local file_path="$1"
305310
local file_path_dirname="${file_path%/*}"
306311
local file_path_basename="${file_path##*/}"
307-
local file_abs_path="$(abs_path "$file_path_dirname")"
312+
abs_path_cached "$file_path_dirname"
313+
local file_abs_path="$abs_path_cached_retval"
308314
local file_path="$file_abs_path/$file_path_basename"
309315
# This is the return value
310316
RETVAL="$file_path"
311317
}
312318

313319
# Prints the absolute path of a directory to stdout
314320
abs_path() {
321+
abs_path_cached "$@"
322+
printf %s "$abs_path_cached_retval"
323+
}
324+
325+
abs_path_cached() {
315326
local path="$1"
316-
# Unset CDPATH because it causes havok: it makes the destination unpredictable
317-
# and triggers 'cd' to print the path to stdout. Route `cd`'s output to /dev/null
318-
# for good measure.
319-
(unset CDPATH && cd "$path" > /dev/null && pwd)
327+
# Update return value only if the argument differs from the last invocation
328+
if ! [ "$path" = "${_abs_path_cached_key:-}" ]
329+
then
330+
_abs_path_cached_key="$path"
331+
# Unset CDPATH because it causes havok: it makes the destination unpredictable
332+
# and triggers 'cd' to print the path to stdout. Route `cd`'s output to /dev/null
333+
# for good measure.
334+
abs_path_cached_retval="$(unset CDPATH && cd "$path" > /dev/null && pwd)"
335+
fi
320336
}
321337

322338
uninstall_legacy() {

0 commit comments

Comments
 (0)