diff --git a/actix-http/CHANGES.md b/actix-http/CHANGES.md index 816d4b71f..045ae461f 100644 --- a/actix-http/CHANGES.md +++ b/actix-http/CHANGES.md @@ -5,7 +5,11 @@ - Implement `MessageBody` for `&mut B` where `B: MessageBody + Unpin`. [#2868] - Implement `MessageBody` for `Pin` where `B::Target: MessageBody`. [#2868] +### Performance +- Improve overall performance of operations on `Extensions`. [#2890] + [#2868]: https://github.com/actix/actix-web/pull/2868 +[#2890]: https://github.com/actix/actix-web/pull/2890 ## 3.2.2 - 2022-09-11 diff --git a/actix-http/src/extensions.rs b/actix-http/src/extensions.rs index 60b769d13..f2047a9ce 100644 --- a/actix-http/src/extensions.rs +++ b/actix-http/src/extensions.rs @@ -1,9 +1,30 @@ use std::{ any::{Any, TypeId}, + collections::HashMap, fmt, + hash::{BuildHasherDefault, Hasher}, }; -use ahash::AHashMap; +/// A hasher for `TypeId`s that takes advantage of its known characteristics. +/// +/// Author of `anymap` crate has done research on the topic: +/// https://github.com/chris-morgan/anymap/blob/2e9a5704/src/lib.rs#L599 +#[derive(Debug, Default)] +struct NoOpHasher(u64); + +impl Hasher for NoOpHasher { + fn write(&mut self, _bytes: &[u8]) { + unimplemented!("This NoOpHasher can only handle u64s") + } + + fn write_u64(&mut self, i: u64) { + self.0 = i; + } + + fn finish(&self) -> u64 { + self.0 + } +} /// A type map for request extensions. /// @@ -11,7 +32,7 @@ use ahash::AHashMap; #[derive(Default)] pub struct Extensions { /// Use AHasher with a std HashMap with for faster lookups on the small `TypeId` keys. - map: AHashMap>, + map: HashMap, BuildHasherDefault>, } impl Extensions { @@ -19,7 +40,7 @@ impl Extensions { #[inline] pub fn new() -> Extensions { Extensions { - map: AHashMap::new(), + map: HashMap::default(), } }