Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix headers bench: use Vec::reserve for reserve buf capacity #215

Merged
merged 3 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ byte_reader = "3.0.0"
tokio = { version = "1.37.0", features = ["full"] }
tracing = "0.1.4"
tracing-subscriber = "0.3.18"
hashbrown = { version = "0.14.5", features = ["raw"] }
hashbrown = { version = "0.14.5", features = ["raw", "inline-more"] }

[dev-dependencies]
rand = "0.8.5"
Expand Down
54 changes: 27 additions & 27 deletions benches/benches/request_headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,46 @@ use ohkami_benches::request_headers::{


fn input() -> Vec<u8> {
// let input: &[u8; 819] = test::black_box(b"\
// Accept-Language: fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5\r\n\
// Authorization: Bearer dummy-authorization-token-sample\r\n\
// Date: Wed, 21 Oct 2015 07:28:00 GMT\r\n\
// Host: localhost:7777\r\n\
// Origin: localhost:3333\r\n\
// Referer: https://developer.mozilla.org/ja/docs/Web/JavaScript\r\n\
// Referrer-Policy: no-referrer\r\n\
// Via: HTTP/1.1 GWA\r\n\
// User-Agent: Mozilla/5.0 (platform; rv:geckoversion) Gecko/geckotrail Firefox/firefoxversion\r\n\
// Transfer-Encoding: identity\r\n\
// Connection: upgrade\r\n\
// Upgrade: a_protocol/1, example ,another_protocol/2.2\r\n\
// Forwarded: for=192.0.2.60; proto=http; by=203.0.113.43\r\n\
// Upgrade-Insecure-Requests: 1\r\n\
// From: [email protected]\r\n\
// X-MyApp-Data: example-custom-header-value\r\n\
// Some-Custom-Header: strawberry\r\n\
// Expect: 100-continue\r\n\
// Cookie: PHPSESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1\r\n\
// Cache-Control: no-cache\r\n\
// \r\n\
// ");

let input: &[u8; 485] = test::black_box(b"\
let input: &[u8; 819] = test::black_box(b"\
Accept-Language: fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5\r\n\
Authorization: Bearer dummy-authorization-token-sample\r\n\
Date: Wed, 21 Oct 2015 07:28:00 GMT\r\n\
Host: localhost:7777\r\n\
Origin: localhost:3333\r\n\
Referer: https://developer.mozilla.org/ja/docs/Web/JavaScript\r\n\
Referrer-Policy: no-referrer\r\n\
Via: HTTP/1.1 GWA\r\n\
User-Agent: Mozilla/5.0 (platform; rv:geckoversion) Gecko/geckotrail Firefox/firefoxversion\r\n\
Transfer-Encoding: identity\r\n\
Connection: Keep-Alive\r\n\
Connection: upgrade\r\n\
Upgrade: a_protocol/1, example ,another_protocol/2.2\r\n\
Forwarded: for=192.0.2.60; proto=http; by=203.0.113.43\r\n\
Upgrade-Insecure-Requests: 1\r\n\
From: [email protected]\r\n\
X-MyApp-Data: example-custom-header-value\r\n\
Some-Custom-Header: strawberry\r\n\
Expect: 100-continue\r\n\
Cookie: PHPSESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1\r\n\
Cache-Control: no-cache\r\n\
\r\n\
");

// let input: &[u8; 485] = test::black_box(b"\
// Authorization: Bearer dummy-authorization-token-sample\r\n\
// Date: Wed, 21 Oct 2015 07:28:00 GMT\r\n\
// Host: localhost:7777\r\n\
// Origin: localhost:3333\r\n\
// User-Agent: Mozilla/5.0 (platform; rv:geckoversion) Gecko/geckotrail Firefox/firefoxversion\r\n\
// Transfer-Encoding: identity\r\n\
// Connection: Keep-Alive\r\n\
// From: [email protected]\r\n\
// X-MyApp-Data: example-custom-header-value\r\n\
// Some-Custom-Header: strawberry\r\n\
// Cookie: PHPSESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1\r\n\
// Cache-Control: no-cache\r\n\
// \r\n\
// ");

// let input: &[u8; 301] = test::black_box(b"\
// Authorization: Bearer dummy-authorization-token-sample\r\n\
// Host: localhost:7777\r\n\
Expand Down Expand Up @@ -171,7 +171,7 @@ fn input() -> Vec<u8> {
b.iter(|| {
let mut r = byte_reader::Reader::new(black_box(input.as_slice()));

let mut h = HeaderHashBrown::new();
let mut h = HeaderHashBrown::<false>::new();
while r.consume("\r\n").is_none() {
let key_bytes = r.read_while(|b| b != &b':');
r.consume(": ").unwrap();
Expand Down
129 changes: 68 additions & 61 deletions benches/benches/response_headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ use ohkami_benches::response_headers::{
.insert("Access-Control-Allow-Credentials", black_box("true"))
.insert("Access-Control-Allow-Headers", black_box("X-Custom-Header,Upgrade-Insecure-Requests"))
.insert("Access-Control-Allow-Origin", black_box("https://foo.bar.org"))
.insert("Access-Control-Allow-Methods", black_box("POST,GET,OPTIONS,DELETE"))
.insert("Access-Control-Max-Age", black_box("86400"))
.insert("Vary", black_box("Origin"))
.insert("Server", black_box("ohkami"))
Expand All @@ -160,6 +161,7 @@ use ohkami_benches::response_headers::{
.AccessControlAllowCredentials(black_box("true"))
.AccessControlAllowHeaders(black_box("X-Custom-Header,Upgrade-Insecure-Requests"))
.AccessControlAllowOrigin(black_box("https://foo.bar.org"))
.AccessControlAllowMethods(black_box("POST,GET,OPTIONS,DELETE"))
.AccessControlMaxAge(black_box("86400"))
.Vary(black_box("Origin"))
.Server(black_box("ohkami"))
Expand All @@ -176,12 +178,13 @@ use ohkami_benches::response_headers::{
}

#[bench] fn insert_header_hashbrown(b: &mut test::Bencher) {
let mut h = HeaderHashBrown::new();
let mut h = HeaderHashBrown::<true>::new();
b.iter(|| {
h
.insert_standard_from_reqbytes(StandardHeader::AccessControlAllowCredentials, black_box(b"true"))
.insert_standard_from_reqbytes(StandardHeader::AccessControlAllowHeaders, black_box(b"X-Custom-Header,Upgrade-Insecure-Requests"))
.insert_standard_from_reqbytes(StandardHeader::AccessControlAllowOrigin, black_box(b"https://foo.bar.org"))
.insert_standard_from_reqbytes(StandardHeader::AccessControlAllowMethods, black_box(b"POST,GET,OPTIONS,DELETE"))
.insert_standard_from_reqbytes(StandardHeader::AccessControlMaxAge, black_box(b"86400"))
.insert_standard_from_reqbytes(StandardHeader::Vary, black_box(b"Origin"))
.insert_standard_from_reqbytes(StandardHeader::Server, black_box(b"ohkami"))
Expand Down Expand Up @@ -223,22 +226,22 @@ use ohkami_benches::response_headers::{

b.iter(|| {
h.set()
.AccessControlAllowCredentials(black_box(None))
.AccessControlAllowHeaders(black_box(None))
.AccessControlAllowOrigin(black_box(None))
.AccessControlAllowMethods(black_box(None))
.AccessControlMaxAge(black_box(None))
.Vary(black_box(None))
.Server(black_box(None))
.Connection(black_box(None))
.Date(black_box(None))
.Via(black_box(None))
.AltSvc(black_box(None))
.ProxyAuthenticate(black_box(None))
.ReferrerPolicy(black_box(None))
.XFrameOptions(black_box(None))
.custom("x-myapp-data", black_box(None))
.custom("something", black_box(None))
.AccessControlAllowCredentials(None)
.AccessControlAllowHeaders(None)
.AccessControlAllowOrigin(None)
.AccessControlAllowMethods(None)
.AccessControlMaxAge(None)
.Vary(None)
.Server(None)
.Connection(None)
.Date(None)
.Via(None)
.AltSvc(None)
.ProxyAuthenticate(None)
.ReferrerPolicy(None)
.XFrameOptions(None)
.custom("x-myapp-data", None)
.custom("something", None)
;
});
}
Expand Down Expand Up @@ -332,6 +335,7 @@ use ohkami_benches::response_headers::{
.insert("Access-Control-Allow-Credentials", black_box("true"))
.insert("Access-Control-Allow-Headers", black_box("X-Custom-Header,Upgrade-Insecure-Requests"))
.insert("Access-Control-Allow-Origin", black_box("https://foo.bar.org"))
.insert("Access-Control-Allow-Methods", black_box("POST,GET,OPTIONS,DELETE"))
.insert("Access-Control-Max-Age", black_box("86400"))
.insert("Vary", black_box("Origin"))
.insert("Server", black_box("ohkami"))
Expand All @@ -350,6 +354,7 @@ use ohkami_benches::response_headers::{
.remove("Access-Control-Allow-Credentials")
.remove("Access-Control-Allow-Headers")
.remove("Access-Control-Allow-Origin")
.remove("Access-Control-Allow-Methods")
.remove("Access-Control-Max-Age")
.remove("Vary")
.remove("Server")
Expand All @@ -372,6 +377,7 @@ use ohkami_benches::response_headers::{
.AccessControlAllowCredentials(black_box("true"))
.AccessControlAllowHeaders(black_box("X-Custom-Header,Upgrade-Insecure-Requests"))
.AccessControlAllowOrigin(black_box("https://foo.bar.org"))
.AccessControlAllowMethods(black_box("POST,GET,OPTIONS,DELETE"))
.AccessControlMaxAge(black_box("86400"))
.Vary(black_box("Origin"))
.Server(black_box("ohkami"))
Expand All @@ -387,32 +393,33 @@ use ohkami_benches::response_headers::{

b.iter(|| {
h.set()
.AccessControlAllowCredentials(black_box(None))
.AccessControlAllowHeaders(black_box(None))
.AccessControlAllowOrigin(black_box(None))
.AccessControlAllowMethods(black_box(None))
.AccessControlMaxAge(black_box(None))
.Vary(black_box(None))
.Server(black_box(None))
.Connection(black_box(None))
.Date(black_box(None))
.Via(black_box(None))
.AltSvc(black_box(None))
.ProxyAuthenticate(black_box(None))
.ReferrerPolicy(black_box(None))
.XFrameOptions(black_box(None))
.custom("x-myapp-data", black_box(None))
.custom("something", black_box(None))
.AccessControlAllowCredentials(None)
.AccessControlAllowHeaders(None)
.AccessControlAllowOrigin(None)
.AccessControlAllowMethods(None)
.AccessControlMaxAge(None)
.Vary(None)
.Server(None)
.Connection(None)
.Date(None)
.Via(None)
.AltSvc(None)
.ProxyAuthenticate(None)
.ReferrerPolicy(None)
.XFrameOptions(None)
.custom("x-myapp-data", None)
.custom("something", None)
;
});
}

#[bench] fn remove_header_hashbrown(b: &mut test::Bencher) {
let mut h = HeaderHashBrown::new();
let mut h = HeaderHashBrown::<true>::new();
h
.insert_standard_from_reqbytes(StandardHeader::AccessControlAllowCredentials, black_box(b"true"))
.insert_standard_from_reqbytes(StandardHeader::AccessControlAllowHeaders, black_box(b"X-Custom-Header,Upgrade-Insecure-Requests"))
.insert_standard_from_reqbytes(StandardHeader::AccessControlAllowOrigin, black_box(b"https://foo.bar.org"))
.insert_standard_from_reqbytes(StandardHeader::AccessControlAllowMethods, black_box(b"POST,GET,OPTIONS,DELETE"))
.insert_standard_from_reqbytes(StandardHeader::AccessControlMaxAge, black_box(b"86400"))
.insert_standard_from_reqbytes(StandardHeader::Vary, black_box(b"Origin"))
.insert_standard_from_reqbytes(StandardHeader::Server, black_box(b"ohkami"))
Expand Down Expand Up @@ -453,11 +460,11 @@ use ohkami_benches::response_headers::{
#[bench] fn write_03_ohkami(b: &mut test::Bencher) {
let mut h = ResponseHeaders::_new();
h.set()
// .AccessControlAllowCredentials(black_box("true"))
// .AccessControlAllowHeaders(black_box("X-Custom-Header,Upgrade-Insecure-Requests"))
// .AccessControlAllowOrigin(black_box("https://foo.bar.org"))
// .AccessControlAllowMethods(black_box("POST,GET,OPTIONS,DELETE"))
// .AccessControlMaxAge(black_box("86400"))
.AccessControlAllowCredentials(black_box("true"))
.AccessControlAllowHeaders(black_box("X-Custom-Header,Upgrade-Insecure-Requests"))
.AccessControlAllowOrigin(black_box("https://foo.bar.org"))
.AccessControlAllowMethods(black_box("POST,GET,OPTIONS,DELETE"))
.AccessControlMaxAge(black_box("86400"))
.Vary(black_box("Origin"))
.Server(black_box("ohkami"))
.Connection(black_box("Keep-Alive"))
Expand Down Expand Up @@ -559,11 +566,11 @@ use ohkami_benches::response_headers::{

#[bench] fn write_02_http_crate(b: &mut test::Bencher) {
let mut h = HeaderMap::new();
// h.insert(header::ACCESS_CONTROL_ALLOW_CREDENTIALS, HeaderValue::from_static(black_box("true")));
// h.insert(header::ACCESS_CONTROL_ALLOW_HEADERS, HeaderValue::from_static(black_box("X-Custom-Header,Upgrade-Insecure-Requests")));
// h.insert(header::ACCESS_CONTROL_ALLOW_ORIGIN, HeaderValue::from_static(black_box("https://foo.bar.org")));
// h.insert(header::ACCESS_CONTROL_ALLOW_METHODS, HeaderValue::from_static(black_box("POST,GET,OPTIONS,DELETE")));
// h.insert(header::ACCESS_CONTROL_MAX_AGE, HeaderValue::from_static(black_box("86400")));
h.insert(header::ACCESS_CONTROL_ALLOW_CREDENTIALS, HeaderValue::from_static(black_box("true")));
h.insert(header::ACCESS_CONTROL_ALLOW_HEADERS, HeaderValue::from_static(black_box("X-Custom-Header,Upgrade-Insecure-Requests")));
h.insert(header::ACCESS_CONTROL_ALLOW_ORIGIN, HeaderValue::from_static(black_box("https://foo.bar.org")));
h.insert(header::ACCESS_CONTROL_ALLOW_METHODS, HeaderValue::from_static(black_box("POST,GET,OPTIONS,DELETE")));
h.insert(header::ACCESS_CONTROL_MAX_AGE, HeaderValue::from_static(black_box("86400")));
h.insert(header::VARY, HeaderValue::from_static(black_box("Origin")));
h.insert(header::SERVER, HeaderValue::from_static(black_box("ohkami")));
h.insert(header::CONNECTION, HeaderValue::from_static(black_box("Keep-Alive")));
Expand Down Expand Up @@ -591,11 +598,11 @@ use ohkami_benches::response_headers::{
#[bench] fn write_03_fxmap(b: &mut test::Bencher) {
let mut h = FxMap::new();
h
// .insert("Access-Control-Allow-Credentials", black_box("true"))
// .insert("Access-Control-Allow-Headers", black_box("X-Custom-Header,Upgrade-Insecure-Requests"))
// .insert("Access-Control-Allow-Origin", black_box("https://foo.bar.org"))
// .insert("Access-Conctrol-Allow-Methods", black_box("POST,GET,OPTIONS,DELETE"))
// .insert("Access-Control-Max-Age", black_box("86400"))
.insert("Access-Control-Allow-Credentials", black_box("true"))
.insert("Access-Control-Allow-Headers", black_box("X-Custom-Header,Upgrade-Insecure-Requests"))
.insert("Access-Control-Allow-Origin", black_box("https://foo.bar.org"))
.insert("Access-Conctrol-Allow-Methods", black_box("POST,GET,OPTIONS,DELETE"))
.insert("Access-Control-Max-Age", black_box("86400"))
.insert("Vary", black_box("Origin"))
.insert("Server", black_box("ohkami"))
.insert("Connection", black_box("Keep-Alive"))
Expand All @@ -617,11 +624,11 @@ use ohkami_benches::response_headers::{
#[bench] fn write_04_headermap(b: &mut test::Bencher) {
let mut h = MyHeaderMap::new();
h.set()
// .AccessControlAllowCredentials(black_box("true"))
// .AccessControlAllowHeaders(black_box("X-Custom-Header,Upgrade-Insecure-Requests"))
// .AccessControlAllowOrigin(black_box("https://foo.bar.org"))
// .AccessControlAllowMethods(black_box("POST,GET,OPTIONS,DELETE"))
// .AccessControlMaxAge(black_box("86400"))
.AccessControlAllowCredentials(black_box("true"))
.AccessControlAllowHeaders(black_box("X-Custom-Header,Upgrade-Insecure-Requests"))
.AccessControlAllowOrigin(black_box("https://foo.bar.org"))
.AccessControlAllowMethods(black_box("POST,GET,OPTIONS,DELETE"))
.AccessControlMaxAge(black_box("86400"))
.Vary(black_box("Origin"))
.Server(black_box("ohkami"))
.Connection(black_box("Keep-Alive"))
Expand All @@ -640,14 +647,14 @@ use ohkami_benches::response_headers::{
});
}

#[bench] fn write_05_header_hashbrown(b: &mut test::Bencher) {
let mut h = HeaderHashBrown::new();
#[bench] fn write_03_header_hashbrown(b: &mut test::Bencher) {
let mut h = HeaderHashBrown::<true>::new();
h
// .insert_standard_from_reqbytes(StandardHeader::AccessControlAllowCredentials, black_box(b"true"))
// .insert_standard_from_reqbytes(StandardHeader::AccessControlAllowHeaders, black_box(b"X-Custom-Header,Upgrade-Insecure-Requests"))
// .insert_standard_from_reqbytes(StandardHeader::AccessControlAllowOrigin, black_box(b"https://foo.bar.org"))
// .insert_standard_from_reqbytes(StandardHeader::AccessControlAllowMethods, black_box(b"POST,GET,OPTIONS,DELETE"))
// .insert_standard_from_reqbytes(StandardHeader::AccessControlMaxAge, black_box(b"86400"))
.insert_standard_from_reqbytes(StandardHeader::AccessControlAllowCredentials, black_box(b"true"))
.insert_standard_from_reqbytes(StandardHeader::AccessControlAllowHeaders, black_box(b"X-Custom-Header,Upgrade-Insecure-Requests"))
.insert_standard_from_reqbytes(StandardHeader::AccessControlAllowOrigin, black_box(b"https://foo.bar.org"))
.insert_standard_from_reqbytes(StandardHeader::AccessControlAllowMethods, black_box(b"POST,GET,OPTIONS,DELETE"))
.insert_standard_from_reqbytes(StandardHeader::AccessControlMaxAge, black_box(b"86400"))
.insert_standard_from_reqbytes(StandardHeader::Vary, black_box(b"Origin"))
.insert_standard_from_reqbytes(StandardHeader::Server, black_box(b"ohkami"))
.insert_standard_from_reqbytes(StandardHeader::Connection, black_box(b"Keep-Alive"))
Expand Down
Loading
Loading