From aaa04b2d078b0f63973083cd0e248f2d3a30e49b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Kj=C3=A6r?= Date: Mon, 13 Nov 2023 10:48:53 +0100 Subject: [PATCH] Optimize for size --- lambda-directly-optimized/.cargo/config.toml | 19 +++++++++++++++++++ lambda-directly-optimized/.dockerignore | 1 + lambda-directly-optimized/Cargo.toml | 7 ++++++- lambda-directly-optimized/Dockerfile-arm | 10 ++++++---- .../Dockerfile-arm-graviton | 12 ++++-------- lambda-directly-optimized/Dockerfile-x86 | 8 +++----- lambda-directly/Dockerfile | 4 ++-- lambda-with-server/Dockerfile | 4 ++-- 8 files changed, 43 insertions(+), 22 deletions(-) create mode 100644 lambda-directly-optimized/.cargo/config.toml diff --git a/lambda-directly-optimized/.cargo/config.toml b/lambda-directly-optimized/.cargo/config.toml new file mode 100644 index 0000000..ab6d5e6 --- /dev/null +++ b/lambda-directly-optimized/.cargo/config.toml @@ -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", +] diff --git a/lambda-directly-optimized/.dockerignore b/lambda-directly-optimized/.dockerignore index b8f58e3..6e3dba8 100644 --- a/lambda-directly-optimized/.dockerignore +++ b/lambda-directly-optimized/.dockerignore @@ -5,3 +5,4 @@ !scaffold.rs !router.yaml !supergraph.graphql +!.cargo/config.toml diff --git a/lambda-directly-optimized/Cargo.toml b/lambda-directly-optimized/Cargo.toml index 1a1c194..bece0ac 100644 --- a/lambda-directly-optimized/Cargo.toml +++ b/lambda-directly-optimized/Cargo.toml @@ -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" diff --git a/lambda-directly-optimized/Dockerfile-arm b/lambda-directly-optimized/Dockerfile-arm index ddd4276..ffadb0e 100644 --- a/lambda-directly-optimized/Dockerfile-arm +++ b/lambda-directly-optimized/Dockerfile-arm @@ -37,7 +37,12 @@ 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 @@ -45,7 +50,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 diff --git a/lambda-directly-optimized/Dockerfile-arm-graviton b/lambda-directly-optimized/Dockerfile-arm-graviton index 57b37c5..b57bbac 100644 --- a/lambda-directly-optimized/Dockerfile-arm-graviton +++ b/lambda-directly-optimized/Dockerfile-arm-graviton @@ -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="-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. @@ -52,10 +53,5 @@ 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 diff --git a/lambda-directly-optimized/Dockerfile-x86 b/lambda-directly-optimized/Dockerfile-x86 index 224cb17..9fe4618 100644 --- a/lambda-directly-optimized/Dockerfile-x86 +++ b/lambda-directly-optimized/Dockerfile-x86 @@ -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. @@ -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 diff --git a/lambda-directly/Dockerfile b/lambda-directly/Dockerfile index 36fd3c1..b72f218 100644 --- a/lambda-directly/Dockerfile +++ b/lambda-directly/Dockerfile @@ -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. @@ -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 diff --git a/lambda-with-server/Dockerfile b/lambda-with-server/Dockerfile index 36fd3c1..b72f218 100644 --- a/lambda-with-server/Dockerfile +++ b/lambda-with-server/Dockerfile @@ -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. @@ -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