From fcb73129d065556cf0215818bcb60df369d03db1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 10 Aug 2023 03:35:43 +0200 Subject: [PATCH 1/2] perf(http): use Cow<[u8]> for setting header --- ext/http/http_next.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/ext/http/http_next.rs b/ext/http/http_next.rs index 3c3724924c6268..495e98a8492b67 100644 --- a/ext/http/http_next.rs +++ b/ext/http/http_next.rs @@ -385,18 +385,23 @@ pub fn op_http_read_request_body( state.resource_table.add_rc(body_resource) } -#[op2] +#[op2(fast)] pub fn op_http_set_response_header( #[smi] slab_id: SlabId, - #[serde] name: ByteString, - #[serde] value: ByteString, + #[string(onebyte)] name: Cow<[u8]>, + #[string(onebyte)] value: Cow<[u8]>, ) { let mut http = slab_get(slab_id); let resp_headers = http.response().headers_mut(); // These are valid latin-1 strings let name = HeaderName::from_bytes(&name).unwrap(); - // SAFETY: These are valid latin-1 strings - let value = unsafe { HeaderValue::from_maybe_shared_unchecked(value) }; + let value = match value { + Cow::Borrowed(bytes) => HeaderValue::from_bytes(&bytes).unwrap(), + // SAFETY: These are valid latin-1 strings + Cow::Owned(bytes_vec) => unsafe { + HeaderValue::from_maybe_shared_unchecked(bytes::Bytes::from(bytes_vec)) + }, + }; resp_headers.append(name, value); } From b41e161a06a74a30e2a7a0ef4f820a52651ca965 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 10 Aug 2023 04:05:03 +0200 Subject: [PATCH 2/2] lint --- ext/http/http_next.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/http/http_next.rs b/ext/http/http_next.rs index 495e98a8492b67..2e9b315ca14341 100644 --- a/ext/http/http_next.rs +++ b/ext/http/http_next.rs @@ -396,7 +396,7 @@ pub fn op_http_set_response_header( // These are valid latin-1 strings let name = HeaderName::from_bytes(&name).unwrap(); let value = match value { - Cow::Borrowed(bytes) => HeaderValue::from_bytes(&bytes).unwrap(), + Cow::Borrowed(bytes) => HeaderValue::from_bytes(bytes).unwrap(), // SAFETY: These are valid latin-1 strings Cow::Owned(bytes_vec) => unsafe { HeaderValue::from_maybe_shared_unchecked(bytes::Bytes::from(bytes_vec))