consensus_encoding: Adjust ConsensusHex impl in serde_as_consensus
What changed, and why it matters
This is a routine performance refactor. It swaps a slow, character-by-character hex formatting loop for one that writes whole chunks at a time. There is no security-relevant change.
No security action needed. Treat as a normal performance improvement.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit changes the ConsensusHex Display implementation in serde_as_consensus.rs to use DisplayHex::as_hex() on encoder.current_chunk() and encoder.advance() rather than flattening an EncoderByteIter into individual hex characters. The diff is purely an optimization: the same data is encoded and serialized with the same lowercase hex case. No input validation, error handling, bounds checking, or cryptographic logic is altered.
Changed components
consensus_encoding/src/serde_as_consensus.rsInspect captured patch +8 / −9
diff --git a/consensus_encoding/src/serde_as_consensus.rs b/consensus_encoding/src/serde_as_consensus.rs
index 6dccfc52..da6fb77c 100644
--- a/consensus_encoding/src/serde_as_consensus.rs
+++ b/consensus_encoding/src/serde_as_consensus.rs
@@ -15,9 +15,10 @@
use core::fmt;
use core::marker::PhantomData;
+use hex::DisplayHex as _;
use serde::{de, Deserializer, Serializer};
-use crate::{Decode, Encode};
+use crate::{Decode, Encode, Encoder as _};
/// Serializes a type as a consensus-encoded hex string.
///
@@ -35,15 +36,13 @@ where
impl<T: Encode> fmt::Display for ConsensusHex<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- let encoder = self.0.encoder();
- let byte_iter = crate::EncoderByteIter::new(encoder);
- let iter = hex::BytesToHexIter::new(byte_iter, hex::Case::Lower).flatten();
-
- for ch in iter {
- fmt::Display::fmt(&ch, f)?;
+ let mut encoder = self.0.encoder();
+ loop {
+ fmt::Display::fmt(&encoder.current_chunk().as_hex(), f)?;
+ if encoder.advance().has_finished() {
+ return Ok(());
+ }
}
-
- Ok(())
}
}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.