Skip to content

Commit 43228c1

Browse files
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.
1 parent 181f73c commit 43228c1

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/header/map.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -445,12 +445,23 @@ impl HeaderMap {
445445
/// assert!(map.is_empty());
446446
/// assert_eq!(0, map.capacity());
447447
/// ```
448+
#[inline]
448449
pub fn new() -> Self {
449-
HeaderMap::try_with_capacity(0).unwrap()
450+
Self::new_empty()
450451
}
451452
}
452453

453454
impl<T> HeaderMap<T> {
455+
fn new_empty() -> Self {
456+
HeaderMap {
457+
mask: 0,
458+
indices: Box::new([]), // as a ZST, this doesn't actually allocate anything
459+
entries: Vec::new(),
460+
extra_values: Vec::new(),
461+
danger: Danger::Green,
462+
}
463+
}
464+
454465
/// Create an empty `HeaderMap` with the specified capacity.
455466
///
456467
/// The returned map will allocate internal storage in order to hold about
@@ -501,13 +512,7 @@ impl<T> HeaderMap<T> {
501512
/// ```
502513
pub fn try_with_capacity(capacity: usize) -> Result<HeaderMap<T>, MaxSizeReached> {
503514
if capacity == 0 {
504-
Ok(HeaderMap {
505-
mask: 0,
506-
indices: Box::new([]), // as a ZST, this doesn't actually allocate anything
507-
entries: Vec::new(),
508-
extra_values: Vec::new(),
509-
danger: Danger::Green,
510-
})
515+
Ok(Self::new_empty())
511516
} else {
512517
let raw_cap = match to_raw_capacity(capacity).checked_next_power_of_two() {
513518
Some(c) => c,
@@ -2165,8 +2170,9 @@ impl<T: fmt::Debug> fmt::Debug for HeaderMap<T> {
21652170
}
21662171

21672172
impl<T> Default for HeaderMap<T> {
2173+
#[inline]
21682174
fn default() -> Self {
2169-
HeaderMap::try_with_capacity(0).expect("zero capacity should never fail")
2175+
HeaderMap::new_empty()
21702176
}
21712177
}
21722178

0 commit comments

Comments
 (0)