Skip to content

Commit 61a82ff

Browse files
committed
transaction/fee: don't use fmt with "{.1}" to reduce binary size
Using this pulls in `core::fmt::float::float_to_decimal_common_shortest` into the binary, which is a huge function. By changing the impl to something simpler, we save 20112 bytes in the binary :O We also add a CI check to avoid re-introducing this function in the future by accident.
1 parent 13e4c1e commit 61a82ff

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

.ci/ci

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ make -j8 factory-setup
5353
make -j8 bootloader-semihosting
5454
make -j8 firmware-semihosting
5555

56+
# Disallow some symbols in the final binary that we don't want.
57+
if arm-none-eabi-nm build/bin/firmware.elf | grep -q "float_to_decimal_common_shortest"; then
58+
echo "Rust fmt float formatting like {.1} adds significant binary bloat."
59+
echo "Use something simpler like (float*10).round() as u64, then format with util::decimal::format"
60+
exit 1
61+
fi
62+
5663
(cd tools/atecc608; go test ./...)
5764

5865
# Don't generate graphics in CI

src/rust/bitbox02-rust/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ sha3 = { workspace = true, optional = true }
3838
digest = "0.10.6"
3939
zeroize = { workspace = true }
4040
num-bigint = { workspace = true, optional = true }
41-
num-traits = { version = "0.2", default-features = false, optional = true }
41+
num-traits = { version = "0.2", default-features = false }
4242
# If you change this, also change src/rust/.cargo/config.toml.
4343
bip32-ed25519 = { git = "https://github.com/digitalbitbox/rust-bip32-ed25519", tag = "v0.1.2", optional = true }
4444
bech32 = { version = "0.11.0", default-features = false, features = ["alloc"], optional = true }
@@ -74,7 +74,6 @@ app-ethereum = [
7474
"erc20_params",
7575
"sha3",
7676
"num-bigint",
77-
"num-traits",
7877
# enable this feature in the deps
7978
"bitbox02/app-ethereum",
8079
]

src/rust/bitbox02-rust/src/workflow/transaction.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ pub async fn verify_recipient(recipient: &str, amount: &str) -> Result<(), UserA
3535
}
3636

3737
fn format_percentage(p: f64) -> String {
38-
format!("{:.1}", p)
38+
let int: u64 = num_traits::float::FloatCore::round(p * 10.) as _;
39+
util::decimal::format_no_trim(int, 1)
3940
}
4041

4142
pub async fn verify_total_fee(

0 commit comments

Comments
 (0)