Skip to content

Commit

Permalink
build!: optimize for x86-64-v3 cpu microarchitecture (Haswell+) (#2374)
Browse files Browse the repository at this point in the history
  • Loading branch information
lklimek authored Dec 18, 2024
1 parent 82a6217 commit d7143cc
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 11 deletions.
5 changes: 4 additions & 1 deletion .cargo/config-release.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
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=x86-64-v3"]

[target.x86_64-unknown-linux-gnu]
rustflags = ["-C", "target-feature=-crt-static", "-C", "target-cpu=x86-64-v3"]

[target.aarch64-unknown-linux-gnu]
linker = "aarch64-linux-gnu-gcc"
19 changes: 18 additions & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,24 @@
rustflags = ["-C", "target-feature=-crt-static", "--cfg", "tokio_unstable"]

[target.x86_64-unknown-linux-musl]
rustflags = ["-C", "target-feature=-crt-static", "--cfg", "tokio_unstable"]
rustflags = [
"-C",
"target-feature=-crt-static",
"--cfg",
"tokio_unstable",
"-C",
"target-cpu=x86-64",
]

[target.x86_64-unknown-linux-gnu]
rustflags = [
"-C",
"target-feature=-crt-static",
"--cfg",
"tokio_unstable",
"-C",
"target-cpu=x86-64",
]

[target.aarch64-unknown-linux-gnu]
linker = "aarch64-linux-gnu-gcc"
Expand Down
35 changes: 26 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,28 @@ 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 [[ "$TARGETARCH" == "amd64" ]] ; then
if [[ "${CARGO_BUILD_PROFILE}" == "release" ]] ; then
echo "export CFLAGS=-march=x86-64-v3" >> /root/env
echo "export CXXFLAGS=-march=x86-64-v3" >> /root/env
echo "export PORTABLE=x86-64-v3" >> /root/env
else
echo "export CFLAGS=-march=x86-64" >> /root/env
echo "export CXXFLAGS=-march=x86-64" >> /root/env
echo "export PORTABLE=x86-64" >> /root/env
fi
else
echo "export PORTABLE=1" >> /root/env
fi
EOS

# Install protoc - protobuf compiler
# The one shipped with Alpine does not work
Expand Down Expand Up @@ -258,14 +277,15 @@ 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

make -j$(nproc) static_lib
mkdir -p /opt/rocksdb/usr/local/lib
cp librocksdb.a /opt/rocksdb/usr/local/lib/
Expand Down Expand Up @@ -320,10 +340,6 @@ RUN --mount=type=secret,id=AWS \
--no-track \
--no-confirm


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

#
# Rust build planner to speed up builds
#
Expand Down Expand Up @@ -465,6 +481,7 @@ RUN --mount=type=cache,sharing=shared,id=cargo_registry_index,target=${CARGO_HOM
# Remove /platform to reduce layer size
rm -rf /platform


#
# STAGE: BUILD JAVASCRIPT INTERMEDIATE IMAGE
#
Expand Down
4 changes: 4 additions & 0 deletions packages/rs-drive-abci/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,7 @@ grovedbg = ["drive/grovedbg"]
[[bin]]
name = "drive-abci"
path = "src/main.rs"


[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(tokio_unstable)'] }

0 comments on commit d7143cc

Please sign in to comment.