-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an example showing Eyra's small code size
Add an example showing the use of [min-sized-rust] techiques to reduce code size. As of this writing, using the same optimizations without Eyra, using `x86_64-unknown-linux-musl`, produces a hello world binary in 50776 bytes, while using them with Eyra uses just 37912 bytes. [min-sized-rust]: https://github.com/johnthagen/min-sized-rust
- Loading branch information
1 parent
1794164
commit 4c730f0
Showing
7 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/target | ||
Cargo.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "hello-world-small" | ||
version = "0.0.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] | ||
eyra = { path = "../.." } | ||
|
||
[profile.release] | ||
# Enable options from min-sized-rust: | ||
strip = true # Automatically strip symbols from the binary. | ||
opt-level = "z" # Optimize for size. | ||
lto = true | ||
codegen-units = 1 | ||
panic = "abort" | ||
|
||
# This is just an example crate, and not part of the c-ward workspace. | ||
[workspace] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
This crate demonstrates the use of Eyra with a smaller binary size! | ||
|
||
This is the same as the [hello-world] example, but enables some options | ||
described in [min-sized-rust] to reduce the size of the final binary. | ||
|
||
It uses the [workaround to support -Zbuild-std], and can be built with | ||
a command like this: | ||
|
||
```console | ||
$ RUSTFLAGS="-Zlocation-detail=none -C relocation-model=static -Ctarget-feature=+crt-static" cargo +nightly run -Z build-std=std,panic_abort -Z build-std-features=panic_immediate_abort --target x86_64-unknown-linux-gnu --release | ||
``` | ||
|
||
This applies all the techniques described on the [min-sized-rust] page | ||
before [Remove `core::fmt` with `#![no_main]` and Careful Usage of `libstd`]. | ||
|
||
As of this writing, using all these same optimizations without Eyra, using | ||
`x86_64-unknown-linux-musl` (which produces smaller statically-linked binaries | ||
than `x86_64-unknown-linux-gnu`), compiles to 50776 bytes, while this Eyra | ||
example currently compiles to 37912 bytes. | ||
|
||
If you're interested in going further down the `#![no_main]`/`#![no_std]` | ||
path, consider [using Origin directly] which can get down to 408 bytes. Or, | ||
consider using [Origin Studio] if you want to go there but still have | ||
`println!`. | ||
|
||
[hello-world]: https://github.com/sunfishcode/eyra/tree/main/example-crates/hello-world/ | ||
[min-sized-rust]: https://github.com/johnthagen/min-sized-rust | ||
[workaround to support -Zbuild-std]: https://github.com/sunfishcode/eyra/blob/main/README.md#compatibility-with--zbuild-std | ||
[Remove `core::fmt` with `#![no_main]` and Careful Usage of `libstd`]: https://github.com/johnthagen/min-sized-rust#remove-corefmt-with-no_main-and-careful-usage-of-libstd | ||
[using Origin directly]: https://github.com/sunfishcode/origin/tree/main/example-crates/tiny | ||
[Origin Studio]: https://github.com/sunfishcode/origin-studio |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
fn main() { | ||
// Pass -nostartfiles to the linker. | ||
println!("cargo:rustc-link-arg=-nostartfiles"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
extern crate eyra; | ||
|
||
fn main() { | ||
println!("Hello, world!"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters