Skip to content

Commit bdb716d

Browse files
weekday-grandine-iopovi
authored andcommitted
Optimize hexadecimal conversions in serde_utils to speed up JSON serialization of byte collections
Slow serialization to JSON was claimed to be the problem that led to 2e92e00. BlobSidecar serialization is now around 15.4 times faster. BlobSidecar deserialization is now around 12 times faster. SignedBeaconBlock serialization is now up to 1.5 times faster. SignedBeaconBlock deserialization is now up to 1.1 times faster. faster-hex might be faster than const-hex in some cases, but faster-hex does not provide the API we require. See https://crates.io/crates/const-hex/1.14.0 for benchmark results comparing various hexadecimal conversion crates.
1 parent 82c1cb5 commit bdb716d

File tree

7 files changed

+51
-8
lines changed

7 files changed

+51
-8
lines changed

Cargo.lock

Lines changed: 45 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ bytesize = { version = '1', features = ['serde'] }
300300
cached = '0.53'
301301
chrono = '0.4'
302302
clap = { version = '4', features = ['derive'] }
303+
const-hex = '1.14'
303304
const_format = '0.2'
304305
constant_time_eq = '0.3'
305306
conv = '0.3'
@@ -339,7 +340,6 @@ hash_hasher = '2'
339340
hashlink = '0.9'
340341
hex = { version = '0.4', features = ['serde'] }
341342
hex-literal = '0.4'
342-
hex_fmt = '0.3'
343343
hmac = '0.12'
344344
http = '1'
345345
http-body-util = '0.1'

serde_utils/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ authors = ["Grandine <[email protected]>"]
77
workspace = true
88

99
[dependencies]
10+
const-hex = { workspace = true }
1011
generic-array = { workspace = true }
1112
hex = { workspace = true }
12-
hex_fmt = { workspace = true }
1313
itertools = { workspace = true }
1414
num-traits = { workspace = true }
1515
serde = { workspace = true }

serde_utils/src/prefixed_hex_or_bytes_array.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub fn deserialize<'de, D: Deserializer<'de>, const N: usize>(
2929
let digits = shared::strip_hex_prefix(string)?;
3030

3131
let mut bytes = [0; N];
32-
hex::decode_to_slice(digits, &mut bytes).map_err(E::custom)?;
32+
const_hex::decode_to_slice(digits, &mut bytes).map_err(E::custom)?;
3333

3434
Ok(bytes)
3535
}

serde_utils/src/prefixed_hex_or_bytes_cow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Cow<'de
3434

3535
fn visit_str<E: Error>(self, string: &str) -> Result<Self::Value, E> {
3636
let digits = shared::strip_hex_prefix(string)?;
37-
let bytes = hex::decode(digits).map_err(E::custom)?;
37+
let bytes = const_hex::decode(digits).map_err(E::custom)?;
3838
Ok(Cow::Owned(bytes))
3939
}
4040
}

serde_utils/src/prefixed_hex_or_bytes_generic_array.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub fn deserialize<'de, D: Deserializer<'de>, N: ArrayLength<u8>>(
4343
let digits = shared::strip_hex_prefix(string)?;
4444

4545
let mut bytes = GenericArray::default();
46-
hex::decode_to_slice(digits, &mut bytes).map_err(E::custom)?;
46+
const_hex::decode_to_slice(digits, &mut bytes).map_err(E::custom)?;
4747

4848
Ok(bytes)
4949
}

serde_utils/src/prefixed_hex_or_bytes_slice.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
use hex_fmt::HexFmt;
21
use serde::Serializer;
32

43
pub fn serialize<S: Serializer>(bytes: impl AsRef<[u8]>, serializer: S) -> Result<S::Ok, S::Error> {
54
if serializer.is_human_readable() {
6-
serializer.collect_str(&format_args!("0x{}", HexFmt(bytes)))
5+
serializer.serialize_str(const_hex::encode_prefixed(bytes).as_str())
76
} else {
87
serializer.serialize_bytes(bytes.as_ref())
98
}

0 commit comments

Comments
 (0)