Skip to content

Commit b531947

Browse files
authored
docs: Clarify the HeaderMap documentation (#774)
1 parent 5d98edc commit b531947

File tree

2 files changed

+44
-42
lines changed

2 files changed

+44
-42
lines changed

src/header/map.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,47 @@ use super::HeaderValue;
1414
pub use self::as_header_name::AsHeaderName;
1515
pub use self::into_header_name::IntoHeaderName;
1616

17-
/// A set of HTTP headers
17+
/// A specialized [multimap](<https://en.wikipedia.org/wiki/Multimap>) for
18+
/// header names and values.
1819
///
19-
/// `HeaderMap` is a multimap of [`HeaderName`] to values.
20+
/// # Overview
21+
///
22+
/// `HeaderMap` is designed specifically for efficient manipulation of HTTP
23+
/// headers. It supports multiple values per header name and provides
24+
/// specialized APIs for insertion, retrieval, and iteration.
25+
///
26+
/// The internal implementation is optimized for common usage patterns in HTTP,
27+
/// and may change across versions. For example, the current implementation uses
28+
/// [Robin Hood
29+
/// hashing](<https://en.wikipedia.org/wiki/Hash_table#Robin_Hood_hashing>) to
30+
/// store entries compactly and enable high load factors with good performance.
31+
/// However, the collision resolution strategy and storage mechanism are not
32+
/// part of the public API and may be altered in future releases.
33+
///
34+
/// # Iteration order
35+
///
36+
/// Unless otherwise specified, the order in which items are returned by
37+
/// iterators from `HeaderMap` methods is arbitrary; there is no guaranteed
38+
/// ordering among the elements yielded by such an iterator. Changes to the
39+
/// iteration order are not considered breaking changes, so users must not rely
40+
/// on any incidental order produced by such an iterator. However, for a given
41+
/// crate version, the iteration order will be consistent across all platforms.
42+
///
43+
/// # Adaptive hashing
44+
///
45+
/// `HeaderMap` uses an adaptive strategy for hashing to maintain fast lookups
46+
/// while resisting hash collision attacks. The default hash function
47+
/// prioritizes performance. In scenarios where high collision rates are
48+
/// detected—typically indicative of denial-of-service attacks—the
49+
/// implementation switches to a more secure, collision-resistant hash function.
50+
///
51+
/// # Limitations
52+
///
53+
/// A `HeaderMap` can store at most 32,768 entries \(header name/value pairs\).
54+
/// Attempting to exceed this limit will result in a panic.
2055
///
2156
/// [`HeaderName`]: struct.HeaderName.html
57+
/// [`HeaderMap`]: struct.HeaderMap.html
2258
///
2359
/// # Examples
2460
///

src/header/mod.rs

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -28,47 +28,13 @@
2828
//!
2929
//! # `HeaderMap`
3030
//!
31-
//! `HeaderMap` is a map structure of header names highly optimized for use
32-
//! cases common with HTTP. It is a [multimap] structure, where each header name
33-
//! may have multiple associated header values. Given this, some of the APIs
34-
//! diverge from [`HashMap`].
31+
//! The [`HeaderMap`] type is a specialized
32+
//! [multimap](<https://en.wikipedia.org/wiki/Multimap>) structure for storing
33+
//! header names and values. It is designed specifically for efficient
34+
//! manipulation of HTTP headers. It supports multiple values per header name
35+
//! and provides specialized APIs for insertion, retrieval, and iteration.
3536
//!
36-
//! ## Overview
37-
//!
38-
//! Just like `HashMap` in Rust's stdlib, `HeaderMap` is based on [Robin Hood
39-
//! hashing]. This algorithm tends to reduce the worst case search times in the
40-
//! table and enables high load factors without seriously affecting performance.
41-
//! Internally, keys and values are stored in vectors. As such, each insertion
42-
//! will not incur allocation overhead. However, once the underlying vector
43-
//! storage is full, a larger vector must be allocated and all values copied.
44-
//!
45-
//! ## Deterministic ordering
46-
//!
47-
//! Unlike Rust's `HashMap`, values in `HeaderMap` are deterministically
48-
//! ordered. Roughly, values are ordered by insertion. This means that a
49-
//! function that deterministically operates on a header map can rely on the
50-
//! iteration order to remain consistent across processes and platforms.
51-
//!
52-
//! ## Adaptive hashing
53-
//!
54-
//! `HeaderMap` uses an adaptive hashing strategy in order to efficiently handle
55-
//! most common cases. All standard headers have statically computed hash values
56-
//! which removes the need to perform any hashing of these headers at runtime.
57-
//! The default hash function emphasizes performance over robustness. However,
58-
//! `HeaderMap` detects high collision rates and switches to a secure hash
59-
//! function in those events. The threshold is set such that only denial of
60-
//! service attacks should trigger it.
61-
//!
62-
//! ## Limitations
63-
//!
64-
//! `HeaderMap` can store a maximum of 32,768 headers (header name / value
65-
//! pairs). Attempting to insert more will result in a panic.
66-
//!
67-
//! [`HeaderName`]: struct.HeaderName.html
68-
//! [`HeaderMap`]: struct.HeaderMap.html
69-
//! [multimap]: https://en.wikipedia.org/wiki/Multimap
70-
//! [`HashMap`]: https://doc.rust-lang.org/std/collections/struct.HashMap.html
71-
//! [Robin Hood hashing]: https://en.wikipedia.org/wiki/Hash_table#Robin_Hood_hashing
37+
//! [*See also the `HeaderMap` type.*](HeaderMap)
7238
7339
mod map;
7440
mod name;

0 commit comments

Comments
 (0)