Skip to content

Commit

Permalink
Optimize replace_headers to avoid copies of keys
Browse files Browse the repository at this point in the history
Sometimes, I'm amazed at what non lexical lifetimes allows.
  • Loading branch information
Dr-Emann authored and seanmonstar committed Aug 30, 2021
1 parent 2881354 commit 2767071
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::header::HeaderMap;
use crate::header::{Entry, HeaderMap, OccupiedEntry};

// xor-shift
#[cfg(not(target_arch = "wasm32"))]
Expand Down Expand Up @@ -42,21 +42,23 @@ pub(crate) fn replace_headers(dst: &mut HeaderMap, src: HeaderMap) {
// The first time a name is yielded, it will be Some(name), and if
// there are more values with the same name, the next yield will be
// None.
//
// TODO: a complex exercise would be to optimize this to only
// require 1 hash/lookup of the key, but doing something fancy
// with header::Entry...

let mut prev_name = None;
let mut prev_entry: Option<OccupiedEntry<_>> = None;
for (key, value) in src {
match key {
Some(key) => {
dst.insert(key.clone(), value);
prev_name = Some(key);
}
None => match prev_name {
Some(ref key) => {
dst.append(key.clone(), value);
Some(key) => match dst.entry(key) {
Entry::Occupied(mut e) => {
e.insert(value);
prev_entry = Some(e);
}
Entry::Vacant(e) => {
let e = e.insert_entry(value);
prev_entry = Some(e);
}
},
None => match prev_entry {
Some(ref mut entry) => {
entry.append(value);
}
None => unreachable!("HeaderMap::into_iter yielded None first"),
},
Expand Down

0 comments on commit 2767071

Please sign in to comment.