From 8fda1ac5dafeffd8e8e7c38ebec5b20ee70184f4 Mon Sep 17 00:00:00 2001 From: Aditya Date: Fri, 24 Jul 2026 12:48:30 +0530 Subject: [PATCH 1/2] headers: optimize from_digits using try_fold for functional digit accumulation Signed-off-by: Aditya --- src/headers.rs | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/src/headers.rs b/src/headers.rs index 0b36e060f0..94237b90fa 100644 --- a/src/headers.rs +++ b/src/headers.rs @@ -68,30 +68,16 @@ pub(super) fn content_length_parse_all_values(values: ValueIter<'_, HeaderValue> } fn from_digits(bytes: &[u8]) -> Option { - // cannot use FromStr for u64, since it allows a signed prefix - let mut result = 0u64; - const RADIX: u64 = 10; - if bytes.is_empty() { return None; } - for &b in bytes { - // can't use char::to_digit, since we haven't verified these bytes - // are utf-8. - match b { - b'0'..=b'9' => { - result = result.checked_mul(RADIX)?; - result = result.checked_add(u64::from(b - b'0'))?; - } - _ => { - // not a DIGIT, get outta here! - return None; - } - } - } - - Some(result) + bytes.iter().try_fold(0u64, |acc, &b| match b { + b'0'..=b'9' => acc + .checked_mul(10)? + .checked_add(u64::from(b - b'0')), + _ => None, + }) } #[cfg(all(feature = "http2", feature = "client"))] From 906e03dc739ec38a7da4bbe8e02d84d8315ab9de Mon Sep 17 00:00:00 2001 From: Aditya Date: Fri, 24 Jul 2026 18:24:34 +0530 Subject: [PATCH 2/2] headers: format from_digits for rustfmt compliance Signed-off-by: Aditya --- src/headers.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/headers.rs b/src/headers.rs index 94237b90fa..251c8517cd 100644 --- a/src/headers.rs +++ b/src/headers.rs @@ -73,9 +73,7 @@ fn from_digits(bytes: &[u8]) -> Option { } bytes.iter().try_fold(0u64, |acc, &b| match b { - b'0'..=b'9' => acc - .checked_mul(10)? - .checked_add(u64::from(b - b'0')), + b'0'..=b'9' => acc.checked_mul(10)?.checked_add(u64::from(b - b'0')), _ => None, }) }