1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-29 16:30:56 -05:00

perf(ext/http): Use flat list of headers for multiple set/get methods (#19336)

This PR attempts to resolve the first item on the list from
https://github.com/denoland/deno/issues/19330 which is about using a
flat list of interleaved key/value pairs, instead of a nested array of
tuples.

I can tackle some more if you can provide a quick example of using raw
v8 arrays, cc @mmastrac
This commit is contained in:
Kamil Ogórek 2023-06-02 17:59:16 +02:00 committed by Bartek Iwańczuk
parent 08813ea25e
commit 554d6ba649
No known key found for this signature in database
GPG key ID: 0C6BCDDC3B3AD750
2 changed files with 19 additions and 18 deletions

View file

@ -334,7 +334,12 @@ class InnerRequest {
if (this.#slabId === undefined) { if (this.#slabId === undefined) {
throw new TypeError("request closed"); throw new TypeError("request closed");
} }
return op_http_get_request_headers(this.#slabId); const headers = [];
const reqHeaders = op_http_get_request_headers(this.#slabId);
for (let i = 0; i < reqHeaders.length; i += 2) {
headers.push([reqHeaders[i], reqHeaders[i + 1]]);
}
return headers;
} }
get slabId() { get slabId() {
@ -570,7 +575,7 @@ function mapToCallback(context, callback, onError) {
if (headers.length == 1) { if (headers.length == 1) {
op_http_set_response_header(req, headers[0][0], headers[0][1]); op_http_set_response_header(req, headers[0][0], headers[0][1]);
} else { } else {
op_http_set_response_headers(req, headers); op_http_set_response_headers(req, headers.flat());
} }
} }

View file

@ -256,12 +256,11 @@ pub fn op_http_get_request_header(
} }
#[op] #[op]
pub fn op_http_get_request_headers( pub fn op_http_get_request_headers(slab_id: SlabId) -> Vec<ByteString> {
slab_id: SlabId,
) -> Vec<(ByteString, ByteString)> {
let http = slab_get(slab_id); let http = slab_get(slab_id);
let headers = &http.request_parts().headers; let headers = &http.request_parts().headers;
let mut vec = Vec::with_capacity(headers.len()); // Two slots for each header key/value pair
let mut vec = Vec::with_capacity(headers.len() * 2);
let mut cookies: Option<Vec<&[u8]>> = None; let mut cookies: Option<Vec<&[u8]>> = None;
for (name, value) in headers { for (name, value) in headers {
if name == COOKIE { if name == COOKIE {
@ -272,7 +271,8 @@ pub fn op_http_get_request_headers(
} }
} else { } else {
let name: &[u8] = name.as_ref(); let name: &[u8] = name.as_ref();
vec.push((name.into(), value.as_bytes().into())) vec.push(name.into());
vec.push(value.as_bytes().into());
} }
} }
@ -283,11 +283,10 @@ pub fn op_http_get_request_headers(
// TODO(mmastrac): This should probably happen on the JS side on-demand // TODO(mmastrac): This should probably happen on the JS side on-demand
if let Some(cookies) = cookies { if let Some(cookies) = cookies {
let cookie_sep = "; ".as_bytes(); let cookie_sep = "; ".as_bytes();
vec.push(( vec.push(ByteString::from(COOKIE.as_str()));
ByteString::from(COOKIE.as_str()), vec.push(ByteString::from(cookies.join(cookie_sep)));
ByteString::from(cookies.join(cookie_sep)),
));
} }
vec vec
} }
@ -313,18 +312,15 @@ pub fn op_http_set_response_header(slab_id: SlabId, name: &str, value: &str) {
} }
#[op] #[op]
pub fn op_http_set_response_headers( pub fn op_http_set_response_headers(slab_id: SlabId, headers: Vec<ByteString>) {
slab_id: SlabId,
headers: Vec<(ByteString, ByteString)>,
) {
let mut http = slab_get(slab_id); let mut http = slab_get(slab_id);
// TODO(mmastrac): Invalid headers should be handled? // TODO(mmastrac): Invalid headers should be handled?
let resp_headers = http.response().headers_mut(); let resp_headers = http.response().headers_mut();
resp_headers.reserve(headers.len()); resp_headers.reserve(headers.len());
for (name, value) in headers { for header in headers.chunks_exact(2) {
// These are valid latin-1 strings // These are valid latin-1 strings
let name = HeaderName::from_bytes(&name).unwrap(); let name = HeaderName::from_bytes(&header[0]).unwrap();
let value = HeaderValue::from_bytes(&value).unwrap(); let value = HeaderValue::from_bytes(&header[1]).unwrap();
resp_headers.append(name, value); resp_headers.append(name, value);
} }
} }