Skip to content
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

Enable LTO in Rust release profile #100

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

ruokun-niu
Copy link
Contributor

@ruokun-niu ruokun-niu commented Oct 25, 2024

Description

Enabling Link Time Optimization (LTO) will reduce the binary size. By setting lto to be true in the release profile, LTO will only be enabled in our release build, as we use the command cargo install in our dockerfiles. When we do a local cargo build, LTO will not be enabled. To enable LTO in cargo build, run with the --release flag

Type of change

  • This pull request is a minor refactor, code cleanup, test improvement, or other maintenance task and doesn't change the functionality of Drasi (issue link optional).

Fixes: #97

@ruokun-niu ruokun-niu requested a review from a team as a code owner October 25, 2024 18:57
@danielgerlag
Copy link
Contributor

What about the development workflow where you need to build the docker image and load it to kind for local testing?
I think we need a flag on the make command?

@ruokun-niu
Copy link
Contributor Author

What about the development workflow where you need to build the docker image and load it to kind for local testing? I think we need a flag on the make command?

Our makefile uses docker to build the image, which should use cargo install as well. When we run cargo install, the release profile will be used

@danielgerlag
Copy link
Contributor

What about the development workflow where you need to build the docker image and load it to kind for local testing? I think we need a flag on the make command?

Our makefile uses docker to build the image, which should use cargo install as well. When we run cargo install, the release profile will be used

Can we have a flag or ENV variable to control this, so that we can build the docker images without LTO by default?

@ruokun-niu
Copy link
Contributor Author

What about the development workflow where you need to build the docker image and load it to kind for local testing? I think we need a flag on the make command?

Our makefile uses docker to build the image, which should use cargo install as well. When we run cargo install, the release profile will be used

Can we have a flag or ENV variable to control this, so that we can build the docker images without LTO by default?

I'll look into it

@ruokun-niu
Copy link
Contributor Author

What about the development workflow where you need to build the docker image and load it to kind for local testing? I think we need a flag on the make command?

Our makefile uses docker to build the image, which should use cargo install as well. When we run cargo install, the release profile will be used

Can we have a flag or ENV variable to control this, so that we can build the docker images without LTO by default?

I have updated the makefiles and added the following section to the contributing-code-building readme:

By default, all our Rust-based images use the release profile in builds, which sets Link-time Optimization (LTO) to true. For more information on Rust profiles and the use of LTO in Rust projects, see Profiles. If you wish to use the default LTO setting, you can use the CARGO_DEBUG parameter as follows:

make docker-build CARGO_DEBUG=1

@danielgerlag
Copy link
Contributor

What about the development workflow where you need to build the docker image and load it to kind for local testing? I think we need a flag on the make command?

Our makefile uses docker to build the image, which should use cargo install as well. When we run cargo install, the release profile will be used

Can we have a flag or ENV variable to control this, so that we can build the docker images without LTO by default?

I have updated the makefiles and added the following section to the contributing-code-building readme:

By default, all our Rust-based images use the release profile in builds, which sets Link-time Optimization (LTO) to true. For more information on Rust profiles and the use of LTO in Rust projects, see Profiles. If you wish to use the default LTO setting, you can use the CARGO_DEBUG parameter as follows:

make docker-build CARGO_DEBUG=1

Great, I think the default should be without LTO, so when you want to cut a release build it would be something like

make docker-build RELEASE=1

@ruokun-niu
Copy link
Contributor Author

What about the development workflow where you need to build the docker image and load it to kind for local testing? I think we need a flag on the make command?

Our makefile uses docker to build the image, which should use cargo install as well. When we run cargo install, the release profile will be used

Can we have a flag or ENV variable to control this, so that we can build the docker images without LTO by default?

I have updated the makefiles and added the following section to the contributing-code-building readme:
By default, all our Rust-based images use the release profile in builds, which sets Link-time Optimization (LTO) to true. For more information on Rust profiles and the use of LTO in Rust projects, see Profiles. If you wish to use the default LTO setting, you can use the CARGO_DEBUG parameter as follows:

make docker-build CARGO_DEBUG=1

Great, I think the default should be without LTO, so when you want to cut a release build it would be something like

make docker-build RELEASE=1

Good point. Will configure this after I fix the build-test workflow

# See the License for the specific language governing permissions and
# limitations under the License.

ARG RELEASE=1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make the default 0

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make the default 0


default: docker-build

docker-build:
docker buildx build .. -f ./Dockerfile -t $(IMAGE_PREFIX)/kubernetes-provider:$(DOCKER_TAG_VERSION) $(DOCKERX_OPTS)
docker buildx build .. --build-arg RELEASE=$(RELEASE) -f ./Dockerfile -t $(IMAGE_PREFIX)/kubernetes-provider:$(DOCKER_TAG_VERSION) $(DOCKERX_OPTS)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we just include this arg in the $(DOCKERX_OPTS) string?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then you would not need to modify the make files at all, you could just pass it as a parameter

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we just include this arg in the $(DOCKERX_OPTS) string?
Then you would not need to modify the make files at all, you could just pass it as a parameter


default: docker-build

docker-build:
docker buildx build .. -f ./Dockerfile -t $(IMAGE_PREFIX)/kubernetes-provider:$(DOCKER_TAG_VERSION) $(DOCKERX_OPTS)
docker buildx build .. --build-arg RELEASE=$(RELEASE) -f ./Dockerfile -t $(IMAGE_PREFIX)/kubernetes-provider:$(DOCKER_TAG_VERSION) $(DOCKERX_OPTS)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we just include this arg in the $(DOCKERX_OPTS) string?
Then you would not need to modify the make files at all, you could just pass it as a parameter

# See the License for the specific language governing permissions and
# limitations under the License.

ARG RELEASE=1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make the default 0

By default, all our Rust-based images use the `release` profile in builds, which sets Link-time Optimization (LTO) to true. For more information on Rust profiles and the use of LTO in Rust projects, see [Profiles](https://doc.rust-lang.org/cargo/reference/profiles.html#lto). If you wish to use the default LTO setting, you can use the `CARGO_DEBUG` parameter as follows:

```sh
make docker-build CARGO_DEBUG=1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still accurate?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enable Link-Time Optimization (LTO) for the Rust part
4 participants