From 1eacbdde090f817d8e61b03566521c043033d9ac Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= <biwanczuk@gmail.com>
Date: Thu, 10 Aug 2023 23:35:01 +0200
Subject: [PATCH] perf(http): use Cow<[u8]> for setting header (#20112)

---
 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..2e9b315ca14341 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);
 }