Skip to content

Commit 09d0de0

Browse files
committed
perf(core): optimize base functions with caching and improve robustness
- Add global caching for `detect_architecture`, `get_darwin_major`, `get_optimal_parallel_jobs`, and `is_ansi_supported` to reduce subshell overhead. - Improve robustness of `get_lsregister_path` by returning 1 on failure. - Enhance security of `get_user_home` by replacing `eval echo` with `id -P`.
1 parent 0fabc6f commit 09d0de0

File tree

1 file changed

+42
-12
lines changed

1 file changed

+42
-12
lines changed

lib/core/base.sh

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ get_lsregister_path() {
6161
fi
6262
done
6363
echo ""
64-
return 0
64+
return 1
6565
}
6666

6767
# ============================================================================
@@ -191,11 +191,17 @@ is_sip_enabled() {
191191
# Detect CPU architecture
192192
# Returns: "Apple Silicon" or "Intel"
193193
detect_architecture() {
194+
if [[ -n "${MOLE_ARCH_CACHE:-}" ]]; then
195+
echo "$MOLE_ARCH_CACHE"
196+
return 0
197+
fi
198+
194199
if [[ "$(uname -m)" == "arm64" ]]; then
195-
echo "Apple Silicon"
200+
export MOLE_ARCH_CACHE="Apple Silicon"
196201
else
197-
echo "Intel"
202+
export MOLE_ARCH_CACHE="Intel"
198203
fi
204+
echo "$MOLE_ARCH_CACHE"
199205
}
200206

201207
# Get free disk space on root volume
@@ -212,13 +218,19 @@ get_free_space() {
212218
# Get Darwin kernel major version (e.g., 24 for 24.2.0)
213219
# Returns 999 on failure to adopt conservative behavior (assume modern system)
214220
get_darwin_major() {
221+
if [[ -n "${MOLE_DARWIN_MAJOR_CACHE:-}" ]]; then
222+
echo "$MOLE_DARWIN_MAJOR_CACHE"
223+
return 0
224+
fi
225+
215226
local kernel
216227
kernel=$(uname -r 2> /dev/null || true)
217228
local major="${kernel%%.*}"
218229
if [[ ! "$major" =~ ^[0-9]+$ ]]; then
219230
# Return high number to skip potentially dangerous operations on unknown systems
220231
major=999
221232
fi
233+
export MOLE_DARWIN_MAJOR_CACHE="$major"
222234
echo "$major"
223235
}
224236

@@ -233,8 +245,10 @@ is_darwin_ge() {
233245
# Get optimal parallel jobs for operation type (scan|io|compute|default)
234246
get_optimal_parallel_jobs() {
235247
local operation_type="${1:-default}"
236-
local cpu_cores
237-
cpu_cores=$(sysctl -n hw.ncpu 2> /dev/null || echo 4)
248+
if [[ -z "${MOLE_CPU_CORES_CACHE:-}" ]]; then
249+
export MOLE_CPU_CORES_CACHE=$(sysctl -n hw.ncpu 2> /dev/null || echo 4)
250+
fi
251+
local cpu_cores="$MOLE_CPU_CORES_CACHE"
238252
case "$operation_type" in
239253
scan | io)
240254
echo $((cpu_cores * 2))
@@ -318,7 +332,7 @@ get_user_home() {
318332
fi
319333

320334
if [[ -z "$home" ]]; then
321-
home=$(eval echo "~$user" 2> /dev/null || true)
335+
home=$(id -P "$user" 2> /dev/null | cut -d: -f9 || true)
322336
fi
323337

324338
if [[ "$home" == "~"* ]]; then
@@ -586,7 +600,7 @@ mktemp_file() {
586600

587601
# Cleanup all tracked temp files and directories
588602
cleanup_temp_files() {
589-
stop_inline_spinner 2> /dev/null || true
603+
stop_inline_spinner || true
590604
local file
591605
if [[ ${#MOLE_TEMP_FILES[@]} -gt 0 ]]; then
592606
for file in "${MOLE_TEMP_FILES[@]}"; do
@@ -641,7 +655,7 @@ note_activity() {
641655
# Usage: start_section_spinner "message"
642656
start_section_spinner() {
643657
local message="${1:-Scanning...}"
644-
stop_inline_spinner 2> /dev/null || true
658+
stop_inline_spinner || true
645659
if [[ -t 1 ]]; then
646660
MOLE_SPINNER_PREFIX=" " start_inline_spinner "$message"
647661
fi
@@ -651,7 +665,7 @@ start_section_spinner() {
651665
# Usage: stop_section_spinner
652666
stop_section_spinner() {
653667
# Always try to stop spinner (function handles empty PID gracefully)
654-
stop_inline_spinner 2> /dev/null || true
668+
stop_inline_spinner || true
655669
# Always clear line to handle edge cases where spinner output remains
656670
# (e.g., spinner was stopped elsewhere but line not cleared)
657671
if [[ -t 1 ]]; then
@@ -732,27 +746,43 @@ update_progress_if_needed() {
732746
# Usage: is_ansi_supported
733747
# Returns: 0 if supported, 1 if not
734748
is_ansi_supported() {
749+
if [[ -n "${MOLE_ANSI_SUPPORTED_CACHE:-}" ]]; then
750+
return "$MOLE_ANSI_SUPPORTED_CACHE"
751+
fi
752+
735753
# Check if running in interactive terminal
736-
[[ -t 1 ]] || return 1
754+
if ! [[ -t 1 ]]; then
755+
export MOLE_ANSI_SUPPORTED_CACHE=1
756+
return 1
757+
fi
737758

738759
# Check TERM variable
739-
[[ -n "${TERM:-}" ]] || return 1
760+
if [[ -z "${TERM:-}" ]]; then
761+
export MOLE_ANSI_SUPPORTED_CACHE=1
762+
return 1
763+
fi
740764

741765
# Check for known ANSI-compatible terminals
742766
case "$TERM" in
743767
xterm* | vt100 | vt220 | screen* | tmux* | ansi | linux | rxvt* | konsole*)
768+
export MOLE_ANSI_SUPPORTED_CACHE=0
744769
return 0
745770
;;
746771
dumb | unknown)
772+
export MOLE_ANSI_SUPPORTED_CACHE=1
747773
return 1
748774
;;
749775
*)
750776
# Check terminfo database if available
751777
if command -v tput > /dev/null 2>&1; then
752778
# Test if terminal supports colors (good proxy for ANSI support)
753779
local colors=$(tput colors 2> /dev/null || echo "0")
754-
[[ "$colors" -ge 8 ]] && return 0
780+
if [[ "$colors" -ge 8 ]]; then
781+
export MOLE_ANSI_SUPPORTED_CACHE=0
782+
return 0
783+
fi
755784
fi
785+
export MOLE_ANSI_SUPPORTED_CACHE=1
756786
return 1
757787
;;
758788
esac

0 commit comments

Comments
 (0)