From 072df6b1539462c796619ea8edc405b934129713 Mon Sep 17 00:00:00 2001 From: Gmer4Lfe Date: Fri, 17 Jul 2026 01:39:21 -0400 Subject: [PATCH] Replace external dirname/basename with parameter expansion in hot loops lidarr_missing_art.sh's album-directory-map loop (both the cache-hit and live-fallback branches) and arr_profile_enforcer.sh's _is_kids_path() called dirname/basename once per item -- 127K+ tracks and ~4000 series/movies respectively, each call forking a subprocess. Measured: 0.39s vs 72.2s for 20K calls, ~185x. Verified identical output against real paths (including unicode/space/paren edge cases) before switching. --- Arrs_Stack/lidarr_missing_art.sh | 7 +++++-- Tools/arr_profile_enforcer.sh | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Arrs_Stack/lidarr_missing_art.sh b/Arrs_Stack/lidarr_missing_art.sh index 1d89a94..b2ce49f 100755 --- a/Arrs_Stack/lidarr_missing_art.sh +++ b/Arrs_Stack/lidarr_missing_art.sh @@ -279,14 +279,17 @@ if [[ -n "$_cached_tracks" ]]; then info "Using cached track data from lidarr_cleanup.sh — skipping live per-artist walk" while IFS=$'\t' read -r _album_id _track_path; do [[ -z "$_album_id" || -z "$_track_path" || "$_track_path" == "null" ]] && continue - ALBUM_DIR_MAP["$_album_id"]=$(dirname "$_track_path") + # Parameter expansion instead of external dirname — this loop runs once per track + # (127K+ on this library), and dirname forks a subprocess per call. Measured + # 2026-07-17: ~185x faster (0.39s vs 72.2s for 20K calls) for the identical result. + ALBUM_DIR_MAP["$_album_id"]="${_track_path%/*}" done < <(echo "$_cached_tracks" | jq -r '.[] | [(.albumId | tostring), .path] | @tsv' 2>/dev/null) else while IFS= read -r _artist_id; do [[ -z "$_artist_id" ]] && continue while IFS=$'\t' read -r _album_id _track_path; do [[ -z "$_album_id" || -z "$_track_path" || "$_track_path" == "null" ]] && continue - ALBUM_DIR_MAP["$_album_id"]=$(dirname "$_track_path") + ALBUM_DIR_MAP["$_album_id"]="${_track_path%/*}" done < <(curl_json "$LIDARR_URL/api/v1/trackFile?artistId=${_artist_id}&apikey=$LIDARR_API_KEY" | \ jq -r '.[] | [(.albumId | tostring), .path] | @tsv' 2>/dev/null) done < <(echo "$_artist_list" | jq -r '.[].id') diff --git a/Tools/arr_profile_enforcer.sh b/Tools/arr_profile_enforcer.sh index 4abdbef..7c01bf5 100755 --- a/Tools/arr_profile_enforcer.sh +++ b/Tools/arr_profile_enforcer.sh @@ -104,8 +104,11 @@ exit(1); _is_kids_path() { local path="$1" - local dir - dir=$(basename "$(dirname "$path")") + # Parameter expansion instead of external dirname+basename — called once per series/movie + # (~4000 items combined), each call was forking two subprocesses. Measured 2026-07-17: + # ~185x faster per equivalent call (0.39s vs 72.2s per 20K) for the identical result. + local parent="${path%/*}" + local dir="${parent##*/}" [[ "$dir" == *kids* || "$dir" == *anime* ]] }