Skip to content

Commit

Permalink
Optimize for size
Browse files Browse the repository at this point in the history
  • Loading branch information
Tehnix committed Nov 13, 2023
1 parent 14485a2 commit aaa04b2
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 22 deletions.
19 changes: 19 additions & 0 deletions lambda-directly-optimized/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[target.x86_64-unknown-linux-gnu]
rustflags = [
# Remove location details from panics to reduce size.
"-Zlocation-detail=none",
# Enable parallel compiler Frontend for compilation speedup.
"-Zthreads=8",
]

[target.aarch64-unknown-linux-gnu]
rustflags = [
# Optimize for the ARM/Graviton2 CPU architecture to improve performance. See more at
# https://github.com/aws/aws-graviton-getting-started/blob/main/rust.md.
"-Ctarget-feature=+lse",
"-Ctarget-cpu=neoverse-n1",
# Remove location details from panics to reduce size.
"-Zlocation-detail=none",
# Enable parallel compiler Frontend for compilation speedup.
"-Zthreads=8",
]
1 change: 1 addition & 0 deletions lambda-directly-optimized/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
!scaffold.rs
!router.yaml
!supergraph.graphql
!.cargo/config.toml
7 changes: 6 additions & 1 deletion lambda-directly-optimized/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ license = "MIT OR Apache-2.0"
# - https://doc.rust-lang.org/cargo/reference/profiles.html#default-profiles
# - https://github.com/johnthagen/min-sized-rust
[profile.release]
opt-level = 3 # Optimize for speed.
lto = true # Enable Link Time Optimization
codegen-units = 1 # Reduce Parallel Code Generation Units to Increase Optimization.
strip = true # Automatically strip symbols from the binary.
debug = false

# Optimize for speed.
# opt-level = 3
# Optimize for size.
opt-level = "z"
panic = "abort"

[dependencies]
# The Apollo Router.
apollo-router = "1.33.1"
Expand Down
10 changes: 6 additions & 4 deletions lambda-directly-optimized/Dockerfile-arm
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,17 @@ WORKDIR /dist/apollo-router-lambda
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 RUSTFLAGS="-Zlocation-detail=none -Zthreads=8"; cargo lambda build --compiler cargo -Z build-std=std --target $TARGET --release; rm -r src; target/lambda/apollo-router-lambda/bootstrap
RUN export PATH="$HOME/.cargo/bin:$PATH"; \
cargo lambda build --compiler cargo -Z build-std=std --target $TARGET --release; \
rm -r src; \
# Delete the previously generated main.rs artifacts.
rm -r target/$TARGET/release/deps/apollo_router_lambda*; \
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"; \
export RUSTFLAGS="-Zlocation-detail=none -Zthreads=8"; \
# Delete the previously generated main.rs artifacts.
rm -r target/$TARGET/release/deps/apollo_router_lambda*; \
cargo lambda build --compiler cargo -Z build-std=std --target $TARGET --release
12 changes: 4 additions & 8 deletions lambda-directly-optimized/Dockerfile-arm-graviton
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,20 @@ 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 RUSTFLAGS="-Ctarget-feature=+lse -Ctarget-cpu=neoverse-n1 -Zlocation-detail=none -Zthreads=8"; \
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 --release; \
cargo lambda build --compiler cargo -Z build-std=std,panic_abort --target $TARGET --release; \
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
# NOTE: We add additional ARM/Graviton2 specific flags to the build process. See more at
# https://github.com/aws/aws-graviton-getting-started/blob/main/rust.md.
RUN export PATH="$HOME/.cargo/bin:$PATH"; \
export RUSTFLAGS="-Ctarget-feature=+lse -Ctarget-cpu=neoverse-n1 -Zlocation-detail=none -Zthreads=8"; \
# Delete the previously generated main.rs artifacts.
rm -r target/$TARGET/release/deps/apollo_router_lambda*; \
cargo lambda build --compiler cargo -Z build-std=std --target $TARGET --release
cargo lambda build --compiler cargo -Z build-std=std,panic_abort -Z build-std-features=panic_immediate_abort --target $TARGET --release
8 changes: 3 additions & 5 deletions lambda-directly-optimized/Dockerfile-x86
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ 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 RUSTFLAGS="-Zlocation-detail=none -Zthreads=8"; \
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 --release; \
rm -r src; \
# Delete the previously generated main.rs 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.
Expand All @@ -53,7 +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"; \
export RUSTFLAGS="-Zlocation-detail=none -Zthreads=8"; \
# Delete the previously generated main.rs artifacts.
rm -r target/$TARGET/release/deps/apollo_router_lambda*; \
cargo lambda build --compiler cargo -Z build-std=std --target $TARGET --release
cargo lambda build --compiler cargo -Z build-std=std -Z build-std-features=panic_immediate_abort --target $TARGET --release
4 changes: 2 additions & 2 deletions lambda-directly/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ RUN export PATH="$HOME/.cargo/bin:$PATH"; \
export AWS_LAMBDA_RUNTIME_API=http://[::]:9000/.rt; \
cargo lambda build --compiler cargo --release; \
rm -r src; \
# Delete the previously generated main.rs 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.
Expand All @@ -49,6 +51,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"; \
# Delete the previously generated main.rs artifacts.
rm -r target/x86_64-unknown-linux-gnu/release/deps/apollo_router_lambda*; \
cargo lambda build --compiler cargo --release
4 changes: 2 additions & 2 deletions lambda-with-server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ RUN export PATH="$HOME/.cargo/bin:$PATH"; \
export AWS_LAMBDA_RUNTIME_API=http://[::]:9000/.rt; \
cargo lambda build --compiler cargo --release; \
rm -r src; \
# Delete the previously generated main.rs 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.
Expand All @@ -49,6 +51,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"; \
# Delete the previously generated main.rs artifacts.
rm -r target/x86_64-unknown-linux-gnu/release/deps/apollo_router_lambda*; \
cargo lambda build --compiler cargo --release

0 comments on commit aaa04b2

Please sign in to comment.