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..."); -}