Skip to content

Commit

Permalink
Build both speed and size binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
Tehnix committed Nov 13, 2023
1 parent 730dfca commit a2900b2
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 11 deletions.
46 changes: 40 additions & 6 deletions .github/workflows/build-and-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ jobs:
# if-no-files-found: error
# retention-days: 1

build-directly-optimized-arm-graviton:
build-directly-optimized-arm-graviton-size:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -149,7 +149,7 @@ jobs:
project: 790k10m5qn
token: ${{ secrets.DEPOT_TOKEN }}
context: lambda-directly-optimized
file: lambda-directly-optimized/Dockerfile-arm-graviton
file: lambda-directly-optimized/Dockerfile-arm-graviton-size
load: true
tags: lambda
platforms: linux/arm64
Expand All @@ -164,7 +164,39 @@ jobs:
- uses: actions/upload-artifact@v3
with:
name: directly-optimized-graviton-arm
name: directly-optimized-graviton-arm-size
path: lambda-directly-optimized/arm/bootstrap
if-no-files-found: error
retention-days: 1

build-directly-optimized-arm-graviton-speed:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: depot/setup-action@v1

- name: Build Apollo Router Lambda
uses: depot/build-push-action@v1
with:
project: 790k10m5qn
token: ${{ secrets.DEPOT_TOKEN }}
context: lambda-directly-optimized
file: lambda-directly-optimized/Dockerfile-arm-graviton-speed
load: true
tags: lambda
platforms: linux/arm64

- name: Extract ARM bootstrap file
working-directory: lambda-directly-optimized
run: |
mkdir -p arm
export TMP_IMAGE_ID=$(docker create --platform linux/arm64 lambda)
docker cp $TMP_IMAGE_ID:/dist/apollo-router-lambda/target/lambda/apollo-router-lambda/bootstrap arm/bootstrap
docker rm -v $TMP_IMAGE_ID
- uses: actions/upload-artifact@v3
with:
name: directly-optimized-graviton-arm-speed
path: lambda-directly-optimized/arm/bootstrap
if-no-files-found: error
retention-days: 1
Expand Down Expand Up @@ -208,7 +240,8 @@ jobs:
build-directly,
# build-directly-optimized-x86,
# build-directly-optimized-arm,
build-directly-optimized-arm-graviton,
build-directly-optimized-arm-graviton-size,
build-directly-optimized-arm-graviton-speed,
]
steps:
- uses: actions/checkout@v4
Expand All @@ -227,7 +260,8 @@ jobs:
mv release-artifacts/directly-x86-64/bootstrap release-artifacts/bootstrap-directly-x86-64
# mv release-artifacts/directly-optimized-x86-64/bootstrap release-artifacts/bootstrap-directly-optimized-x86-64
# mv release-artifacts/directly-optimized-arm/bootstrap release-artifacts/bootstrap-directly-optimized-arm
mv release-artifacts/directly-optimized-graviton-arm/bootstrap release-artifacts/bootstrap-directly-optimized-graviton-arm
mv release-artifacts/directly-optimized-graviton-arm-size/bootstrap release-artifacts/bootstrap-directly-optimized-graviton-arm-size
mv release-artifacts/directly-optimized-graviton-arm-speed/bootstrap release-artifacts/bootstrap-directly-optimized-graviton-arm-speed
- name: Set release info
id: info
Expand All @@ -239,7 +273,7 @@ jobs:
with:
# NOTE: release-artifacts/bootstrap-directly-optimized-arm is not included here.
# NOTE: release-artifacts/bootstrap-directly-optimized-x86-64 is not included here.
artifacts: "release-artifacts/bootstrap-with-server-x86-64,release-artifacts/bootstrap-directly-x86-64,release-artifacts/bootstrap-directly-optimized-graviton-arm"
artifacts: "release-artifacts/bootstrap-with-server-x86-64,release-artifacts/bootstrap-directly-x86-64,release-artifacts/bootstrap-directly-optimized-graviton-arm-speed,release-artifacts/bootstrap-directly-optimized-graviton-arm-size"
# Automatically create the tag from the commit if it doesn't exist.
tag: ${{ steps.info.outputs.tag-name }}
commit: ${{ github.sha }}
Expand Down
11 changes: 10 additions & 1 deletion lambda-directly-optimized/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,17 @@ codegen-units = 1 # Reduce Parallel Code Generation Units to Increase Optimizati
strip = true # Automatically strip symbols from the binary.
debug = false

# Use the `--profile release-speed` flag to optimize for speed in release mode. E.g.
# `cargo build --profile release-speed`.
[profile.release-speed]
inherits = "release"
# Optimize for speed.
# opt-level = 3
opt-level = 3

