Skip to content

Commit d29e1a5

Browse files
committed
Clarify the HeaderMap documentaion.
1 parent 613d3d4 commit d29e1a5

File tree

2 files changed

+45
-42
lines changed

2 files changed

+45
-42
lines changed

src/header/map.rs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,48 @@ 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+
/// The `HeaderMap` type is a specialized
18+
/// [multimap](<https://en.wikipedia.org/wiki/Multimap>) structure for storing
19+
/// header names and values.
1820
///
19-
/// `HeaderMap` is a multimap of [`HeaderName`] to values.
21+
/// # Overview
22+
///
23+
/// `HeaderMap` is designed specifically for efficient manipulation of HTTP
24+
/// headers. It supports multiple values per header name and provides
25+
/// specialized APIs for insertion, retrieval, and iteration.
26+
///
27+
/// The internal implementation is optimized for common usage patterns in HTTP,
28+
/// and may change across versions. For example, the current implementation uses
29+
/// [Robin Hood
30+
/// hashing](<https://en.wikipedia.org/wiki/Hash_table#Robin_Hood_hashing>) to
31+
/// store entries compactly and enable high load factors with good performance.
32+
/// However, the collision resolution strategy and storage mechanism are not
33+
/// part of the public API and may be altered in future releases.
34+
///
35+
/// # Iteration order
36+
///
37+
/// Unless otherwise specified, the order in which items are returned by
38+
/// iterators from `HeaderMap` methods is arbitrary; there is no guaranteed
39+
/// ordering between elements yielded by such an iterator. However, for a given
40+
/// crate version, the iteration order will be consistent across all platforms.
41+
/// Changing the iteration order of such an iterator is considered as a [minor
42+
/// breaking change](<https://doc.rust-lang.org/cargo/reference/semver.html>).
43+
///
44+
/// # Adaptive hashing
45+
///
46+
/// `HeaderMap` uses an adaptive strategy for hashing to maintain fast lookups
47+
/// while resisting hash collision attacks. The default hash function
48+
/// prioritizes performance. In scenarios where high collision rates are
49+
/// detected—typically indicative of denial-of-service attacks—the
50+
/// implementation switches to a more secure, collision-resistant hash function.
51+
///
52+
/// # Limitations
53+
///
54+
/// A `HeaderMap` can store at most 32,768 entries \(header name/value pairs\).
55+
/// Attempting to exceed this limit will result in a panic.
2056
///
2157
/// [`HeaderName`]: struct.HeaderName.html
58+
/// [`HeaderMap`]: struct.HeaderMap.html
2259
///
2360
/// # Examples
2461
///

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)