-
Notifications
You must be signed in to change notification settings - Fork 0
ビルド時間の短縮 #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
ビルド時間の短縮 #21
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
ビルド時間短縮を目的に、Rustビルド設定とDockerビルド手順を最適化するPRです。
Changes:
.cargo/config.tomlでmuslターゲットのリンカーをclangに固定し、moldを確実に利用- 不要な依存関係の削除と
sea-ormの不要feature削除、ビルドプロファイル最適化(release/dev) - Dockerビルドを依存関係先行ビルド+BuildKitキャッシュで高速化(ただし現状キャッシュを壊す行あり)
Reviewed changes
Copilot reviewed 3 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| Dockerfile | 依存関係を先にビルドしてDockerキャッシュ活用、BuildKit cache mount導入 |
| Cargo.toml | 不要依存の削除、sea-orm feature整理、profile最適化追加 |
| Cargo.lock | 依存削除に伴うロック更新 |
| .cargo/config.toml | musl向けにclangリンカー指定を追加してmold利用を安定化 |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| FROM alpine:latest | ||
| COPY . . | ||
| RUN touch src/main.rs |
Copilot
AI
Jan 31, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RUN touch src/main.rs updates the file timestamp on every build, which makes this layer (and everything after it) effectively uncacheable and reduces build reproducibility. Removing this line (or replacing it with a deterministic cache-busting strategy only when needed) will preserve Docker layer caching and speed up builds.
| RUN touch src/main.rs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これわざと
| RUN rm -rf src | ||
|
|
||
| FROM alpine:latest | ||
| COPY . . |
Copilot
AI
Jan 31, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This COPY . . will send the entire build context into the image. Since the repo has no .dockerignore, it will also include directories like .git/ (and potentially target/ after local builds), which slows builds and can invalidate cache unexpectedly. Consider adding a .dockerignore (or narrowing what gets copied) to keep the build context small and caching effective.
| COPY . . | |
| COPY src ./src |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これはぐうの音も出ないから対応した
| --mount=type=cache,target=/root/.cargo/git \ | ||
| cargo build --release -j $(nproc) | ||
|
|
||
| FROM alpine:latest |
Copilot
AI
Jan 31, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The runtime stage uses FROM alpine:latest, which pins your base image to a mutable latest tag on Docker Hub; if that tag is ever compromised or updated with a malicious or vulnerable image, every build using this Dockerfile can silently inherit the compromise. To make builds deterministic and reduce supply-chain risk, pin the base image to an immutable reference such as a specific version tag or image digest under your control.
Overview
・リンカーが書かれてはいたけど使われてなかったため修正(.cargo/config.toml)
・不要な依存関係の削除(Cargo.toml、Cargo.lock)
・最適化設定の変更(Cargo.toml)
・Dockerのキャッシュ設定や不要なcargo fixの削除(Dockerfile)
Changes
.cargo/config.toml
linkerをclangに明示的に指定することでmoldが正しく使われるようにした。
Cargo.toml,Cargo.lock
これらのクレートを削除。
Dockerfile
わざと
src/main.rsに空のfn main() {}を作り、先にCargo.tomlだけを使ってビルドするようにした。これによりライブラリだけのビルド結果をDockerキャッシュに保存でき、自分のコードを書き替えてもライブラリの再ビルドはスキップできるようにした。
cargo build -j $(nproc)にすることで明示的にすべてのコアを使って並列ビルドできるようにした。不要な
cargo fixを削ることによりビルド時間を短縮した。Optimization Settings
Link Time Optimizationの設定をthinにすることで最適化をしながらビルド時間を伸ばさないようにした。ltoをthinにするとうまくDockerのキャッシュが効かないのでfalseにした。
codegen-unitsを16にすることで並列コンパイルをするようにした。opt-levelを2にすることで実行速度を十分に確保しつつ、ビルド時間も伸びすぎないようにした。最適化を一切せず、デバッグ情報も生成しないようにした。
これによりバイナリサイズが劇的に小さくなりビルド後の書き込み時間を短縮した。