Skip to content

Commit

Permalink
refactor(header): remove BytesMut inline optimization when creating (#…
Browse files Browse the repository at this point in the history
…738)

`HeaderValue` from integers

`::bytes::BytesMut >= 1` has no inline optimization
  • Loading branch information
ADD-SP authored Dec 27, 2024
1 parent 091ee9a commit 4e02046
Showing 1 changed file with 2 additions and 22 deletions.
24 changes: 2 additions & 22 deletions src/header/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::error::Error;
use std::fmt::Write;
use std::hash::{Hash, Hasher};
use std::str::FromStr;
use std::{cmp, fmt, mem, str};
use std::{cmp, fmt, str};

use crate::header::name::HeaderName;

Expand Down Expand Up @@ -424,27 +424,7 @@ macro_rules! from_integers {
($($name:ident: $t:ident => $max_len:expr),*) => {$(
impl From<$t> for HeaderValue {
fn from(num: $t) -> HeaderValue {
let mut buf = if mem::size_of::<BytesMut>() - 1 < $max_len {
// On 32bit platforms, BytesMut max inline size
// is 15 bytes, but the $max_len could be bigger.
//
// The likelihood of the number *actually* being
// that big is very small, so only allocate
// if the number needs that space.
//
// The largest decimal number in 15 digits:
// It wold be 10.pow(15) - 1, but this is a constant
// version.
if num as u64 > 999_999_999_999_999_999 {
BytesMut::with_capacity($max_len)
} else {
// fits inline...
BytesMut::new()
}
} else {
// full value fits inline, so don't allocate!
BytesMut::new()
};
let mut buf = BytesMut::with_capacity($max_len);
let _ = buf.write_str(::itoa::Buffer::new().format(num));
HeaderValue {
inner: buf.freeze(),
Expand Down

0 comments on commit 4e02046

Please sign in to comment.