# Use the `--profile release-size` flag to optimize for size in release mode. E.g.
# `cargo build --profile release-size`.
[profile.release-size]
inherits = "release"
# Optimize for size.
opt-level = "z"
panic = "abort"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ RUN export PATH="$HOME/.cargo/bin:$PATH"; \
export AWS_LAMBDA_FUNCTION_VERSION=1; \
export AWS_LAMBDA_FUNCTION_MEMORY_SIZE=1024; \
export AWS_LAMBDA_RUNTIME_API=http://[::]:9000/.rt; \
cargo lambda build --compiler cargo -Z build-std=std,panic_abort --target $TARGET --release; \
cargo lambda build --compiler cargo -Z build-std=std,panic_abort --target $TARGET --profile release-size; \
rm -r src; \
# Delete the previously generated build artifacts.
rm -r target/$TARGET/release/deps/apollo_router_lambda*; \
Expand All @@ -54,4 +54,4 @@ COPY src/main.rs /dist/apollo-router-lambda/src/main.rs
# Build our lambda bootstrap binary. The release artifact can be found at:
# /dist/apollo-router-lambda/target/lambda/apollo-router-lambda/bootstrap
RUN export PATH="$HOME/.cargo/bin:$PATH"; \
cargo lambda build --compiler cargo -Z build-std=std,panic_abort -Z build-std-features=panic_immediate_abort --target $TARGET --release
cargo lambda build --compiler cargo -Z build-std=std,panic_abort -Z build-std-features=panic_immediate_abort --target $TARGET --profile release-size
57 changes: 57 additions & 0 deletions lambda-directly-optimized/Dockerfile-arm-graviton-speed
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
FROM public.ecr.aws/lambda/provided:al2-arm64 as builder
# We need nightly to be able to use build-std. Alternatively, use 1.72.0.
ARG RUST_VERSION=nightly-2023-11-12
ARG TARGET=aarch64-unknown-linux-gnu

RUN yum install -y jq openssl-devel gcc wget tar gzip unzip \
&& yum group install -y "Development Tools"

# Install a recent cmake.
RUN wget https://github.com/Kitware/CMake/releases/download/v3.5.1/cmake-3.5.1.tar.gz \
&& tar -xvf cmake-3.5.1.tar.gz \
&& cd cmake-3.5.1 \
&& ./bootstrap \
&& make \
&& make install

# Install profobuf (used by apollo-router).
RUN curl -LO https://github.com/protocolbuffers/protobuf/releases/download/v3.15.8/protoc-3.15.8-linux-aarch_64.zip \
&& unzip protoc-3.15.8-linux-aarch_64.zip -d .\
# Protobuf expects its "include" directory to be in the same directory as the binary.
&& mv bin/protoc /usr/local/bin/protoc \
&& mv include /usr/local/bin/include

RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs \
| sh -s -- -y --profile minimal --default-toolchain $RUST_VERSION; \
export PATH="$HOME/.cargo/bin:$PATH"; rustup component add rust-src --toolchain $RUST_VERSION

# Fetch the cargo-lambda binary.
RUN curl -LO https://github.com/cargo-lambda/cargo-lambda/releases/download/v0.22.0/cargo-lambda-v0.22.0.aarch64-unknown-linux-musl.tar.gz \
&& tar -xvf cargo-lambda-v0.22.0.aarch64-unknown-linux-musl.tar.gz \
&& chmod +x cargo-lambda \
&& mv cargo-lambda $HOME/.cargo/bin/cargo-lambda

WORKDIR /dist/apollo-router-lambda

# Build all of our dependencies.
COPY Cargo.toml /dist/apollo-router-lambda/Cargo.toml
COPY Cargo.lock /dist/apollo-router-lambda/Cargo.lock
COPY scaffold.rs /dist/apollo-router-lambda/src/main.rs
RUN export PATH="$HOME/.cargo/bin:$PATH"; \
export AWS_LAMBDA_FUNCTION_NAME="_"; \
export AWS_LAMBDA_FUNCTION_VERSION=1; \
export AWS_LAMBDA_FUNCTION_MEMORY_SIZE=1024; \
export AWS_LAMBDA_RUNTIME_API=http://[::]:9000/.rt; \
cargo lambda build --compiler cargo -Z build-std=std --target $TARGET --profile release-speed; \
rm -r src; \
# Delete the previously generated build artifacts.
rm -r target/$TARGET/release/deps/apollo_router_lambda*; \
rm target/lambda/apollo-router-lambda/bootstrap

# Copy our actual application code and build this.
COPY src/main.rs /dist/apollo-router-lambda/src/main.rs

# Build our lambda bootstrap binary. The release artifact can be found at:
# /dist/apollo-router-lambda/target/lambda/apollo-router-lambda/bootstrap
RUN export PATH="$HOME/.cargo/bin:$PATH"; \
cargo lambda build --compiler cargo -Z build-std=std --target $TARGET --profile release-speed
4 changes: 2 additions & 2 deletions lambda-directly-optimized/Dockerfile-x86
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ RUN export PATH="$HOME/.cargo/bin:$PATH"; \
export AWS_LAMBDA_FUNCTION_VERSION=1; \
export AWS_LAMBDA_FUNCTION_MEMORY_SIZE=1024; \
export AWS_LAMBDA_RUNTIME_API=http://[::]:9000/.rt; \
cargo lambda build --compiler cargo -Z build-std=std --target $TARGET --release; \
cargo lambda build --compiler cargo -Z build-std=std --target $TARGET --profile release-speed; \
rm -r src; \
# Delete the previously generated main.rs artifacts.
rm -r target/$TARGET/release/deps/apollo_router_lambda*; \
Expand All @@ -54,4 +54,4 @@ COPY src/main.rs /dist/apollo-router-lambda/src/main.rs
# Build our lambda bootstrap binary. The release artifact can be found at:
# /dist/apollo-router-lambda/target/lambda/apollo-router-lambda/bootstrap
RUN export PATH="$HOME/.cargo/bin:$PATH"; \
cargo lambda build --compiler cargo -Z build-std=std,panic_abort -Z build-std-features=panic_immediate_abort --target $TARGET --release
cargo lambda build --compiler cargo -Z build-std=std,panic_abort -Z build-std-features=panic_immediate_abort --target $TARGET --profile release-speed

0 comments on commit a2900b2

Please sign in to comment.