Skip to content

Commit

Permalink
cc-wrapper: More quadratic performance fixes
Browse files Browse the repository at this point in the history
This eliminates the slow lookup of whether we've already seen an rpath
/ library path entry.

Issue #27609.
  • Loading branch information
edolstra committed Jul 25, 2017
1 parent aa4a92d commit 47821f1
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions pkgs/build-support/cc-wrapper/ld-wrapper.sh
Expand Up @@ -64,7 +64,9 @@ extra+=($NIX_LDFLAGS_AFTER $NIX_LDFLAGS_HARDEN)
# Add all used dynamic libraries to the rpath.
if [ "$NIX_DONT_SET_RPATH" != 1 ]; then

libPath=""
declare -A libDirsSeen

This comment has been minimized.

Copy link
@Ericson2314

Ericson2314 Jul 25, 2017

Member

~~This won't work in nix-shell on Darwin :D cc @copumpkin. Associate arrays are a bash 4 feature.~~~

Nevermind, it's not sourced 😊.

This comment has been minimized.

Copy link
@copumpkin

copumpkin Jul 25, 2017

Member

Nope it's fine due to shebang at the top 😄 the only issue is the stuff that nix-shell runs directly under the host shell, which should just be a few stdenv hooks

declare -a libDirs

addToLibPath() {
local path="$1"
if [ "${path:0:1}" != / ]; then return 0; fi
Expand All @@ -76,29 +78,27 @@ if [ "$NIX_DONT_SET_RPATH" != 1 ]; then
fi
;;
esac
case $libPath in
*\ $path\ *) return 0 ;;
esac
libPath+=" $path "
if [[ -z ${libDirsSeen[$path]} ]]; then
libDirs+=("$path")
libDirsSeen[$path]=1
fi
}

declare -A rpathsSeen
declare -a rpaths

addToRPath() {
# If the path is not in the store, don't add it to the rpath.
# This typically happens for libraries in /tmp that are later
# copied to $out/lib. If not, we're screwed.
if [ "${1:0:${#NIX_STORE}}" != "$NIX_STORE" ]; then return 0; fi
case $rpath in
*\ $1\ *) return 0 ;;
esac
rpath+=" $1 "
}

libs=""
addToLibs() {
libs+=" $1"
if [[ -z ${rpathsSeen[$1]} ]]; then
rpaths+=("$1")
rpathsSeen[$1]=1
fi
}

rpath=""
declare -a libs

# First, find all -L... switches.
allParams=("${params[@]}" ${extra[@]})
Expand All @@ -112,10 +112,10 @@ if [ "$NIX_DONT_SET_RPATH" != 1 ]; then
addToLibPath ${p2}
n=$((n + 1))
elif [ "$p" = -l ]; then
addToLibs ${p2}
libs+=(${p2})
n=$((n + 1))
elif [ "${p:0:2}" = -l ]; then
addToLibs ${p:2}
libs+=(${p:2})
elif [ "$p" = -dynamic-linker ]; then
# Ignore the dynamic linker argument, or it
# will get into the next 'elif'. We don't want
Expand All @@ -135,20 +135,18 @@ if [ "$NIX_DONT_SET_RPATH" != 1 ]; then
# so, add the directory to the rpath.
# It's important to add the rpath in the order of -L..., so
# the link time chosen objects will be those of runtime linking.

for i in $libPath; do
for j in $libs; do
for i in ${libDirs[@]}; do
for j in ${libs[@]}; do
if [ -f "$i/lib$j.so" ]; then
addToRPath $i
break
fi
done
done


# Finally, add `-rpath' switches.
for i in $rpath; do
extra+=(-rpath $i)
for i in ${rpaths[@]}; do
extra+=(-rpath "$i")
done
fi

Expand Down

0 comments on commit 47821f1

Please sign in to comment.