Skip to content

Commit

Permalink
build!: optimize build process for Haswell CPUs
Browse files Browse the repository at this point in the history
  • Loading branch information
lklimek committed Dec 3, 2024
1 parent d8eb951 commit 348963b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .cargo/config-release.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
rustflags = ["-C", "target-feature=-crt-static"]

[target.x86_64-unknown-linux-musl]
rustflags = ["-C", "target-feature=-crt-static"]
rustflags = ["-C", "target-feature=-crt-static", "-C", "target-cpu=haswell"]

[target.aarch64-unknown-linux-gnu]
linker = "aarch64-linux-gnu-gcc"
31 changes: 24 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,19 @@ RUN TOOLCHAIN_VERSION="$(grep channel rust-toolchain.toml | awk '{print $3}' | t
ONBUILD ENV HOME=/root
ONBUILD ENV CARGO_HOME=$HOME/.cargo
# Configure Rust toolchain
ONBUILD ARG CARGO_BUILD_PROFILE=dev
# Configure Rust toolchain and C / C++ compiler
RUN <<EOS
# It doesn't sharing PATH between stages, so we need "source $HOME/.cargo/env" everywhere
RUN echo 'source $HOME/.cargo/env' >> /root/env
echo 'source $HOME/.cargo/env' >> /root/env

# Enable gcc / g++ optimizations
if [[ "${CARGO_BUILD_PROFILE}" == "release" ]] ; then
echo "export CFLAGS=-march=haswell" >> /root/env
echo "export CXXFLAGS=-march=haswell" >> /root/env
fi
EOS

# Install protoc - protobuf compiler
# The one shipped with Alpine does not work
Expand Down Expand Up @@ -258,13 +268,23 @@ WORKDIR /tmp/rocksdb
# sccache -s
# EOS

# Select whether we want dev or release
# This variable will be also visibe in next stages
ONBUILD ARG CARGO_BUILD_PROFILE=dev

RUN --mount=type=secret,id=AWS <<EOS
set -ex -o pipefail
git clone https://github.com/facebook/rocksdb.git -b v8.10.2 --depth 1 .
source /root/env

# Support any CPU architecture
export PORTABLE=1
# For x86-64 release, tune the build to use haswell optimizations.
# It requires 64-bit extensions, MMX, SSE, SSE2, SSE3, SSSE3, SSE4.1, SSE4.2, POPCNT, CX16, SAHF, FXSR, AVX, XSAVE, PCLMUL,
# FSGSBASE, RDRND, F16C, AVX2, BMI, BMI2, LZCNT, FMA, MOVBE and HLE instruction set support.
if [[ "$TARGETARCH" == "amd64" ]] && [[ "$CARGO_BUILD_PROFILE" == "release" ]] ; then
export PORTABLE=haswell
else
export PORTABLE=1
fi

make -j$(nproc) static_lib
mkdir -p /opt/rocksdb/usr/local/lib
Expand Down Expand Up @@ -321,9 +341,6 @@ RUN --mount=type=secret,id=AWS \
--no-confirm


# Select whether we want dev or release
ONBUILD ARG CARGO_BUILD_PROFILE=dev

#
# Rust build planner to speed up builds
#
Expand Down

0 comments on commit 348963b

Please sign in to comment.