Skip to content

Commit 21b67b4

Browse files
Merge pull request #208 from Tcode-Motion/bolt-perf-hex-encode-17363716070361557883
⚡ Bolt: Replace map format hex encoding with hex::encode
2 parents ceee72b + 06e48e2 commit 21b67b4

4 files changed

Lines changed: 9 additions & 9 deletions

File tree

‎.jules/bolt.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@
3535
## 2024-05-24 - Canvas Property Extraction Optimization
3636
**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.
3737
**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.
38+
## 2024-11-20 - Avoid `format!` inside loops for hex string encoding
39+
**Learning:** Using `.iter().map(|b| format!("{:02x}", b)).collect::<String>()` 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.
40+
**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.

‎stdlib/src/crypto.rs‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,8 @@ impl StdlibRegistry {
6161
combined.extend_from_slice(&ciphertext);
6262

6363
// Hex encode ciphertext
64-
let hex_ciphertext = combined
65-
.iter()
66-
.map(|b| format!("{:02x}", b))
67-
.collect::<String>();
64+
// ⚡ Bolt Performance Optimization: Replaced iterative format! with hex::encode for single-allocation hex string conversion.
65+
let hex_ciphertext = hex::encode(combined);
6866
Ok(RuntimeValue::Str(hex_ciphertext))
6967
},
7068
}),

‎stdlib/src/security.rs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ impl StdlibRegistry {
1818
use rand::Rng;
1919
let bytes: Vec<u8> =
2020
rand::thread_rng().gen::<[u8; 32]>()[..len.min(32)].to_vec();
21-
let hex: String = bytes.iter().map(|b| format!("{:02x}", b)).collect();
21+
// ⚡ Bolt Performance Optimization: Replaced iterative format! with hex::encode for single-allocation hex string conversion.
22+
let hex = hex::encode(bytes);
2223
Ok(RuntimeValue::Str(hex))
2324
},
2425
}),
@@ -48,7 +49,8 @@ impl StdlibRegistry {
4849
let input = args[0].to_string();
4950
use sha2::Digest;
5051
let digest = sha2::Sha256::digest(input.as_bytes());
51-
let hex: String = digest.iter().map(|b| format!("{:02x}", b)).collect();
52+
// ⚡ Bolt Performance Optimization: Replaced iterative format! with hex::encode for single-allocation hex string conversion.
53+
let hex = hex::encode(digest);
5254
Ok(RuntimeValue::Str(hex))
5355
},
5456
}),

‎test_perf.rs‎

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)