From 06e48e212ec0e9dd4c5ff8803111172e7ed2f86d Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 23 Sep 2026 05:07:39 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Replace=20iterative=20hex?= =?UTF-8?q?=20formatting=20with=20single-allocation=20hex::encode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Tcode-Motion <188012755+Tcode-Motion@users.noreply.github.com> --- .jules/bolt.md | 3 +++ stdlib/src/crypto.rs | 6 ++---- stdlib/src/security.rs | 6 ++++-- test_perf.rs | 3 --- 4 files changed, 9 insertions(+), 9 deletions(-) delete mode 100644 test_perf.rs diff --git a/.jules/bolt.md b/.jules/bolt.md index ff826971..362f27c6 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -35,3 +35,6 @@ ## 2024-05-24 - Canvas Property Extraction Optimization **Learning:** Extracting properties from the `RuntimeValue` DSL property lists in `canvas.rs` was inefficient because the code called `.iter().find()` multiple times for each property in `logo`, `rings`, `emblem`, `letter`, `core`, and `circuits` definitions. **Action:** Replace multiple `.find()` calls with a single-pass `for p in &dsl.properties` loop and `match` on the property names, mutating local fallback variables. +## 2024-11-20 - Avoid `format!` inside loops for hex string encoding +**Learning:** Using `.iter().map(|b| format!("{:02x}", b)).collect::()` to encode a byte array to a hex string incurs a heavy performance penalty because it allocates a new temporary `String` for every single byte processed before concatenating them. The `hex::encode()` function from the `hex` crate performs this conversion directly into a single pre-allocated `String` with zero intermediate allocations. +**Action:** Always prefer `hex::encode(bytes)` over iterative `format!` mapping when performing hexadecimal encoding of byte arrays or slices to eliminate intermediate string allocations and significantly boost performance. diff --git a/stdlib/src/crypto.rs b/stdlib/src/crypto.rs index a7b97e60..54a9cd19 100644 --- a/stdlib/src/crypto.rs +++ b/stdlib/src/crypto.rs @@ -61,10 +61,8 @@ impl StdlibRegistry { combined.extend_from_slice(&ciphertext); // Hex encode ciphertext - let hex_ciphertext = combined - .iter() - .map(|b| format!("{:02x}", b)) - .collect::(); + // ⚡ Bolt Performance Optimization: Replaced iterative format! with hex::encode for single-allocation hex string conversion. + let hex_ciphertext = hex::encode(combined); Ok(RuntimeValue::Str(hex_ciphertext)) }, }), diff --git a/stdlib/src/security.rs b/stdlib/src/security.rs index c19542b1..e92faab6 100644 --- a/stdlib/src/security.rs +++ b/stdlib/src/security.rs @@ -18,7 +18,8 @@ impl StdlibRegistry { use rand::Rng; let bytes: Vec = rand::thread_rng().gen::<[u8; 32]>()[..len.min(32)].to_vec(); - let hex: String = bytes.iter().map(|b| format!("{:02x}", b)).collect(); + // ⚡ Bolt Performance Optimization: Replaced iterative format! with hex::encode for single-allocation hex string conversion. + let hex = hex::encode(bytes); Ok(RuntimeValue::Str(hex)) }, }), @@ -48,7 +49,8 @@ impl StdlibRegistry { let input = args[0].to_string(); use sha2::Digest; let digest = sha2::Sha256::digest(input.as_bytes()); - let hex: String = digest.iter().map(|b| format!("{:02x}", b)).collect(); + // ⚡ Bolt Performance Optimization: Replaced iterative format! with hex::encode for single-allocation hex string conversion. + let hex = hex::encode(digest); Ok(RuntimeValue::Str(hex)) }, }), diff --git a/test_perf.rs b/test_perf.rs deleted file mode 100644 index 8093e40b..00000000 --- a/test_perf.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - println!("Testing performance..."); -}