Skip to content

Instantly share code, notes, and snippets.

@cpcloud
Last active June 28, 2018 18:06
Show Gist options
  • Save cpcloud/995c80b1eeae09b8bd24e63a3f488bc2 to your computer and use it in GitHub Desktop.
Save cpcloud/995c80b1eeae09b8bd24e63a3f488bc2 to your computer and use it in GitHub Desktop.
Bash script to demonstrate the difference in size when stripping symbols and using -Os
#!/bin/bash
set -eo pipefail
function env_exists() {
conda env list | cut -d ' ' -f 1 | grep -v '#' | grep "${1}" 1> /dev/null
}
envname="skimage-test-opt"
# if our env exists, blow it away
if [ $(env_exists "${envname}") ]; then
conda env remove -n "${envname}" -y
fi
# create an env to test with
conda create -n "${envname}" scikit-image cython nomkl -y -c conda-forge
cloneloc="/tmp/"${envname}""
rm -rf "${cloneloc}"
# shallow-ish clone of scikit image
git clone --depth=20 https://github.com/scikit-image/scikit-image "${cloneloc}"
skimage_hash=49f964e6babdb6fd48bd94d49c63049d7b58a432
# go to the clone
pushd "${cloneloc}"
# reset to a hash we know we can patch
git reset --hard "${skimage_hash}"
# activate the environment
source activate "${envname}"
# export the env in case someone wants to look at what's installed
conda env export > "${envname}.yml"
echo "Building WITHOUT size optimization and stripping ..."
python setup.py develop > /dev/null 2>&1
# compute the total size of dynamic shared objects built before optimization
before_total=$(du -hc $(find "$PWD" -name '*.so') | grep total)
# apply the patch to setup.py that strips all unnecessary symbols, builds for
# size and does not include debug symbols
cat <<EOF | git apply
diff --git a/setup.py b/setup.py
index 74115d5d..033b5e5a 100644
--- a/setup.py
+++ b/setup.py
@@ -81,6 +81,11 @@ def configuration(parent_package='', top_path=None):
config.add_subpackage('skimage')
config.add_data_dir('skimage/data')
+ for ext in config.ext_modules:
+ ext.extra_link_args.append('-Wl,--strip-all')
+ ext.extra_compile_args.append('-Os')
+ ext.extra_compile_args.append('-g0')
+
return config
EOF
echo "Cleaning ..."
python setup.py clean
find . -name '*.so' -delete
echo "Building WITH size optimization and stripping ..."
python setup.py develop > /dev/null 2>&1
# compute the total size of dynamic shared objects built after optimization
after_total=$(du -hc $(find "$PWD" -name '*.so') | grep total)
echo "WITHOUT optimization: "${before_total}""
echo "WITH optimization: "${after_total}""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment