From 43228c1977d1275d68cb967ab23ca31c28a11dc4 Mon Sep 17 00:00:00 2001 From: Alex Bakon Date: Wed, 21 May 2025 07:15:12 -0400 Subject: [PATCH 1/2] Avoid unnecessary .expect()s for empty HeaderMap This change removes the Result::expect() calls in the constructors for an empty HeaderMap. These calls were provably not going to fail at runtime but rustc's inliner wasn't smart enough to figure that out: strings analysis of compiled binaries showed that the error message to the expect() still showed up in generated code. There are no behavioral differences as a result of this change. --- src/header/map.rs | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/header/map.rs b/src/header/map.rs index ea12a96c..4f72447e 100644 --- a/src/header/map.rs +++ b/src/header/map.rs @@ -445,12 +445,23 @@ impl HeaderMap { /// assert!(map.is_empty()); /// assert_eq!(0, map.capacity()); /// ``` + #[inline] pub fn new() -> Self { - HeaderMap::try_with_capacity(0).unwrap() + Self::new_empty() } } impl HeaderMap { + fn new_empty() -> Self { + HeaderMap { + mask: 0, + indices: Box::new([]), // as a ZST, this doesn't actually allocate anything + entries: Vec::new(), + extra_values: Vec::new(), + danger: Danger::Green, + } + } + /// Create an empty `HeaderMap` with the specified capacity. /// /// The returned map will allocate internal storage in order to hold about @@ -501,13 +512,7 @@ impl HeaderMap { /// ``` pub fn try_with_capacity(capacity: usize) -> Result, MaxSizeReached> { if capacity == 0 { - Ok(HeaderMap { - mask: 0, - indices: Box::new([]), // as a ZST, this doesn't actually allocate anything - entries: Vec::new(), - extra_values: Vec::new(), - danger: Danger::Green, - }) + Ok(Self::new_empty()) } else { let raw_cap = match to_raw_capacity(capacity).checked_next_power_of_two() { Some(c) => c, @@ -2165,8 +2170,9 @@ impl fmt::Debug for HeaderMap { } impl Default for HeaderMap { + #[inline] fn default() -> Self { - HeaderMap::try_with_capacity(0).expect("zero capacity should never fail") + HeaderMap::new_empty() } } From 6f661561974060e286aaeee80ef26fb84a309c44 Mon Sep 17 00:00:00 2001 From: Alex Bakon Date: Thu, 22 May 2025 09:57:51 -0400 Subject: [PATCH 2/2] Move new_empty() body into `Default` impl This gives us one fewer named method since we can use default() in place of new_empty(). It preserves the property that `HeaderMap::new()` constrains the generic type to `T=HeaderValue`. --- src/header/map.rs | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/header/map.rs b/src/header/map.rs index 4f72447e..4fa7b85a 100644 --- a/src/header/map.rs +++ b/src/header/map.rs @@ -447,12 +447,12 @@ impl HeaderMap { /// ``` #[inline] pub fn new() -> Self { - Self::new_empty() + Self::default() } } -impl HeaderMap { - fn new_empty() -> Self { +impl Default for HeaderMap { + fn default() -> Self { HeaderMap { mask: 0, indices: Box::new([]), // as a ZST, this doesn't actually allocate anything @@ -461,7 +461,9 @@ impl HeaderMap { danger: Danger::Green, } } +} +impl HeaderMap { /// Create an empty `HeaderMap` with the specified capacity. /// /// The returned map will allocate internal storage in order to hold about @@ -512,7 +514,7 @@ impl HeaderMap { /// ``` pub fn try_with_capacity(capacity: usize) -> Result, MaxSizeReached> { if capacity == 0 { - Ok(Self::new_empty()) + Ok(Self::default()) } else { let raw_cap = match to_raw_capacity(capacity).checked_next_power_of_two() { Some(c) => c, @@ -2169,13 +2171,6 @@ impl fmt::Debug for HeaderMap { } } -impl Default for HeaderMap { - #[inline] - fn default() -> Self { - HeaderMap::new_empty() - } -} - impl ops::Index for HeaderMap where K: AsHeaderName,