1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-27 17:52:56 +01:00

increase content-length fast path to responses up to 1MB

This commit is contained in:
Rob Ede 2020-02-24 20:58:41 +00:00
parent f266b44cb0
commit 94da08f506
No known key found for this signature in database
GPG Key ID: C2A3B36E841A91E6
2 changed files with 82 additions and 4 deletions

View File

@ -67,7 +67,7 @@ mod _new {
bytes.put_u8(DIGITS_START + d100); bytes.put_u8(DIGITS_START + d100);
bytes.put_u8(DIGITS_START + d10); bytes.put_u8(DIGITS_START + d10);
bytes.put_u8(DIGITS_START + d1); bytes.put_u8(DIGITS_START + d1);
} else if n < 10000 { } else if n < 10_000 {
let n = n as u16; let n = n as u16;
let d1000 = (n / 1000) as u8; let d1000 = (n / 1000) as u8;
@ -75,6 +75,36 @@ mod _new {
let d10 = ((n / 10) % 10) as u8; let d10 = ((n / 10) % 10) as u8;
let d1 = (n % 10) as u8; let d1 = (n % 10) as u8;
bytes.put_u8(DIGITS_START + d1000);
bytes.put_u8(DIGITS_START + d100);
bytes.put_u8(DIGITS_START + d10);
bytes.put_u8(DIGITS_START + d1);
} else if n < 100_000 {
let n = n as u32;
let d10000 = (n / 10000) as u8;
let d1000 = ((n / 1000) % 10) as u8;
let d100 = ((n / 100) % 10) as u8;
let d10 = ((n / 10) % 10) as u8;
let d1 = (n % 10) as u8;
bytes.put_u8(DIGITS_START + d10000);
bytes.put_u8(DIGITS_START + d1000);
bytes.put_u8(DIGITS_START + d100);
bytes.put_u8(DIGITS_START + d10);
bytes.put_u8(DIGITS_START + d1);
} else if n < 1_000_000 {
let n = n as u32;
let d100000 = (n / 100000) as u8;
let d10000 = ((n / 10000) % 10) as u8;
let d1000 = ((n / 1000) % 10) as u8;
let d100 = ((n / 100) % 10) as u8;
let d10 = ((n / 10) % 10) as u8;
let d1 = (n % 10) as u8;
bytes.put_u8(DIGITS_START + d100000);
bytes.put_u8(DIGITS_START + d10000);
bytes.put_u8(DIGITS_START + d1000); bytes.put_u8(DIGITS_START + d1000);
bytes.put_u8(DIGITS_START + d100); bytes.put_u8(DIGITS_START + d100);
bytes.put_u8(DIGITS_START + d10); bytes.put_u8(DIGITS_START + d10);

View File

@ -88,7 +88,7 @@ pub fn write_content_length(n: usize, bytes: &mut BytesMut) {
bytes.put_u8(DIGITS_START + d100); bytes.put_u8(DIGITS_START + d100);
bytes.put_u8(DIGITS_START + d10); bytes.put_u8(DIGITS_START + d10);
bytes.put_u8(DIGITS_START + d1); bytes.put_u8(DIGITS_START + d1);
} else if n < 10000 { } else if n < 10_000 {
let n = n as u16; let n = n as u16;
let d1000 = (n / 1000) as u8; let d1000 = (n / 1000) as u8;
@ -96,6 +96,36 @@ pub fn write_content_length(n: usize, bytes: &mut BytesMut) {
let d10 = ((n / 10) % 10) as u8; let d10 = ((n / 10) % 10) as u8;
let d1 = (n % 10) as u8; let d1 = (n % 10) as u8;
bytes.put_u8(DIGITS_START + d1000);
bytes.put_u8(DIGITS_START + d100);
bytes.put_u8(DIGITS_START + d10);
bytes.put_u8(DIGITS_START + d1);
} else if n < 100_000 {
let n = n as u32;
let d10000 = (n / 10000) as u8;
let d1000 = ((n / 1000) % 10) as u8;
let d100 = ((n / 100) % 10) as u8;
let d10 = ((n / 10) % 10) as u8;
let d1 = (n % 10) as u8;
bytes.put_u8(DIGITS_START + d10000);
bytes.put_u8(DIGITS_START + d1000);
bytes.put_u8(DIGITS_START + d100);
bytes.put_u8(DIGITS_START + d10);
bytes.put_u8(DIGITS_START + d1);
} else if n < 1_000_000 {
let n = n as u32;
let d100000 = (n / 100000) as u8;
let d10000 = ((n / 10000) % 10) as u8;
let d1000 = ((n / 1000) % 10) as u8;
let d100 = ((n / 100) % 10) as u8;
let d10 = ((n / 10) % 10) as u8;
let d1 = (n % 10) as u8;
bytes.put_u8(DIGITS_START + d100000);
bytes.put_u8(DIGITS_START + d10000);
bytes.put_u8(DIGITS_START + d1000); bytes.put_u8(DIGITS_START + d1000);
bytes.put_u8(DIGITS_START + d100); bytes.put_u8(DIGITS_START + d100);
bytes.put_u8(DIGITS_START + d10); bytes.put_u8(DIGITS_START + d10);
@ -195,11 +225,17 @@ mod tests {
write_content_length(5909, &mut bytes); write_content_length(5909, &mut bytes);
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 5909\r\n"[..]); assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 5909\r\n"[..]);
bytes.reserve(50); bytes.reserve(50);
write_content_length(9999, &mut bytes);
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 9999\r\n"[..]);
bytes.reserve(50);
write_content_length(10001, &mut bytes); write_content_length(10001, &mut bytes);
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 10001\r\n"[..]); assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 10001\r\n"[..]);
bytes.reserve(50); bytes.reserve(50);
write_content_length(59094, &mut bytes); write_content_length(59094, &mut bytes);
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 59094\r\n"[..]); assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 59094\r\n"[..]);
bytes.reserve(50);
write_content_length(99999, &mut bytes);
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 99999\r\n"[..]);
bytes.reserve(50); bytes.reserve(50);
write_content_length(590947, &mut bytes); write_content_length(590947, &mut bytes);
@ -208,6 +244,12 @@ mod tests {
b"\r\ncontent-length: 590947\r\n"[..] b"\r\ncontent-length: 590947\r\n"[..]
); );
bytes.reserve(50); bytes.reserve(50);
write_content_length(999999, &mut bytes);
assert_eq!(
bytes.split().freeze(),
b"\r\ncontent-length: 999999\r\n"[..]
);
bytes.reserve(50);
write_content_length(5909471, &mut bytes); write_content_length(5909471, &mut bytes);
assert_eq!( assert_eq!(
bytes.split().freeze(), bytes.split().freeze(),
@ -219,5 +261,11 @@ mod tests {
bytes.split().freeze(), bytes.split().freeze(),
b"\r\ncontent-length: 59094718\r\n"[..] b"\r\ncontent-length: 59094718\r\n"[..]
); );
bytes.reserve(50);
write_content_length(4294973728, &mut bytes);
assert_eq!(
bytes.split().freeze(),
b"\r\ncontent-length: 4294973728\r\n"[..]
);
} }
} }