summaryrefslogtreecommitdiff
path: root/ext/http/http_next.rs
diff options
context:
space:
mode:
authorMatt Mastracci <matthew@mastracci.com>2023-06-12 17:43:49 -0600
committerGitHub <noreply@github.com>2023-06-12 23:43:49 +0000
commit397b22eccf0c388e3e287c523f169a9946fc06ce (patch)
treede4192f4c1d8a1291ea21e3b340d7bb60f1a7740 /ext/http/http_next.rs
parente8ed59208c225e0a0379d4df83bcfc32c4d1f908 (diff)
perf(ext/http): from_maybe_shared_unchecked for header values (#19478)
Prevents re-checking strings we already know are latin-1. Small improvement: 115k->116k
Diffstat (limited to 'ext/http/http_next.rs')
-rw-r--r--ext/http/http_next.rs19
1 files changed, 14 insertions, 5 deletions
diff --git a/ext/http/http_next.rs b/ext/http/http_next.rs
index 93634ae3e..f2e25555a 100644
--- a/ext/http/http_next.rs
+++ b/ext/http/http_next.rs
@@ -16,6 +16,7 @@ use crate::slab::slab_insert;
use crate::slab::SlabId;
use crate::websocket_upgrade::WebSocketUpgrade;
use crate::LocalExecutor;
+use bytes::Bytes;
use cache_control::CacheControl;
use deno_core::error::AnyError;
use deno_core::futures::TryFutureExt;
@@ -376,12 +377,17 @@ pub fn op_http_read_request_body(
}
#[op(fast)]
-pub fn op_http_set_response_header(slab_id: SlabId, name: &str, value: &str) {
+pub fn op_http_set_response_header(
+ slab_id: SlabId,
+ name: ByteString,
+ value: ByteString,
+) {
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.as_bytes()).unwrap();
- let value = HeaderValue::from_bytes(value.as_bytes()).unwrap();
+ let name = HeaderName::from_bytes(&name).unwrap();
+ // SAFETY: These are valid latin-1 strings
+ let value = unsafe { HeaderValue::from_maybe_shared_unchecked(value) };
resp_headers.append(name, value);
}
@@ -410,7 +416,9 @@ fn op_http_set_response_headers(
let v8_name: ByteString = from_v8(scope, name).unwrap();
let v8_value: ByteString = from_v8(scope, value).unwrap();
let header_name = HeaderName::from_bytes(&v8_name).unwrap();
- let header_value = HeaderValue::from_bytes(&v8_value).unwrap();
+ // SAFETY: These are valid latin-1 strings
+ let header_value =
+ unsafe { HeaderValue::from_maybe_shared_unchecked(v8_value) };
resp_headers.append(header_name, header_value);
}
}
@@ -425,7 +433,8 @@ pub fn op_http_set_response_trailers(
for (name, value) in trailers {
// These are valid latin-1 strings
let name = HeaderName::from_bytes(&name).unwrap();
- let value = HeaderValue::from_bytes(&value).unwrap();
+ // SAFETY: These are valid latin-1 strings
+ let value = unsafe { HeaderValue::from_maybe_shared_unchecked(value) };
trailer_map.append(name, value);
}
*http.trailers().borrow_mut() = Some(trailer_map);