cc-wrapper: More quadratic performance fixes

This eliminates the slow lookup of whether we've already seen an rpath
/ library path entry.

Issue #27609.

+21 -23
+21 -23
pkgs/build-support/cc-wrapper/ld-wrapper.sh
··· 64 64 # Add all used dynamic libraries to the rpath. 65 65 if [ "$NIX_DONT_SET_RPATH" != 1 ]; then 66 66 67 - libPath="" 67 + declare -A libDirsSeen 68 + declare -a libDirs 69 + 68 70 addToLibPath() { 69 71 local path="$1" 70 72 if [ "${path:0:1}" != / ]; then return 0; fi ··· 76 78 fi 77 79 ;; 78 80 esac 79 - case $libPath in 80 - *\ $path\ *) return 0 ;; 81 - esac 82 - libPath+=" $path " 81 + if [[ -z ${libDirsSeen[$path]} ]]; then 82 + libDirs+=("$path") 83 + libDirsSeen[$path]=1 84 + fi 83 85 } 86 + 87 + declare -A rpathsSeen 88 + declare -a rpaths 84 89 85 90 addToRPath() { 86 91 # If the path is not in the store, don't add it to the rpath. 87 92 # This typically happens for libraries in /tmp that are later 88 93 # copied to $out/lib. If not, we're screwed. 89 94 if [ "${1:0:${#NIX_STORE}}" != "$NIX_STORE" ]; then return 0; fi 90 - case $rpath in 91 - *\ $1\ *) return 0 ;; 92 - esac 93 - rpath+=" $1 " 94 - } 95 - 96 - libs="" 97 - addToLibs() { 98 - libs+=" $1" 95 + if [[ -z ${rpathsSeen[$1]} ]]; then 96 + rpaths+=("$1") 97 + rpathsSeen[$1]=1 98 + fi 99 99 } 100 100 101 - rpath="" 101 + declare -a libs 102 102 103 103 # First, find all -L... switches. 104 104 allParams=("${params[@]}" ${extra[@]}) ··· 112 112 addToLibPath ${p2} 113 113 n=$((n + 1)) 114 114 elif [ "$p" = -l ]; then 115 - addToLibs ${p2} 115 + libs+=(${p2}) 116 116 n=$((n + 1)) 117 117 elif [ "${p:0:2}" = -l ]; then 118 - addToLibs ${p:2} 118 + libs+=(${p:2}) 119 119 elif [ "$p" = -dynamic-linker ]; then 120 120 # Ignore the dynamic linker argument, or it 121 121 # will get into the next 'elif'. We don't want ··· 135 135 # so, add the directory to the rpath. 136 136 # It's important to add the rpath in the order of -L..., so 137 137 # the link time chosen objects will be those of runtime linking. 138 - 139 - for i in $libPath; do 140 - for j in $libs; do 138 + for i in ${libDirs[@]}; do 139 + for j in ${libs[@]}; do 141 140 if [ -f "$i/lib$j.so" ]; then 142 141 addToRPath $i 143 142 break ··· 145 144 done 146 145 done 147 146 148 - 149 147 # Finally, add `-rpath' switches. 150 - for i in $rpath; do 151 - extra+=(-rpath $i) 148 + for i in ${rpaths[@]}; do 149 + extra+=(-rpath "$i") 152 150 done 153 151 fi 154 152